-1

I am new to C++, so I am sorry if this a simple or obvious error. I have been reading a lot other questions and the documentation, but I haven't been able to find a solution yet.

I am trying to add a new class definition to a large existing project. Everything compiles perfectly fine without my additions. However, when I add my code below I get a LNK2019 error on the constructor method (and any additional method). I have noticed adding/referencing properties does not cause this linker error, only the methods. Below is the most simple example that produces the error:

Header:

namespace foo
{
  class bar_interface
  {
    public:

    //My code
    #ifndef Point_H
    #define Point_H
      class Point
      {
      public:
        Point(int x, int y);
      };
    #endif

    //existing code
    void onStartup();        
  }
}

Class:

//My code
#ifndef Point_H
#define Point_H
class foo:bar_interface::Point{
public:
    Point(int x, int y)
    {
    };
};
#endif

//existing code
void bar_interface::onStartup()
{
  foo::bar_interface::Point p( (int)8, (int)8 );
  //continue existing code
}

Error 1 error LNK2019: unresolved external symbol "public: __thiscall foo::bar_interface::Point::Point(int,int)" (??0Point@bar_interface@foo@@QAE@HH@Z) referenced in function "public: void __thiscall foo::bar_interface::onStartup(void)" (?onStartup@bar_interface@foo@@QAEXXZ)

I realize that probably do not need such explicit calls to Point or casting the numbers as ints, but I wanted to make sure I wasn't missing anything obvious (removing them doesnt change the error). I have tried moving the 'Point' class to its own file and defining it outside the 'bar_interface' but within the 'foo' namespace. Removing the #ifndef code creates a C2011 redefinition error. I am at a loss for how to proceed.

valiano
  • 16,433
  • 7
  • 64
  • 79
Sarah
  • 328
  • 1
  • 6
  • 15
  • 3
    Your program contains a One Definition Rule violation, by defining class `Point` twice, differently. To implement the constructor, simply write `foo::bar_interface::Point::Point(int x, int y) { ... }` – Igor Tandetnik Dec 26 '17 at 17:21
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – user0042 Dec 26 '17 at 17:24
  • Don't define a second Point class. I mean `class Point` then `class foo:bar_interface::Point{` you are trying to define the class 2 times. Just implement `foo::bar_interface::Point::Point(int x, int y)` in your source file. – drescherjm Dec 26 '17 at 17:45
  • 1
    You have very strange ideas about how include guards work. – Retired Ninja Dec 26 '17 at 17:45
  • @IgorTandetnik this was exactly what was wrong. Thank you! – Sarah Dec 26 '17 at 18:19
  • @RetiredNinja - this was the only way I could get it to compile without the redefinition error. I thought defining the guard in the code and the header was odd, but I seemed to need both. With Igor's suggestion, I dont need the guard in the cpp anymore. – Sarah Dec 26 '17 at 18:21
  • You should not have an include guard in the middle of the definition of class bar_interface either. – drescherjm Dec 26 '17 at 18:32
  • @drescherjm - The proximity was just because I had been copying/pasting the code between other places/files. It wasnt intentionally there outside of convenience in testing. – Sarah Dec 26 '17 at 18:35

2 Answers2

0

Unresolved external means that the definition is missing, that is that the linker cannot find an implementation of the named function.

Somewhere you need:

namespace foo
{
  bar_interface::Point::Point(int,int)
  { ... }
}
SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • I have tried adding the full path to the constructor foo:bar_interface::Point::Point(int x, int y) in the header and the cpp, but the error is still the same. Is this what you meant? – Sarah Dec 26 '17 at 17:38
0

Remove all your lines from the code above that start from # and the reason of the issue becomes cleaner.

273K
  • 29,503
  • 10
  • 41
  • 64
  • I am not sure exactly what you mean. Other than a few #include and using statements at the top, this is verbatim the beginning of both files (outside changing the namespace/class names). Do you mean removing the includes, or are you suggesting I move the Point definition to the top of the file? – Sarah Dec 26 '17 at 17:42
  • No, do not remove all lines with #. Remove those lines that you posted only. – 273K Dec 26 '17 at 17:44