2

when i was trying to seperate the declaration and implementation of a non-member overloaded operator, i got a LNK2001 error in VC2010, my code was like this:

-foo.h-

class A
{
public:
    A(float x);
    float x;
};
A operator +(const A&, const A&);

-foo.cpp-

A::A(float x)
{
    this->x = x;
}

A operator +(const A& lh, const A& rh)
{
    return A(lh.x + rh.x);
}

so once i use the '+' operation, the error pumps out, but if i remove the declaration in the header file, there are no LNK2001 errors.. i cannot figure out why..

3
  • Are you using any namespaces? Commented Nov 8, 2010 at 3:57
  • yes, i used a my own defined namespace Commented Nov 8, 2010 at 4:01
  • next time give the exact error message. But the namespace is creating the problem. See my answer. Commented Nov 8, 2010 at 4:02

3 Answers 3

5

I suspect you have the definition in a different namespace than the declaration. ADL is finding the declaration (since it's in the same namespace as the class), and then you get an unresolved external error during link.

e.g.

-foo.h-

namespace aspace
{
  class A
  {
  public:
      A(float x);
      float x;
  };
  A operator +(const A&, const A&);
}

-foo.cpp-

#include "foo.h"
using namespace aspace;

A::A(float x)
{
    this->x = x;
}

A operator +(const A& lh, const A& rh)
{
    return A(lh.x + rh.x);
}

Will give the error you describe. The solution is to put the operator+ definition in the correct namespace:

namespace aspace
{
  A operator +(const A& lh, const A& rh)
  {
      return A(lh.x + rh.x);
  }
}
Sign up to request clarification or add additional context in comments.

7 Comments

hi Ben, this is exactly as you said, i got it right now, thank you very much!!
@Alan: Since you're new to this site, you may not have noticed that just to the left of my answer is a gray outline of a checkmark. If you click that it will make my answer "accepted" which gives me points and lets anyone else with this question know that it is the right answer. You should also "accept" the correct answer on your other question.
@Alan: as a general rule, banish using namespace from your toolbox. I know many books / tutorials use it, but it brings more trouble than it's worth. Consider namespace aliasing instead if it's really too unwieldy.
@Matthieu: using namespace is big trouble inside header files, but is perfectly alright in implementation files and inside functions.
@Ben: you just answered a question where it posed an issue :) I don't see much gain with this directive, you can either use namespace aliasing or using a function.
|
0

I can't see anything wrong with the code you have presented.

Are you sure that this is exactly the code you have?

If it is, then it seems the only explanation is that you have somehow managed to not have [foo.cpp] in your Visual Studio project, assuming you're using Visual Studio.

If using command line tools, the corresponding thing would be not including [foo.obj].

If those comments don't help, can you create a single file small program that exhibits the problem?

Cheers,

Comments

0

Change your prototype to (if you insist on keeping the definition of operator+ in the enclosing namespace for whatever reason)

A aspace::operator +(const A& lh, const A& rh) 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.