Thread: Confused by asymmetric assignment operator templates

  1. #1
    Registered User
    Join Date
    Jan 2009

    Question Confused by asymmetric assignment operator templates


    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...

    -- 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
    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 05:28 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2009
    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, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM