Dav*_*ani 1 c++ destructor symbols class
这是我的代码:
#include <iostream>
using namespace std;
class new_class{
public:
new_class();
float multiplication(){return x*y;}
~new_class();
private:
float x;
float y;
};
int main()
{ new_class class_11;
cout<<class_11.multiplication()<<endl;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误日志:
Main.obj : error LNK2001: unresolved external symbol "public: __thiscall new_class::~new_class(void)" (??1new_class@@QAE@XZ)
Main.obj : error LNK2001: unresolved external symbol "public: __thiscall new_class::new_class(void)" (??0new_class@@QAE@XZ)
Run Code Online (Sandbox Code Playgroud)
我正在使用Visual Studio 2010,visual c ++可以任何人解释我我做错了什么?
你还没有定义你的构造函数或析构函数,你刚刚声明了它们.
必须在某处定义程序中使用的任何函数.函数定义包含函数声明及其定义.例如,您的multiplication成员函数已定义:
float multiplication() { return x * y; }
^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
function declaration this makes the declaration a definition
Run Code Online (Sandbox Code Playgroud)
"未解析的外部符号"错误意味着编译器找到了函数的声明,但链接器无法找到定义.因此,您需要为链接器指出的两个函数提供定义:默认构造函数和您声明的析构函数.
这就是说,注意,如果你不声明任何构造函数,编译器会隐式为类,这往往是足以提供一个默认的构造函数.如果不声明析构函数,编译器将隐式提供析构函数.因此,除非您确实需要在构造函数或析构函数中执行某些操作,否则您无需自己声明和定义它们.
确保你有一本很好的入门C++书.这本书将详细介绍如何定义成员函数以及编写构造函数和析构函数的最佳实践(正确编写析构函数充满了危险).
| 归档时间: |
|
| 查看次数: |
3162 次 |
| 最近记录: |