473,382 Members | 1,362 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

template class problem

Hi All,

Not sure if this is a c++ issue or a compiler issue.

I have the following in a class:

template<class T>
bool Utility::getInput(T& i, T low, T up){
bool result = false;
if(cin >i){
if((i >= low)&&(i <= up)){
result = true;
}
} else {
cin.clear();
cin.ignore(1000, '\n');
}
return result;
}

in another file and class I have :

Utility utility;
utility.getInput(sal, 0.0, 999999.99);
utility.getInput(sid, 0, 999999);

Now, if I put all this in one file it compiles no worries. But, if I compile
it as is i get an error from the linker:

[linker error] Undefined reference to 'bool
Utility::getInput<double>(double&, double, double)'

Why if the linker can't find the reference to 'bool
Utility::getInput<double>(double&, double, double)' does it not complain
about the (int&, int, int) reference that comes before it in the code? How
can I resolve the error? The only linker errors I've had before have been
related to not including files in the compile command, but this is something
else....

Thanks for your help.

Regards

Michael
Mar 28 '07 #1
3 2099
On Mar 28, 12:51 pm, "michael" <s...@begone.netwrote:
Hi All,

Not sure if this is a c++ issue or a compiler issue.

I have the following in a class:

template<class T>
bool Utility::getInput(T& i, T low, T up){
bool result = false;
if(cin >i){
if((i >= low)&&(i <= up)){
result = true;
}
} else {
cin.clear();
cin.ignore(1000, '\n');
}
return result;

}

in another file and class I have :

Utility utility;
utility.getInput(sal, 0.0, 999999.99);
utility.getInput(sid, 0, 999999);

Now, if I put all this in one file it compiles no worries. But, if I compile
it as is i get an error from the linker:

[linker error] Undefined reference to 'bool
Utility::getInput<double>(double&, double, double)'

Why if the linker can't find the reference to 'bool
Utility::getInput<double>(double&, double, double)' does it not complain
about the (int&, int, int) reference that comes before it in the code? How
can I resolve the error? The only linker errors I've had before have been
related to not including files in the compile command, but this is something
else....

Thanks for your help.

Regards

Michael
can you post a minimal testcase that compiles but does not link to
demonstrate your problem?
m.

Mar 28 '07 #2

michael schrieb:
Hi All,

Not sure if this is a c++ issue or a compiler issue.

I have the following in a class:

template<class T>
bool Utility::getInput(T& i, T low, T up){
bool result = false;
if(cin >i){
if((i >= low)&&(i <= up)){
result = true;
}
} else {
cin.clear();
cin.ignore(1000, '\n');
}
return result;
}

in another file and class I have :

Utility utility;
utility.getInput(sal, 0.0, 999999.99);
utility.getInput(sid, 0, 999999);

Now, if I put all this in one file it compiles no worries. But, if I compile
it as is i get an error from the linker:

[linker error] Undefined reference to 'bool
Utility::getInput<double>(double&, double, double)'

Why if the linker can't find the reference to 'bool
Utility::getInput<double>(double&, double, double)' does it not complain
about the (int&, int, int) reference that comes before it in the code? How
can I resolve the error? The only linker errors I've had before have been
related to not including files in the compile command, but this is something
else....

Thanks for your help.

Regards

Michael
Is the definition and implementation of this function inside the same
header? You have to put the definition into the same header or into
one that is included by this header.

Mar 28 '07 #3
On Mar 28, 2:51 pm, "michael" <s...@begone.netwrote:
Hi All,

Not sure if this is a c++ issue or a compiler issue.

I have the following in a class:

template<class T>
bool Utility::getInput(T& i, T low, T up){
bool result = false;
if(cin >i){
if((i >= low)&&(i <= up)){
result = true;
}
} else {
cin.clear();
cin.ignore(1000, '\n');
}
return result;

}

in another file and class I have :

Utility utility;
utility.getInput(sal, 0.0, 999999.99);
utility.getInput(sid, 0, 999999);

Now, if I put all this in one file it compiles no worries. But, if I compile
it as is i get an error from the linker:

[linker error] Undefined reference to 'bool
Utility::getInput<double>(double&, double, double)'

Why if the linker can't find the reference to 'bool
Utility::getInput<double>(double&, double, double)' does it not complain
about the (int&, int, int) reference that comes before it in the code? How
can I resolve the error? The only linker errors I've had before have been
related to not including files in the compile command, but this is something
else....

Thanks for your help.

Regards

Michael
The problem is not in the code.You seem to not know how to device
modular (multi-file projects) programming with C++ .Read the
documentation for your compiler.
[linker error] Undefined reference to 'bool
Utility::getInput<double>(double&, double, double)'
this means that you have refered to(called/used) an
identifier(function in this context) that the linker can not find in
any file.

Mar 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
4
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
6
by: Adam Parkin | last post by:
Hello, all I'm having a problem with friend functions in a templatized Queue class I'm writing using linked lists. The problem is that I can't get the friend function to be able to access private...
7
by: Drew McCormack | last post by:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this: template <typename T> class M { static Registrar<M>...
0
by: Leslaw Bieniasz | last post by:
Cracow, 16.09.2004 Hi, I have a problem with compiling the following construction involving cross-calls of class template methods, with additional inheritance. I want to have three class...
2
by: Siegfried Weiss | last post by:
Hi guys, i give up finding a solution by reading or by trial & error. Hope, YOU can help me! (Sorry for my rather long posting.) Stroustrup says, that templates could be declared with - type...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
4
by: danilo.turina | last post by:
Hi all, today I encountered a problem that I'm only able to solve by using reinterpret_cast (and I'd like to avoid it). This was my code until yesterday (omitting includes, "using namespace"...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.