simple template question [ newbie ]

S

sieg1974

Hi,

I have make this program and the compliler keeps showing an error
message saying 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


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 numberA;
CLASS_B numberB;
};

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;
}

int main( int argc, char *argv[] )
{
Dog < int, long > theDog( 10, 20, 30l, 40l ); <-- *** the error is
here ***

// theDog.showBigger;

return( 0 );
}
 
V

Victor Bazarov

sieg1974 said:
I have make this program and the compliler keeps showing an error
message saying 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


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 numberA;
CLASS_B numberB;
};

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;
}

int main( int argc, char *argv[] )
{
Dog < int, long > theDog( 10, 20, 30l, 40l ); <-- *** the error is
here ***

// theDog.showBigger;

return( 0 );
}

Your code contains several errors. First, there are no members number_A
and number_B in the template Dog. Second, 'cout' and 'endl' are undefined.
Once those are corrected it compiles without a problem.

Could it be you're using some kind of compiler so old it doesn't understand
templates?

Victor
 
V

velthuijsen

#include said:
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 numberA;
CLASS_B numberB;
CLASS_A number_A;
CLASS_B number_B;
};

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;

personal opinion: please use () around the expression to be evaluated
most of the operators have a higher precendence then ?: but not all.
}

template < class CLASS_A, class CLASS_B >
void Dog< CLASS_A, CLASS_B >::showBigger()
{
cout << number_A << endl;
cout << number_B << endl;
std::cout << number_A << std::endl;
std::cout << number_B << std::endl;
}

int main( int argc, char *argv[] )
{
Dog < int, long > theDog( 10, 20, 30l, 40l ); <-- *** the error is
here ***

// theDog.showBigger; theDog.showBigger();

return( 0 );
}

Program compiles for me after changes.
My guess is that you have a dog.h and a dog.cpp, move the entire
dog.cpp into the dog.h and it should compile.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top