在C++中使用类时出错(与调用 Class_name 不匹配)
error in using class in C++ (no match for call to Class_name)
当我尝试编译下面的代码时,我收到以下错误:
homework3_test.cpp:111:25: 错误:对"(PowerN) (int&)"的调用不匹配 power_three(test_power2);
PowerN 是一个类。
class PowerN{
public:
static int i;
PowerN(int a);
};
power_three定义为
功率N power_three(3);
这个很好,而"3"是一个整数。但对于以下三个整数变量:
国际test_power1、test_power2 test_power3;
它返回错误。那么它们为什么不同呢?是int test_power1没有值所以它变成int&的原因吗?我该如何解决这个问题?
下面是PowerN的代码
PowerN::PowerN(int a){
int b=0;
if (i>0){
a = pow(b,(i-1));
}
else {
b=a;
}
i= i+1;
}
更新:PowerN 需要更改它需要的第 2、3、4 个整数的值,以下是要求:
PowerN 的构造函数采用单个整数 N。每次调用 PowerN 实例的函数运算符时,它都会将其参数的值更改为 N**x,其中 x 是调用函数运算符的次数。例如:
int x;
PowerN power_three(3);
power_three(x);//x will now be 1
power_three(x);//x will now be 3
power_three(x);//x will now be 9
重现错误的代码是:
homework3_test.cpp:111:25: 错误:对"(PowerN) (int&)"的调用不匹配
power_three(test_power2);
问题出现在更新的代码中:
int x;
PowerN power_three(3);
power_three(x);//x will now be 1
最后一行尝试调用PowerN::operator()
。但是,您没有定义operator()
。错误消息有点神秘,但它说它寻找了PowerN::operator()(int &)
但没有找到它。
我不知道你想象会发生什么,会导致"x 现在将是 1"。 似乎您正在尝试在已存在的对象上再次调用构造函数。 这是不合法的;即使是这样,语法也会有所不同。
也许您应该将构造函数中当前的一些代码移动到成员函数中(例如calculate(int &)
,然后执行以下操作:
PowerN power_three(3);
power_three.calculate(x);
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么 class 的函数没有在 main 中被调用?
- 使用基类对象调用Dervied Class函数
- 调用 list.size() 方法给出错误"expression must have class type"
- 稍后使用<Class>调用类构造函数创建 std::vector
- 调用'class'没有匹配函数
- 通过在每个元素上调用名称(),将类的向量转换为clangtype(class :: name())的向量
- 使用带有对象的数组调用类中的方法,错误"non-class type",c++
- c 参考class对象的返回 - 为什么未调用复制构造函数
- 调用从C++代码导出在DLL中的Delphi CLASS
- 不能在没有对象的情况下调用成员函数 std::string class::function()
- 从外部使用"class::func()"调用非静态函数或构造函数
- "Expression must have class type" 错误,尝试调用类函数
- 从 c++ 调用 COM dll,"Class Not Registered"
- 错误:重载的“bind(int(Class::*)(int,int),Class*,int,int”的调用不明确