Thread: strange linking error with operator overloading

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    strange linking error with operator overloading

    Hello everyone,


    I am meeting with a strange linking error regarding to operator overloading. I am creating a C++ static lib, and then linking it with a C application. Here is the error message and source codes,

    I am using adapter.cpp, adapter.h, foo.cpp and foo.h to create a static lib called foo.lib, then link it with a C application (goo.c). There is no problem to generate the static lib foo.lib.

    Any comments to solve this link error?

    Code:
    error LNK2019: unresolved external symbol "private: class FooNameSpace::Foo __thiscall FooNameSpace::Foo::operator=(long)" (??4Foo@FooNameSpace@@AAE?AV01@J@Z) referenced in function "private: __thiscall FooNameSpace::Foo::Foo(
    long)" (??0Foo@FooNameSpace@@AAE@J@Z)
    
    fatal error LNK1120: 1 unresolved externals

    adapter.cpp

    Code:
    #include "adapter.h"
    #include "foo.h"
    
    using namespace FooNameSpace;
    
    void compute(void)
    {
    	Goo g;
    	g.computeFoo();
    }
    adapter.h

    Code:
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    void compute (void);
    
    #ifdef __cplusplus
    }
    #endif
    foo.cpp

    Code:
    #include "foo.h"
    
    using namespace FooNameSpace;
    
    Foo Foo::operator=(long value)
    {
      this -> value = value;
      return (*this);
    }
    foo.h

    Code:
    namespace FooNameSpace
    {
    	class Foo
    	{
    
    	private:
    		long value;
      
    		 Foo (long value) { (*this) = value; }
    		 inline Foo operator= (long value);
    		 friend class Goo;
      };
    
    	class Goo
    	{
    	public:
    		void computeFoo()
    		{
    			Foo foo = Foo(0L);
    		}
      };
    }
    goo.c (link with foo.lib)

    Code:
    #include "adapter.h"
    #include "windows.h"
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        LPWSTR lpCmdLine, int nCmdShow)
    {
    	compute();
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You either call the using directive in foo.cpp or use the scope operator on the operator overload. You cant do both.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by Mario F.
    You either call the using directive in foo.cpp or use the scope operator on the operator overload. You cant do both.
    Huh? One is a namespace, the other is the class name.
    Code:
    /*myn.h */
    #ifndef MY_NAMESPACE_H
    #define MY_NAMESPACE_H
    
    namespace MY {
       class foo {
         public:
            void bar();
       };
    }
    
    #endif
    Code:
    /*imp.cpp */
    #include <iostream>
    #include "myn.h"
    
    using namespace MY;
    
    void foo::bar() // Nothing wrong here
    {
         return;
    } // you could even requalify it multiple times MY::foo::foo::foo::bar()
    // It doesn't matter
    To the OP, I think your problem is with your extern "C". Try taking it out and see what happens.
    Last edited by SlyMaelstrom; 07-03-2006 at 07:01 AM.
    Sent from my iPadŽ

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Actually you are right. I skimmed through the source code. Bad advice.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hello Mario,


    Quote Originally Posted by Mario F.
    You either call the using directive in foo.cpp or use the scope operator on the operator overload. You cant do both.
    I am confused. What do you mean "the using directive in foo.cpp" and "use the scope operator"?


    regards,
    George

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi SlyMaelstrom,


    Quote Originally Posted by SlyMaelstrom
    Huh? One is a namespace, the other is the class name.
    Code:
    /*myn.h */
    #ifndef MY_NAMESPACE_H
    #define MY_NAMESPACE_H
    
    namespace MY {
       class foo {
         public:
            void bar();
       };
    }
    
    #endif
    Code:
    /*imp.cpp */
    #include <iostream>
    #include "myn.h"
    
    using namespace MY;
    
    void foo::bar() // Nothing wrong here
    {
         return;
    } // you could even requalify it multiple times MY::foo::foo::foo::bar()
    // It doesn't matter
    To the OP, I think your problem is with your extern "C". Try taking it out and see what happens.
    I have not seen any relationship between your answer and my question.

    1. Do you mean I use the same name for a namespace and for a class? As you can see, I am using FooNameSpace as the namespace name and Foo as the class name -- there are no duplicates.

    2. I can not remove extern "C" since I am linking foo.cpp, adapter.cpp, adapter.h and foo.h (which will generate a static lib called foo.lib) to a C application (goo.c). If I remove extern "C", C application (goo.c) can not work with C++'s name mangling.


    regards,
    George

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    No george. It was a mistake I made based on the linker error and on the fact I just skimmed through your code. My advice was partially wrong.

    Anyways, the problem here is that you have your operator declared as inline. Either remove the inline specifier or move the operator definition to the header file.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by George2
    Hi SlyMaelstrom,




    I have not seen any relationship between your answer and my question.

    1. Do you mean I use the same name for a namespace and for a class? As you can see, I am using FooNameSpace as the namespace name and Foo as the class name -- there are no duplicates.

    2. I can not remove extern "C" since I am linking foo.cpp, adapter.cpp, adapter.h and foo.h (which will generate a static lib called foo.lib) to a C application (goo.c). If I remove extern "C", C application (goo.c) can not work with C++'s name mangling.


    regards,
    George
    No, the whole namespace issue was not in regards to you, it was in regards to the reply you recieved. I understand that you need to use extern "C", but the issue is that I don't think the C program your linking with likes your overloaded assignment operator because it's not understanding it. I could be wrong, but if I had to guess, the problem is the combination of trying to link with C source and overloading an operator. I would suggest instead of overloading your assignment operator, that you just create a member function to do the same thing. See if that helps at all.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems linking with g++
    By Just in forum Linux Programming
    Replies: 11
    Last Post: 07-24-2006, 01:35 AM
  2. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 10:51 PM
  3. Replies: 8
    Last Post: 04-27-2006, 10:39 AM
  4. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  5. Grrr.... SDL Linking Problem
    By 7EVEN in forum Game Programming
    Replies: 5
    Last Post: 08-12-2005, 08:44 PM