Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 22nd, 2006, 8:38 PM   #1
MicahCarrick
Newbie
 
Join Date: Jan 2006
Posts: 1
Rep Power: 0 MicahCarrick is on a distinguished road
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
}
MicahCarrick is offline   Reply With Quote
Old Jan 22nd, 2006, 9:48 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
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?
The Dark is offline   Reply With Quote
Old Jan 23rd, 2006, 5:33 AM   #3
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,208
Rep Power: 5 grumpy is on a distinguished road
Within your class declaration, the constructor
       // copy construction
        Triple(const Triple& t);
should be specified as;
       // copy construction
        Triple(const Triple<T1,T2,T3> & t);
Or (better yet) don't declare one at all, as the copy constructor that the compiler will generate for you by default will work correctly in this case.

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.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:58 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC