CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Aug 2009
    Posts
    440

    Template operator overloading assistance

    Hello,

    I am working on a template vector class. I have to have both a .h and .cpp file for this (already emailed teacher saying I that many people just put the implementation of template classes all in the same .h file). Anyway, I am running into an issue with overloading the operator =.

    MyVector.h
    Code:
    MyVector& operator=(const MyVector &rhs);
    MyVector.cpp
    Code:
    emplate <typename T>
    MyVector& MyVector<T>::operator=(const MyVector &rhs)
    {
        // Code here
    }
    The error I am getting is:

    Code:
    error: invalid use of template-name ‘MyVector’ without an argument list
    I've not done much template work, so I am not sure what exactly this is getting at. Thanks for any help.

    Edit:

    If in the .cpp I change it to:
    Code:
    template <typename T>
    MyVector<T>& MyVector<T>::operator=(const MyVector<T> &rhs)
    {
        // Code here
    }
    Everything seems to work. Can someone explain this to me? Is this the right approach? I assume I need to specify the template type at each stage to indicate that this particular use of the = operator is of type T.

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Template operator overloading assistance

    Yes, it looks like you fixed your own problem. If your teacher wants you to keep the implementation in a separate file, then I suggest naming this separate file with an extension like ".ipp" or "inl" instead of ".cpp" to avoid confusion.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Aug 2009
    Posts
    440

    Re: Template operator overloading assistance

    Thanks for the reply. I've run into a problem that I want to debug with Code::Blocks (as opposed to either using gedit and gdb or Emacs and gdb). I am able to compile my code fine in Fedora 15, but not when using Code::Blocks (using GCC compiler). I am assuming my problem might be because I am separating a template class into a .h and .cpp file.

    Anyway the structure of my code is as follows:

    MyVector.h
    Code:
    #ifndef MYVECTOR_H
    #define MYVECTOR_H
    
    template <typename T>
    class MyVector
    {
    	public:
    		MyVector();
                    // Remaining Class Declarations
    };
    
    #include "MyVector.cpp"
    
    #endif // MYVECTOR_H
    MyVector.cpp
    Code:
    #include "MyVector.h"
    
    template <typename T>
    MyVector<T>::MyVector()
    {
    	capacity 		= 10;
    	vect_front	 	= 0;
    	vectsize		= 0;
    	vector 	 	        = new T[capacity];
    }
    
    // Remaining functions
    The error messages I am getting are:
    Code:
    error: redefinition of 'MyVector<T>::MyVector()'
    error: 'MyVector<T>::MyVector()' previously declared here
    Any idea why I am getting the above error messages with Code::Blocks? Any idea why I am able to compile this under Fedora without running into these errors? If you need to entire code, let me know. Thanks for any assistance.
    Last edited by Alterah; September 3rd, 2011 at 07:51 PM.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Template operator overloading assistance

    You have a circular inclusion, so I am surprised that you got it "to compile this under Fedora without running into these errors". Remove the #include "MyVector.h" from MyVector.cpp. Remember, MyVector.cpp is not a source file. It is part of a header.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Aug 2009
    Posts
    440

    Re: Template operator overloading assistance

    Hmm...it's funny because this is how they want us to do templates. And, when I looked at it some more, it looked like circular inclusion. What I decided to do for debugging purposes is to just throw everything in the header...at least for now. I find it a lot easier to debug with Code::Blocks (or Visual Studio) than with the command line and gdb.

    Finally, when I remove the :

    #include "MyVector.h"

    from my .cpp file, leaving it like so:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <algorithm>
    
    template <typename T>
    MyVector<T>::MyVector()
    {
    	capacity 		= 10;
    	vect_front	 	= 0;
    	vectsize		= 0;
    	vector 	 		= new T[capacity];
    
    	popfrontcount   = 0;
    }
    // Remaining code
    I get the following error:
    Code:
    error: expected constructor, destructor, or type conversion before '<' token
    For line 6 (and similar messages for the other functions).

    Thanks again for your help.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Template operator overloading assistance

    I want you to copy the contents of this .cpp file and paste it into the header file, then delete this .cpp file. You should get the exact same error (other than say, a difference in line number). If you did not, then obviously you treated this .cpp file as a source file by compiling it when I explicitly told you that it was not a source file, but rather part of a header file. This is also why I suggested that you use a file extension other than .cpp for it.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Aug 2009
    Posts
    440

    Re: Template operator overloading assistance

    That was it. If I copy the contents of the implementation file to the header file, I don't get the error.

    But, leaving the implementation in the MyVector.cpp, I had added the MyVector.cpp file to the project, so it was trying to compile it as a source file. After reading your comment, I removed the MyVector.cpp file from the project and recompiled everything and everything works.

    Just for completion and to possibly answer your statement:

    "...so I am surprised that you got it 'to compile this under Fedora without running into these errors'."

    see my makefile that I use when I build this on the Fedora 15 system:

    Code:
    main1: main1.o
    	g++ -o main1 main1.o
    
    main1.o: main1.cpp MyVector.h
    	g++ -c -g main1.cpp
    
    MyVector.o: MyVector.cpp MyVector.cpp
    	g++ -c -g MyVector.cpp
    
    clean:
    	rm -f *.o *~main1
    I am a bit new when it comes to actually making makefiles...and I feel like I've made a mistake when it comes to the above instructions for MyVector.o...but it appears to compile and link everything together.

    Anyway, thanks a ton for your help.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Template operator overloading assistance

    Quote Originally Posted by Alterah View Post
    see my makefile that I use when I build this on the Fedora 15 system:
    What does "main1.cpp" consist of? That is the all important part of what is happening. If your main() program doesn't instantiate any templates, then the linker will ignore your template code.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Aug 2009
    Posts
    440

    Re: Template operator overloading assistance

    Currently it consists of:

    Code:
    #include "MyVector.h"
    #include <iostream>
    
    int main()
    {
        MyVector<int> temp;
    
        for( int i = 0; i < 40; i++ )
        {
            temp.push_end(i);
        }
    
        //temp.pop_end();
        //temp.pop_front();
    
        //temp.insert(500, 10);
        for( int i = 0; i < 30; i++ )
        {
            temp.remove(1);
        }
    
        temp.print();
    
        return 0;
    }
    So, it does use my vector class. I've been lazy and not created test files, but just modifying my main1 code after I complete a function and running through each case of the code to see if my template class behaves as expected.
    Last edited by Alterah; September 4th, 2011 at 05:32 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured