开发者

What wrong with class destructor symbol ? in vc++

开发者 https://www.devze.com 2023-02-26 18:03 出处:网络
Here is my code : #include <iostream> using namespace std; class new_class{ public: new_class(); float multiplication(){return x*y;}

Here is my code :

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

Error log :

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)

I am using Visual studio 2010 , visual c++ can anyone explain me What i a开发者_开发百科m doing wrong ?


You haven't defined your constructor or destructor, you've just declared them.

Any function that is used in your program must be defined somewhere. A function definition consists of the function declaration followed by its definition. For example, your multiplication member function is defined:

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

The "unresolved external symbol" error means that the compiler found the declaration of the function but the linker was unable to find the definition. So, you need to provide definitions for the two functions noted by the linker: the default constructor and the destructor that you declared.

That said, note that if you don't declare any constructors, the compiler will implicitly provide a default constructor for your class, which is often sufficient. If you don't declare a destructor, the compiler will implicitly provide a destructor. So, unless you actually need to do something in the constructor or destructor, you don't need to declare and define them yourself.

Make sure that you have a good introductory C++ book. Such a book will go into greater detail about how member functions are defined and best practices for writing constructors and destructors (correctly writing a destructor is fraught with peril).


you should implement destructor, you've just declared it

0

精彩评论

暂无评论...
验证码 换一张
取 消