473,322 Members | 1,540 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,322 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 2094
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.