Overloading operators in a template class

This is a discussion on Overloading operators in a template class within the C++ Programming forums, part of the General Programming Boards category; I am trying to overload various operators for a class I am creating. In this instance, I am trying the ...

  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 04: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 04:45 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Post the smallest and simplest program that you think should compile but which demonstrates the compile error that you are currently facing.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,344
    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%.

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, 12: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21