Thread: MSVC Template Constructor/Assignment Errors

  1. #1
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856

    Question MSVC Template Constructor/Assignment Errors

    Okay, MSVC6 is making me nuts because it errors for reasons I am unable to determine. I doubt there is a problem with my code because it compiles cleanly with Borland and the it is quite straightforward...

    Here is exactly what I'm compiling with the errors listed on their corresponding lines. Any ideas?
    Code:
    Test.h
    #include <iostream>
    using namespace std;
    
    class Test {
    public:
      Test();
    
      Test(const Test &);
    
      template <typename T>
        Test(const T &);
    
    
      Test& operator=(const Test &);
    
      template <typename T>
        Test& operator=(const T &);
    };
    Code:
    Test.cpp
    #include <iostream>
    #include <string>
    #include "Trash.h"
    using namespace std;
    
    
    Test foo() {
      Test t;
      t = string("foo");
      return t;// 'Test::Test' : ambiguous call to overloaded function
               // no legal conversion of return value to return type 'class Test *'
    }
    
    Test foo2() {
      Test t;
      t = foo();// 'operator =' is ambiguous
      return t;// 'Test::Test' : ambiguous call to overloaded function
                // no legal conversion of return value to return type 'class Test *'
    
    }
    
    
    int main() {
      string str = "BLAH";
    
      Test t(str);
    
      Test t2;
      t2 = str;
      Test t3 = foo();// 'initializing' : cannot convert from 'class Test' to 'class Test'
                      //       No copy constructor available for class 'Test'
    
      return 0;
    }
    
    
    Test::Test() {
    }
    
    template <typename T>
    Test::Test(const T &) {
    }
    
    Test::Test(const Test &) {
    }
    
    template <typename T>
    Test& Test::operator=(const T &) {
      return *this;
    }
    
    Test& Test::operator=(const Test &) {
      return *this;
    }
    Last edited by LuckY; 07-22-2005 at 02:42 PM.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    What happens if you move the templated funcs to the .h rather than the .cpp??
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Well, I've fixed that problem by making the non-template constructor and assignment operator templates (just adding "template <typename T>" to them), but I've got other related problems I'll have to try to figure out...

    [edit]
    As I am beginning to understand, Microsoft is seemingly much more uninterested in adhering to standards than I previously imagined.

    Boost's listing of MSVC errors
    Microsoft's list of deficiencies in Visual C++ 6
    Last edited by LuckY; 07-22-2005 at 03:30 PM.

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Quote Originally Posted by Stoned_Coder
    What happens if you move the templated funcs to the .h rather than the .cpp??
    Actually, implementing the functions in the .h results in several more errors. In any case, template functions need not be defined in the header if they do not belong to a non-template class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 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. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  4. Compiling using g++ and getting errors...
    By alvifarooq in forum C++ Programming
    Replies: 2
    Last Post: 09-24-2004, 08:36 AM
  5. MSVC 6 SP5 template errors
    By jdinger in forum C++ Programming
    Replies: 2
    Last Post: 04-12-2003, 09:59 AM