类析构函数符号出了什么问题?在vc++中

What wrong with class destructor symbol ? in vc++

本文关键字:vc++ 问题 什么 析构函数 符号      更新时间:2023-10-16

这是我的代码:

   #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;
}

错误日志:

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)

我正在使用Visual studio 2010,Visual c++有人能解释我吗我做错了什么

您还没有定义您的构造函数或析构函数,您只是声明了它们。

程序中使用的任何函数都必须在某个地方定义。函数定义由函数声明及其定义组成。例如,您的multiplication成员函数定义为:

float multiplication() { return x * y; }
^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^
function declaration   this makes the declaration a definition

"未解析的外部符号"错误意味着编译器找到了函数的声明,但链接器找不到定义。因此,您需要为链接器指出的两个函数提供定义:默认构造函数和您声明的析构函数。

也就是说,请注意,如果您没有声明任何构造函数,编译器将隐式地为您的类提供默认构造函数,这通常就足够了。如果不声明析构函数,编译器将隐式提供析构函数。所以,除非你真的需要在构造函数或析构函数中做一些事情,否则你不需要自己声明和定义它们。

确保你有一本很好的C++入门书。这本书将更详细地介绍如何定义成员函数,以及编写构造函数和析构函数的最佳实践(正确编写析构函数充满危险(。

您应该实现析构函数,您刚刚将其声明为