Undefined Reference Problem When Using Multiple Files

So on one of my project programs I am having an issue trying to get rid of this error:
main.cpp:(.text+0xc9): undefined reference to `void readCin<int>(int&)'


Instead of posting my entire program here I made a smaller one that wound up creating the same problem to see if anyone can help. I'm sure it's some beginning syntax/#include thing I'm forgetting but here it is anyway:

main.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
#include <iostream>
using namespace std;

template <typename X>
void readCin(X& var);

int main()
{
	int i, j, k, l;
	char m;
	
	cout<<"Hello! This is a test!"<<endl;
	
	cout<<"1"<<endl;
	readCin(i);
	cout<<"2"<<endl;
	readCin(j);
	cout<<"3"<<endl;
	readCin(k);
	cout<<"4"<<endl;
	readCin(l);
	cout<<"L"<<endl;
	readCin(m);
	
	cout<<endl<<i<<"  "<<j<<"  "<<k<<"  "<<l<<"  "<<m<<endl;

	
	cout<<endl<<"Press Enter To Continue: ";
	//clearinput();
	cin.ignore();
  return 0;
}


main_fucts.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

template <typename X>
void readCin(X& var)
{
	
	cin>>var;
	cin.ignore();
	
}
Hi ,
running into the same error when i run on my side .. not able to understand as why getting the error .
templates cannot be defined in a separate source file like that. You're getting this error for the same reason you get an error if you try to put the readCin prototype in a header file.

In order for functions to be linked, they must be instantiated. Function templates are not instantiated until they're called.

So basically:

-) main_functs does not instantiate readCin, it simply defines the template.
-) main.cpp is requiring that readCin<int> and readCin<char> be instantiated
-) main.cpp can't instantiate them itself because it doesn't see the template body
-) main_functs could instantiate them, but it doesn't know that it needs to, so it doesn't.


The solution here is to put the readCin body either in main.cpp where the prototype is, or move it and the prototype to a header file and #include that from main.cpp.

Either way, the entire function body must be in every cpp file that uses it. That's just how templates work.
"main_fucts.cpp" should probably be "main_fucts.h" and then instead of the template declaration of readCin() before main() you should have #include "main_fucts.h". Also cin.ignore() without any arguments will only clear 1 character from the input buffer which may not be what you had envisioned.
Alright, thanks. Will try it out. And as far as
Also cin.ignore() without any arguments will only clear 1 character from the input buffer which may not be what you had envisioned.
, I'm using them to get rid of the extra '\n' that is being sent through every time enter is pressed. If I don't do this then:
1
2
3
cout<<endl<<"Press Enter To Continue: ";
//clearinput();
cin.ignore();

this won't work correctly and I'll wind up having to hit enter about 4 times instead of the one.

But thanks. I didn't know that templates were a bit of a special case.
@Disch
Even i put all togather in the same file .. even then also i am getting the error.
You shouldn't be. Can you post your code?
@Disch
its working.. i think i did not build it first time ..
thanks Disch
Topic archived. No new replies allowed.