Confused by asymmetric assignment operator templates

This is a discussion on Confused by asymmetric assignment operator templates within the C++ Programming forums, part of the General Programming Boards category; Hi, Does anyone have an insight on why the code below produces linker errors claiming that the assignment operator template ...

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

    Question Confused by asymmetric assignment operator templates

    Hi,

    Does anyone have an insight on why the code below produces linker errors claiming that the assignment operator template functions were not found ("LNK2019: unresolved external symbol", using MSVC 2008)? I assume I've messed something up, but can't figure it out myself...


    Code:
    -- a.h --
    class A
    {
    	template <class T> operator=(const T& value);
    };
    
    -- a.cpp --
    #include "a.h"
    #include <iostream>
    
    template <typename T>
    A& A::operator=(const T& value)
    {
    	std::cout << value << std::endl;
    	return *this;
    }
    
    -- main.cpp --
    #include "a.h"
    
    int main(int argc, char** argv)
    {
    	A a;
    
    	a = 1.0f;
    	a = "test";
    
    	return 0;
    }
    Any help or thoughts on the subject would be greatly appreciated

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    You might want to read this FAQ: Why can't I separate the definition of my templates class from it's declaration and put it inside a .cpp file?

    On a related, and arguably more relevant (since you have a function template) note: How can I avoid linker errors with my template functions?
    Last edited by laserlight; 01-12-2009 at 04:28 AM.
    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

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    You're absolutely right, that's what I wasn't taking in account for. Thank you very much for the links!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 08:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM

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