LNK1120 error

Hi,
Dear all

My code was build successfully. But after that i shifted header files in the proper folders (because before in the project folder).

but it generate link errors. I think in the main it is not finding method chi_square_test();

1>testchi2.obj : error LNK2019: unresolved external symbol "bool __cdecl chi_square_test<class plCSVFileDataDescriptor<int>,class std::vector<int,class std::allocator<int> > >(class plCSVFileDataDescriptor<int> * const,int,int,class std::vector<int,class std::allocator<int> >,float,double *,double *)" (??$chi_square_test@V?$plCSVFileDataDescriptor@H@@V?$vector@HV?$allocator@H@std@@@std@@@@YA_NQAV?$plCSVFileDataDescriptor@H@@HHV?$vector@HV?$allocator@H@std@@@std@@MPAN2@Z) referenced in function _main

1>C:\ProBT_SLP\work\yasin\testChi2\Debug\Chi2test.exe : fatal error LNK1120: 1 unresolved externals


test main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <pl.h>
#include <iostream>
#include <conio.h>

#include "cond_indep/indepTest.h"

using namespace std;


int main()
{

	static string file_name;
	cout << "\n Enter data file along with CSV extension :\n ";
	cin >> file_name;
	

plCSVFileDataDescriptor<int> desc(file_name, 8);
std::vector < int >  vars;

cout << "To calculate independence test between two variables A and B, please enter variable numbers ( from 1 to "
<< desc.get_num_fields() << std::endl;

int var_a = -1;
int var_b = -1;
cin >> var_a >> var_b;

cout<< "\n Please enter conditioning set of variables (numbers from 1 to " << desc.get_num_fields() << ")";
cout<<"\nFirst enter total number of variables (0 for nothing)then individual variable numbers" << std::endl;
unsigned int total = 0;
cin >> total;

if (total){
	cout<<"\n Please enter "<<total<<" variables" << std::endl;
	for (unsigned int i = 0; i < total; ++i) {
		int given_var = -1;
		std::cin >> given_var;  
		vars.push_back(given_var);
	}
}
float alpha = 0.05;
double chi=0, p=0;

if ( chi_square_test(&desc, var_a, var_b, vars, alpha, &chi, &p))

	std::cout << "\n\n chi2 value is ="<<chi <<"\n p value is = "<<p <<" \nVariables are independent ";

else
	
	std::cout << "\n\n chi2 value is ="<<chi <<"\n p value is = "<<p <<" \n Variables are dependent ";


getch();

}


indepTest.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef _INDEPTEST_H
#define _INDEPTEST_H

#include <pl.h>
#include <iostream>
#include <conio.h>
#include <math.h>
#include <boost/math/distributions/chi_squared.hpp>

using namespace std;
using namespace boost;


template<typename T, typename V>
bool chi_square_test(T * const pCompDataSet, int var_a, int var_b, V vars);

template<typename T, typename V>
bool chi_square_test(T * const pCompDataSet, int var_a, int var_b, V vars, float alpha);

template<typename T, typename V>
bool chi_square_test(T * const pCompDataSet, int var_a, int var_b, V vars, float alpha, double *chi, double *p);


#endif // 


indepTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <pl.h>
#include <iostream>
#include <conio.h>
#include <math.h>
#include <boost/math/distributions/chi_squared.hpp>

#include "cond_indep/indepTest.h"
//#include "indepTest.h"

using namespace std;
using namespace boost;


template<typename T, typename V>
bool chi_square_test(T * const pCompDataSet, int var_a, int var_b, V vars)
{
	float alpha = 0.05;
	double chi=0;
	double p=0;

	
    return chi_square_test(pCompDataSet, var_a, var_b, vars, alpha, &chi, &p);
}

template<typename T, typename V>
bool chi_square_test(T * const pCompDataSet, int var_a, int var_b, V vars, float alpha)
{
	double chi=0;
	double p=0;
	
	return chi_square_test(pCompDataSet, var_a, var_b, vars, alpha, &chi, &p);
		
}

template<typename T, typename V>
bool chi_square_test(T * const pCompDataSet, int var_a, int var_b, V vars, float alpha, double *chi, double *p)
{
	
	bool ind;
	unsigned int  df=1, counter=0;
	
	
	///****************input from user for indep test******************
	// vars contains indices of variables in the CSV file, but 1-based
	//cout<< "\n\n successfully called function 3" ;
	vars.push_back(var_b);
	vars.push_back(var_a);

	///******************** loading variables in vectors****************************

...........
............

When you are using templates, you need the implementation of the functions to be in the header in which they are declared, not in a separate cpp file
closed account (S6k9GNh0)
Templates and multiple-file projects
From the point of view of the compiler, templates are not normal functions or classes. They are compiled on demand, meaning that the code of a template function is not compiled until an instantiation with specific template arguments is required. At that moment, when an instantiation is required, the compiler generates a function specifically for those arguments from the template.

When projects grow it is usual to split the code of a program in different source code files. In these cases, the interface and implementation are generally separated. Taking a library of functions as example, the interface generally consists of declarations of the prototypes of all the functions that can be called. These are generally declared in a "header file" with a .h extension, and the implementation (the definition of these functions) is in an independent file with c++ code.

Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.

Since no code is generated until a template is instantiated when required, compilers are prepared to allow the inclusion more than once of the same template file with both declarations and definitions in a project without generating linkage errors.


http://www.cplusplus.com/doc/tutorial/templates/
Last edited on
Topic archived. No new replies allowed.