Thread: Overloading operators in a template class

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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