Thread: operator+ for template class

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248

    operator+ for template class

    Hi everyone,

    When I try to overload the operator+ for a template class, the gnu g++-4.x compiler generates the following error message on the 'return' statement in the body of the operator definition:

    ../Vector.h:73: error: no matching function for call to ‘Vector<double>::Vector(Vector<double>&)’
    ../Vector.h: In constructor ‘Vector<T>::Vector(const size_t&) [with T = double]’:
    ../Vector.h:69: instantiated from ‘Vector<T> Vector<T> :: operator+(const Vector<T>&) [with T = double]’


    Voila the code:

    Code:
    template<typename T>
    class Vector
    {
     public:
      Vector( void);
      explicit Vector( Vector<T> const&);  // copy constructor
      explicit Vector( size_t const&); // zero value
    
      Vector<T> operator+( Vector<T> const& vec2)
      {
        Vector<T> result( vec2.size());
        for( int i=0; i<result.size(); i++) {
          result[i] = this->vec[i] + vec2[i];
        }
        return result; // line 73, which generates error message
      }
    
     private:
      size_t N;
      T* vec;
    
    };
    It searches for a kind of copy constructor; without the keyword 'explicit' it suggests the one-argument constructor as a candidate. But implementing this not-so-pretty copy constructor seems really awkward to me. Has it got something to do with all my 'const' declarations?

    I know that I do not need to specialise the return type of the operator because of argument deduction, but my goal is to use traits to determine (map) the return type. This is a small simplification to hunt down the bug.

    Thanks a lot in advance!!
    Last edited by MarkZWEERS; 05-07-2008 at 03:36 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you want the copy constructor to be explicit?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Quote Originally Posted by laserlight View Post
    Why do you want the copy constructor to be explicit?
    original reply:

    ehm... don't know. I always declare one-argument constructors or one-argument functions 'explicit', think it is just a bad habit. By the way, you're fast! tnx

    corrected reply:

    well I just remembered why I wanted to declare the copy constructor as 'explicit'. I want to create cast constructors as well (with the usage of traits), so to make the distinction between the copy constructor and the cast constructor...
    Last edited by MarkZWEERS; 05-07-2008 at 03:42 PM.

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Making the copy constructor "explicit" causes horrible breakage, as you discover... I can't think of a single sane reason why you'd want to.

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Quote Originally Posted by brewbuck View Post
    Making the copy constructor "explicit" causes horrible breakage, as you discover... I can't think of a single sane reason why you'd want to.
    Yes, the 'explicit' copy constructor caused the problem.

    But why exactly? Does the compiler try to cast implicitly the argument to a 'const' argument?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MarkZWEERS View Post
    Yes, the 'explicit' copy constructor caused the problem.

    But why exactly? Does the compiler try to cast implicitly the argument to a 'const' argument?
    No, the act of returning ANY sort of object will invoke its copy constructor in an implicit way. So making the copy constructor explicit breaks all kind of fundamental behaviors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  3. Defining derivated class problem
    By mikahell in forum C++ Programming
    Replies: 9
    Last Post: 08-22-2007, 02:46 PM
  4. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM