Linker Error

Aug 7, 2009 at 11:20am
Hi.
I am using a class and an application using it.When I have my class implementation and interface in one file,every thing is OK;but as soon as I put them in two separated files, I get this error:
[Linker error] undefined reference to `ClassName::ClassMemberFunc()'

I do every thing OK as follow:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//ClassFile.h
#ifndef CLASSNAME_H
#define CLASSNAME_H

#include<string>
#include<iostream>
#include<fstream>
#include<vector>

using namespace std;

class ClassName
{
    private:
        ..
    public:
         void ClassMemberFunc()
        ..
};


#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//ClassFile.cpp
#include<string>
#include<iostream>
#include<fstream>
#include<vector>
#include "ClassFile.h"

using namespace std;
..
..
//Defenitions
..

void ClassName::ClassMemberFunc()
{
}

..


And I put #include "ClassFile.h" in my application.

I really appreciate your help.
Aug 7, 2009 at 11:24am
You are compiling and linking ClassFile.cpp, right?
Aug 7, 2009 at 11:27am
When I compile Class.cpp I get this error:
[Linker error] undefined reference to `WinMain@16'
ld returned 1 exit status
Aug 7, 2009 at 11:38am
What kind of application are you creating is it a DLL or an EXE ?
because from the error it seems that you dont have not written winmain in your executable file.
Because when I copied your above code and tried it worked perfectly fine with the main added in the third file.

#include "ClassFile.h"

using namespace std;

int main()
{
ClassName N;
N.ClassMemberFunc();
}

So in my opinion you are trying to build an executable and have not written any main or winmain in that.
Aug 7, 2009 at 11:44am
I have a "main" and not a "winmain". Exactly like you did . I am so confused.
Aug 7, 2009 at 12:01pm
Ok can you please again tell me your application strucutre.
according to me you should be having :
1. ClassFile.h -> Containing class declaration
2. ClassFile.cpp -> containing implementation of each member function of class
3. <Somefile>.cpp -> Where this main resides

Also what kind of application you have choosen to create from the app wizard of VC++ i.e. console app or windows app ?

Aug 7, 2009 at 12:14pm
I have got these 3 files you mentioned and I am using Dev-c++ to write a simple .cpp application ,and actually this is a console app.I tried it in VC++(console app) and I got "error LNk2019".
Aug 7, 2009 at 12:37pm
It has to do with your project settings because LNK2019 means unresolved external symbol.
It seems that this has to do with your project settings in VC++ and if you can go back to vc++ and check that all three files are added in project.

i.e. ClassFile.h, ClassFile.cpp and <ThirdFileWithMain>.cpp
because chances are you have included ClassFile.h in <ThirdFileWithMain>.cpp but have not added them to the project which means that compiler is able to deduce the the declaration of the function and class but Linker is not able to find the definition of these functions and classes.

Check that all the files should be in path, and should be included in project.

Then it should work
Aug 7, 2009 at 12:47pm
Thanks so much.It worked in VC++. ;)
I had the files in the same directory but I hadn't added them in project.
Topic archived. No new replies allowed.