I am new to classes and was trying to create an elmentary program to understand classes and objects.
I ve created three files
1) header file for class declaration
2) class definition .cpp file
3) class implementation main .cpp file

I cannot understand the error I am getting. Please help.

"1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall sum::add(int,int)" (?add@sum@@QAEXHH@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall sum::sum(void)" (??0sum@@QAE@XZ) referenced in function _main
1>c:\users\haider\documents\visual studio 2010\Projects\Learn_class\Debug\Learn_class.exe : fatal error LNK1120: 2 unresolved externals

Please see below my code for the files.

//1) Header file sum.h
    class sum

        sum ();
    void add(int , int);


    // 2)Class defination file

    #include "sum.h"
    using namespace std;
    void sum::add(int a, int b)
    { int c;
    cout<<"sum is: "<<c;

    //3) Class implementation file

    #include "sum.h"
    using namespace std;

    int main()
        sum sum1;
        int a,b;
        cout<<"enter the nos";


Recommended Answers

All 5 Replies

Congratulations! You're no longer a DaniWeb newbie.<br /> <br />
Your DaniWeb account has just been upgraded from newbie status and now you have the ability to take advantage of everything the community has to offer.<br /> <br />
You can now enjoy an advertisement-free DaniWeb by ticking the checkbox to Disable Ads in your profile. You will no longer have to fill out the human verification check when you post. You can also now send unlimited private messages, participate in live chat, contribute new code snippets, and tag articles with never-before-used tags.

How are you building the files together into an executable? The code looks fine, but the errors suggests that main.cpp and definition.cpp aren't linked together.

I have just created files in the same project in visual studio 2010. I am not aware if I have to link them..can you please tell me how to link both the files.

Then I'm not sure. You're definitely missing a definition for the sum constructor, but that shouldn't cause a linker error with add.

One problem is, in sum.h, you set up sum(), but you didn't implement it. Add this to your class definition file:


On a side note, you might find it more versatile to return the answer and keep the console I/O in the calling procedure. In a large project you may find that sometimes you need to display the answer and sometimes you don't. This way your function remains the same.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.