Thread: Overloading operators in a template class

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    Overloading operators in a template class

    I am trying to overload various operators for a class I am creating. In this instance, I am trying the assignment operator. I get the same error for all of them, so I will post just the assignment operator:
    // header
    Code:
    #include <cstdio>
    #include <iostream>
    
    namespace mine {
    	template<class type>
    	class vector
    	{
    	public:
    	        ...
    		vector& operator=(const vector& other);
    	        ...
    	};
    }
    and the implementation
    Code:
    #include "vector.h"
    #include <cstdio>
    #include <iostream>
    
    
    template<class type>
    vector& vector::vector<type>::operator=(const vector& other) {
    	if(&other != this) {
    		ptr = new type [other.n];
    		for (int i = 0; i < other.n; i++) {
    			ptr[i] = other.ptr[i];
    		}
    	}
    }
    I've tried various combinations of scoping on the implementation to see if it would do anything but I keep getting the:
    "Expected constructor, destructor, or type conversion before '&' token" error.
    I am running this on a Mac OSX Snow Leopard 10.6.2 with Xcode Version 3.2.1. Please Help!
    Last edited by meiskru; 11-22-2009 at 05:40 PM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Giving it a quick look, you would probably need
    Code:
    using namespace mine;
    or
    Code:
    mine::list::vector<type>::operator=(..)

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    2

    edited list

    Sorry, the list that was in the original post has been removed.
    I've tried:

    Code:
    mine::vector<type>::operator=(...)
    but I still receive the same error...
    Last edited by meiskru; 11-22-2009 at 05:45 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Post the smallest and simplest program that you think should compile but which demonstrates the compile error that you are currently facing.
    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
    Jun 2005
    Posts
    6,815
    Try
    Code:
    template<class type>
    mine::vector<type> &mine::vector<type>::operator=(const mine::vector<type> & other)
    // etc
    Probably better to do things in a manner that avoids the need for a self-assignment test too, but that's unrelated to your problem.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  2. template function v.s. template class
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2007, 01:46 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. overloading postfix/prefix operators for a class
    By jpipitone in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2003, 09:06 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

Tags for this Thread