* simple error woth templates
@ 2004-04-22 14:27 Andre Kirchner
2004-04-22 14:32 ` Eljay Love-Jensen
2004-04-22 17:42 ` llewelly
0 siblings, 2 replies; 4+ messages in thread
From: Andre Kirchner @ 2004-04-22 14:27 UTC (permalink / raw)
To: gcc-help
Hi,
I have made this program to learn templates, but gcc
keeps says that there's an undefined reference to
`Dog<int,long>::Dog[in-charge](int,int,long,log)` at
the line marked
bellow.
Could someone show me what's wrong?
Thanks in advance,
Andre
**** Dog.h *****
#ifndef IOSTREAM
#define IOSTREAM
#include <iostream.h>
#endif
template < class CLASS_A, class CLASS_B >
class Dog
{
public:
Dog( CLASS_A firstNumber, CLASS_A secondNumber,
CLASS_B thirdNumber, CLASS_B forthNumber );
~Dog(){};
void showBigger();
private:
CLASS_A number_A;
CLASS_B number_B;
};
*** Dog.cpp ***
#ifndef DOG
#define DOG
#include "Dog.h"
#endif
template < class CLASS_A, class CLASS_B >
Dog< CLASS_A, CLASS_B >::Dog( CLASS_A firstNumber,
CLASS_A secondNumber, CLASS_B thirdNumber, CLASS_B
forthNumber )
{
number_A = ( firstNumber>secondNumber
)?firstNumber:secondNumber;
number_B = ( thirdNumber>forthNumber
)?thirdNumber:forthNumber;
}
template < class CLASS_A, class CLASS_B >
void Dog< CLASS_A, CLASS_B >::showBigger()
{
cout << number_A << endl;
cout << number_B << endl;
}
*** main.cpp ***
#ifndef DOG
#define DOG
#include "Dog.h"
#endif
int main( int argc, char *argv[] )
{
Dog < int, long > theDog( 10, 20, 30l, 40l );
// theDog.showBigger;
return( 0 );
}
__________________________________
Do you Yahoo!?
Yahoo! Tax Center - File online by April 15th
http://taxes.yahoo.com/filing.html
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: simple error woth templates
2004-04-22 14:27 simple error woth templates Andre Kirchner
@ 2004-04-22 14:32 ` Eljay Love-Jensen
2004-04-22 17:42 ` llewelly
1 sibling, 0 replies; 4+ messages in thread
From: Eljay Love-Jensen @ 2004-04-22 14:32 UTC (permalink / raw)
To: Andre Kirchner, gcc-help
Hi Andre,
It appears that you neglected to #include "Dog.cpp" in your Main.cpp source
file.
Personally, I'm not a proponent of naming a header file with a ".cpp"
extension. But that's just me.
HTH,
--Eljay
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: simple error woth templates
2004-04-22 14:27 simple error woth templates Andre Kirchner
2004-04-22 14:32 ` Eljay Love-Jensen
@ 2004-04-22 17:42 ` llewelly
1 sibling, 0 replies; 4+ messages in thread
From: llewelly @ 2004-04-22 17:42 UTC (permalink / raw)
To: Andre Kirchner; +Cc: gcc-help
Andre Kirchner <supercroc1974@yahoo.com> writes:
> Hi,
>
> I have made this program to learn templates, but gcc
> keeps says that there's an undefined reference to
> `Dog<int,long>::Dog[in-charge](int,int,long,log)` at
> the line marked
> bellow.
> Could someone show me what's wrong?
[snip]
gcc does not support seperate compilation for templates.
You must #include the definition of each template in each
translation unit which uses it.
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: simple error woth templates
@ 2004-04-22 14:51 Lev Assinovsky
0 siblings, 0 replies; 4+ messages in thread
From: Lev Assinovsky @ 2004-04-22 14:51 UTC (permalink / raw)
To: Andre Kirchner, gcc-help
Hi!
All template related code (declarations and definitions) should be avaliable
to the caller (main in your case) at compile time.
I put your code into one file and
it works fine.
----
Lev Assinovsky
Aelita Software Corporation
(now is a part of Quest Software)
O&S InTrust Framework Division, Team Leader
ICQ# 165072909
> -----Original Message-----
> From: Andre Kirchner [mailto:supercroc1974@yahoo.com]
> Sent: Thursday, April 22, 2004 6:27 PM
> To: gcc-help@gcc.gnu.org
> Subject: simple error woth templates
>
>
> Hi,
>
> I have made this program to learn templates, but gcc
> keeps says that there's an undefined reference to
> `Dog<int,long>::Dog[in-charge](int,int,long,log)` at
> the line marked
> bellow.
> Could someone show me what's wrong?
>
> Thanks in advance,
>
> Andre
>
>
> **** Dog.h *****
>
> #ifndef IOSTREAM
> #define IOSTREAM
> #include <iostream.h>
> #endif
>
> template < class CLASS_A, class CLASS_B >
> class Dog
> {
> public:
> Dog( CLASS_A firstNumber, CLASS_A secondNumber,
> CLASS_B thirdNumber, CLASS_B forthNumber );
> ~Dog(){};
> void showBigger();
>
> private:
> CLASS_A number_A;
> CLASS_B number_B;
> };
>
>
> *** Dog.cpp ***
>
> #ifndef DOG
> #define DOG
> #include "Dog.h"
> #endif
>
> template < class CLASS_A, class CLASS_B >
> Dog< CLASS_A, CLASS_B >::Dog( CLASS_A firstNumber,
> CLASS_A secondNumber, CLASS_B thirdNumber, CLASS_B
> forthNumber )
> {
> number_A = ( firstNumber>secondNumber
> )?firstNumber:secondNumber;
> number_B = ( thirdNumber>forthNumber
> )?thirdNumber:forthNumber;
> }
>
> template < class CLASS_A, class CLASS_B >
> void Dog< CLASS_A, CLASS_B >::showBigger()
> {
> cout << number_A << endl;
> cout << number_B << endl;
> }
>
>
> *** main.cpp ***
>
> #ifndef DOG
> #define DOG
> #include "Dog.h"
> #endif
>
> int main( int argc, char *argv[] )
> {
> Dog < int, long > theDog( 10, 20, 30l, 40l );
>
> // theDog.showBigger;
>
> return( 0 );
> }
>
> __________________________________
> Do you Yahoo!?
> Yahoo! Tax Center - File online by April 15th
> http://taxes.yahoo.com/filing.html
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2004-04-22 17:42 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-22 14:27 simple error woth templates Andre Kirchner
2004-04-22 14:32 ` Eljay Love-Jensen
2004-04-22 17:42 ` llewelly
2004-04-22 14:51 Lev Assinovsky
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).