LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-22-2006, 08:27 PM   #1
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Rep: Reputation: 31
Homework Help: Copy Constructor on a template class.


I am creating a template class for a school assignment and am stuck on the copy constructor. I get the following error:

Code:
/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:

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
}
Can anyone id what I am doing wrong?
 
Old 01-22-2006, 10:33 PM   #2
carl.waldbieser
Member
 
Registered: Jun 2005
Location: Pennsylvania
Distribution: Kubuntu
Posts: 197

Rep: Reputation: 32
Quote:
Originally Posted by MicahCarrick
I am creating a template class for a school assignment and am stuck on the copy constructor. I get the following error:

Code:
/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:

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
}
Can anyone id what I am doing wrong?
Well, I didn't get exactly the same error you did, but I guess that could be a difference in compilers. For future reference, you might leave your #include lines in so the code compiles to the errors you want to demonstrate.

I think your problem is with the argument you are passing to the copy constructor:
Code:
Triple(const Triple& t);

ought to be

Triple(const Triple<const T1, const T2, const T3 >& t);
Needs to be changed in the definition, too. I think the compiler was just complaining about the const-correctness of the types.
 
Old 01-22-2006, 10:43 PM   #3
MicahCarrick
Member
 
Registered: Jul 2004
Distribution: Fedora
Posts: 241

Original Poster
Rep: Reputation: 31
Wow... that was it. Heh. Thanks so much!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copy constructor for class containing array of vaiable size objects ? qwijibow Programming 4 12-21-2005 09:50 PM
c++ class template doesn't work!! tagigogo Programming 2 11-02-2005 04:45 PM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 08:27 PM
template class ckcheung0927 Programming 3 11-28-2004 03:59 PM
Class constructor not being called ChimpFace9000 Programming 1 06-03-2002 08:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration