c++ - 函数调用错误中的参数过多
问题描述
我正在创建一个类的对象并编写了一个构造函数并正在添加一个对象。我相信我有足够的函数变量,但它说我有太多的参数。我不明白它为什么这么说。
我尝试重做我的构造函数和代码,但我继续遇到同样的错误。我最终希望能够克隆该对象,但我也不知道该怎么做。
#include "pch.h"
#include <iostream>
#include <string>
using namespace std;
class Animal {
public:
Animal() {};
Animal(string uAName, string uASize, string uAColor, int uANumLegs)
: aName(uAName), aSize(uASize), aColor(uAColor), numLegs(uANumLegs) {};
void printAnimal(Animal) {
cout << "Your animal is: " << aName << endl;
cout << "The animal size is: " << aSize << endl;
cout << "The animal Color is: " << aColor << endl;
cout << "The animal has " << numLegs << " legs" << endl;
}
virtual Animal* clone() { return (new Animal(*this)); }
void aClone(Animal* nAnimal) {
Animal* cal = nAnimal->clone();
}
private:
string aName = "";
string aSize= "";
string aColor = "";
int numLegs = 0;
};
int main()
{
Animal newAnimal();
string uName = "Bear";
string uSize = "Large";
string uColor = "Black";
int uLegs = 4;
newAnimal(uName, uSize, uColor, uLegs);
}
解决方案
Animal newAnimal();
是函数声明,而不是变量声明(由于“最令人烦恼的解析”)。因此调用newAnimal(uName, uSize, uColor, uLegs);
试图调用具有 4 个值的 0 参数函数,因此出现错误。
即使您修复了该声明(通过删除括号),您的代码仍然无法编译,因为newAnimal(uName, uSize, uColor, uLegs);
那时会尝试operator()
在newAnimal
对象上调用,但您的类没有实现operator()
.
要调用您的类构造函数,您需要main()
改为:
int main() {
string uName = "Bear";
string uSize = "Large";
string uColor = "Black";
int uLegs = 4;
Animal newAnimal(uName, uSize, uColor, uLegs);
}
推荐阅读
- grep - 如何使用 grep for Windows 获取文本文件中第一个匹配项的第一部分?
- node.js - 在nodejs中以正确的方式读写typedArray
- python - asyncio 任务仅在调用 asyncio.sleep 后启动
- python - Python Logging 模块 - 记录器未登录正确的级别
- python - 具有滚动平均值的数据透视表
- sql - 如何创建一个 ISA 层次结构,其中 SQL 中只允许存在一种子类型?
- lua - 循环两个宏
- sql - 如何使用 curl 语句获取 influxDB 的列名(标签)?
- java - 使用递归函数列出给定字符串中所有可能的 IP 地址组合
- docker - Docker:来自守护进程的错误响应:未能在网桥上创建端点 priceless_noether: