LNK1120 error

Hi,

Dear can you guide me to solve this link error. i think there is no problem visual studio settings.


1>AlgoPC.obj : error LNK2019: unresolved external symbol "bool __cdecl IndepTest::chi_square_test<class plCSVFileDataDescriptor<unsigned int>,class std::vector<int,class std::allocator<int> > >(class plCSVFileDataDescriptor<unsigned int> * const,int,int,class std::vector<int,class std::allocator<int> >)" (??$chi_square_test@V?

thnaks in advance
99.9% of the time "unresolved external symbol" means that the linker cannot find the body of a function. This could be because you prototyped the function, but never gave it a body... or (since this is a template related error), you separated your template stuff incorrectly.

A template's functions must be visible in every .cpp file which uses them, unless you specifically instantiate the template class so that it's linked throughout the project.

Long story short, you need to ensure both of the following:

1) Make sure all your functions have bodies
2) For templates, make sure those bodies are in the .h file, and not in a separate .cpp file.

There are ways around #2, but they're all significantly more complicated.
Dear thanks for your reply,

yes you are right. i am using namespace as a hearder .h and their definition in separate file .cpp ,

if i want to preserve my code separate from header, how i can resolve this problem

Again thanks
.h file
1
2
3
4
5
6
7
8
9
#ifndef _INDEPTEST_H
#define _INDEPTEST_H

namespace IndepTest
{
template < >
   void myfunction();
}


.cpp
1
2
3
4
5
6
7
8

void IndepTest::myfunction()
{
...
..

}


my main program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<.....h>


using namspace IndepTest

void main()
{

myfunction();


}






Last edited on
There's no way.
Can you suggest the best way ? i also want to keep separate code from header file.
closed account (S6k9GNh0)
??? The point of a header is usually to create prototypes so that they can be called. Prototypes call the main body of the function. The header doesn't exactly know how to define itself and as a result you need to include the header in the implementation file which contains the body of the functions.

1
2
3
4
5
//bob.h
class bob
{
    int ohnoes(bool wtf);
};


1
2
//bob.cpp
int bob::ohnoes) { return true; }


The combination above will NOT work unless you include the file with the prototype.

1
2
3
4
//bob.cpp
#include "bob.h"

int bob::ohnoes) { return true; }
yes you are right, but now i want to use template in bob.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

//bob.h
class bob
{
template<typename t>   
 int ohnoes(t wtf);
};


or 


//bob.h
template<typename t> 
class bob
{

 int ohnoes(t wtf);
};





Last edited on
When dealing with template classes and functions, the easiest solution is to include
the entire body of the class or functions in the header file. If you don't want to do
that, then the next easiest solution is to put the body in another file with an extension
of, say, .inl, and then #include the .inl file in the header file.

Topic archived. No new replies allowed.