![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Posts: 1
Rep Power: 0
![]() |
Help: Template Class' copy constructor
I am creating a template class for a school assignment and am stuck on the copy constructor. I get the following error:
/tmp/ccxWE2Mp.o(.text+0x3c7): In function `main': lab2.cc: undefined reference to `Triple<int, float, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::Triple(Triple<int, float, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&)' collect2: ld returned 1 exit status And this is my code: template<class T1, class T2, class T3>
class Triple
{
public:
// default construction
Triple() {}
// construction with initial values
Triple(T1 v1, T2 v2, T3 v3) : _value1(v1), _value2(v2), _value3(v3) { }
// copy construction
Triple(const Triple& t);
// accessors
T1 value1() { return _value1; }
T2 value2() { return _value2; }
T3 value3() { return _value3; }
// modifiers
void set_value1(T1 v) { _value1 = v; }
void set_value2(T2 v) { _value2 = v; }
void set_value3(T3 v) { _value3 = v; }
private:
T1 _value1;
T2 _value2;
T3 _value3;
};
// copy constructor
template< class T1, class T2, class T3 >
Triple< T1, T2, T3 >::Triple(const Triple< T1, T2, T3 >& t)
{
_value1 = t.value1();
_value2 = t.value2();
_value3 = t.value3();
}
int
main()
{
// TEST TEMPLATE CLASS
Triple<int, float, string> ifs_triple1;
// test modifiers
ifs_triple1.set_value1(5);
ifs_triple1.set_value2(5.34);
ifs_triple1.set_value3("Hello!");
// test copy constructor
Triple<int, float, string> ifs_triple2(ifs_triple1);
// print values (should be the same)
cout << "TEST TEMPLATE CLASS:" << endl;
cout << "ifs_triple1.value1()=" << ifs_triple1.value1();
cout << " ifs_triple1.value2()=" << ifs_triple1.value2();
cout << " ifs_triple1.value3()=" << ifs_triple1.value3();
cout << endl;
cout << "ifs_triple2.value1()=" << ifs_triple2.value1();
cout << " ifs_triple2.value2()=" << ifs_triple2.value2();
cout << " ifs_triple2.value3()=" << ifs_triple2.value3();
cout << endl;
return 0; // exit successfull
} |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
Works for me under Visual C 7 and gcc 3.4.4
I had to put const after the value getter function declarations to get it to compile properly. What compiler were you using? |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,208
Rep Power: 5
![]() |
Within your class declaration, the constructor
// copy construction
Triple(const Triple& t); // copy construction
Triple(const Triple<T1,T2,T3> & t);Unrelated to your problem: While it's not actually wrong to pass arguments of type T1, T2, and T3 by value, it will often be better to pass them via const reference. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|