:未定义对 'testClass::sum()' 的引用
: undefined reference to `testClass::sum()'
#include<iostream>
using namespace std;
class testClass
{
public:
int sum();
//Postcondition: Returns the sum of the
// private data members.
void print();
//Prints the values of the private data members.
testClass();
//default constructor
//Postcondition: x = 0; y = 0;
testClass(int a, int b);
//Constructor with parameters
//Initializes the private data members to the
//values specified by the parameters.
//Postconditon: x = a; y = b
private:
int x;
int y;
};
int sum()
{
int a, b, total;
total = a + b;
return total;
}
void print()
{
cout<< "sum = " << sum() << endl;
}
testClass::testClass()
{
x = 0;
y = 0;
}
testClass::testClass(int a, int b)
{
x = a;
y = b;
}
该程序编译 100% 正常,但是当我运行它时,我收到以下错误:--------------------配置:mingw5 - CUI 调试,生成器类型:MinGW
--------------------正在检查文件依赖关系...连接。。。[错误]C:\Dev-Cpp\MAlikChapter1\Exercise14.cpp:56: 未定义对testClass::sum()'
[Error] C:Dev-CppMAlikChapter1Exercise14.cpp:58: undefined reference to
testClass::p rint()'[错误] collect2:ld 返回 1 个退出状态
完成制作练习14:3 个错误,0 个警告
int main()
{
int m, n;
testClass mySum;
testClass myPrint;
mySum.sum();
myPrint.print();
}
这是一个示例程序,来自:Malik "使用 C++ 的数据结构"
在
类外实现函数时sum
和print
函数的名称前添加testClass::
:
int testClass::sum()
{
// ...
}
void testClass::print()
{
// ...
}
这将解决未定义的引用错误,但sum
函数中还有另一个错误。您声明局部a
和b
变量而不初始化它们,然后在表达式中使用它们 a + b
.要么用某些东西初始化它们,要么如果你想对类成员求和x
和y
,那么使用这些变量而不是a
和b
,添加testClass::
后就可以访问它们了。
相关文章:
- 将对象数组的引用传递给函数
- 什么时候在C++中返回常量引用是个好主意
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 何时在引用或唯一指针上使用移动语义
- 如何在c++中使用引用实现类似python的行为
- 编译C++时未定义的引用
- Ctypes wstring通过引用传递
- c++r值引用应用于函数指针
- 理解c++中的引用
- C++取消引用指针.为什么会发生变化
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- C++Boost Asio Pool线程,带有lambda函数和传递引用变量
- 强制转换为引用类型
- 引用一个已擦除类型(void*)的指针
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 具有默认值的引用获取函数
- 如何使用基类指针引用派生类成员
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- :未定义对 'testClass::sum()' 的引用