0

EDIT: I know there are similar questions, but I cannot find an answer to a following issue: Why the methods inside the class are working correctly and outside are not.

I've got a weird problem in my project which I'm developing in MSVC++ 2012. My project consists of different modules of code. The important modules from the problem's point of view is a library and the GUI. They exist as different projects in the same solution. I have some methods in the library which are part of the classes (in this case Calibration3D):

void Calibration3D::load(const std::string &path)

I use it without problems when I need it in the GUI, however I need to use a following method (outside the class):

void xxxyyy() 

But when I'm trying to use that function (outside the class but in the same namespace) I get a following error:

1>project_xml.obj : error LNK2001: unresolved external symbol "void __cdecl   cci::xxxyyy(void)" (?xxxyyy@cci@@YAXXZ) 1>D:\praca_pw\cci\build-msvc2012\x64\Release\\ccigui.exe : fatal error LNK1120: 1 unresolved externals

Anybody knows how to solve it?

Paweł Jastrzębski
  • 747
  • 1
  • 6
  • 24
  • 1
    possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – πάντα ῥεῖ Jul 17 '14 at 11:46
  • I've read the other topic and it there is plenty of theoretical deliberations that I know about. The problem is that the metods inside the class are working and outside are not! – Paweł Jastrzębski Jul 17 '14 at 12:06

2 Answers2

2

When I have a header file like this:

namespace xyz {
  void foo();
  class bar { ... };
}

then I write the cpp file like this:

#include "xyz.h"
namespace xyz {
  void foo() { ... }
  bar::bar() { ... }
}

This means I have to type a lot less and make fewer mistakes with regard to namespaces.

quamrana
  • 37,849
  • 12
  • 53
  • 71
0

OK, solved, it seems that when a method is defined inside the namespace in header file, it should also be defined explicitly as part of namespace in implementation file, in this case:

cci::xxxyyy() 
{ 
... 
} 

will work and

xxxyyy() 
{ 
...
} 

will not.

Paweł Jastrzębski
  • 747
  • 1
  • 6
  • 24
  • 1
    Yes, when defining the function you have to make sure it is defined in the same namespace as it was declared (otherwise it is two different functions). You could either do `void cci::xxxyyyy() { `, or `namespace cci { void xxxyyyy() {` – M.M Jul 17 '14 at 12:22