Thread: Plus and equal operators

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    49

    Unhappy Plus and equal operators

    I wrote two operators

    Code:
    template <class T>
    Vector<T>& operator+( const Vector<T>& vec, const T& value )
    {
    	Vector<T> tempvec(vec.length()); //create temp vector
    	for(int counter=0; counter<vec.length(); counter++)
    		tempvec.SetSpecificValue(counter, value+vec.GetSpecificValue(counter));
    	return tempvec;
    }
    
    template <class T>
    Vector<T>& Vector<T>::operator=( const T& value )
    {
    	for(int counter=0; counter<capacity; counter++)
    	{
    		array[counter] = value;
    	}
    	return *this;
    }
    I know that the equal operator is working because
    Code:
    vec4//already exists
    Vector<int> vec6(vec4.length());
    vec6.print();
    vec6 = vec4;
    vec6.print();
    Is working!

    But the following does not

    Code:
    vec6 = vec4 + 5;
    vec6.print();
    Any ideas???
    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    tempvec is a local variable, but you are returning it by reference. You should return it by value instead.
    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
    Dec 2007
    Posts
    49
    Quote Originally Posted by laserlight View Post
    tempvec is a local variable, but you are returning it by reference. You should return it by value instead.
    Still not working...Help? :-(

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How does it not work? You could provide the smallest and simplest compilable program that demonstrates the error.
    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

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    49
    Quote Originally Posted by laserlight View Post
    How does it not work? You could provide the smallest and simplest compilable program that demonstrates the error.
    OK...so far so good...still something small is wrong, but when I said it doens't work I was wrong. I did a stupid printing mistake so I couldn't see the change.

    Thank you very much! hopefully I'm done with this one :-)
    Last edited by misterowakka; 01-03-2008 at 12:20 PM.

Popular pages Recent additions subscribe to a feed