Public Group

This topic is 2563 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi guys,
This linking error is driving me nuts, can someone help me out?
The program is relatively simple, the problem comes down to the code below.
MazeBuilder is a base class with a protected constructor because I want it to be abstract.
If I remove the StandardMazeBuilder constructor (and therefore use the default one, the code compiles fine without errors)

1>------ Build started: Project: Builder, Configuration: Debug Win32 ------ 1> StandardMazeBuilder.cpp 1>StandardMazeBuilder.obj : error LNK2019: unresolved external symbol "protected: __thiscall MazeBuilder::MazeBuilder(void)" (??0MazeBuilder@@IAE@XZ) referenced in function "public: __thiscall StandardMazeBuilder::StandardMazeBuilder(void)" (??0StandardMazeBuilder@@QAE@XZ) 1>C:\Dev\Projects\Design Patterns\Builder\Debug\Builder.exe : fatal error LNK1120: 1 unresolved externals ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

 // //MazeBuilder.h // #pragma once #include "Maze.h" class MazeBuilder { public: virtual void BuildMaze(); virtual void BuildRoom(); virtual void BuildDoor(); protected: MazeBuilder(); private: }; // //StandardMazeBuilder.h // #pragma once #include "Maze.h" #include "MazeBuilder.h" class StandardMazeBuilder : public MazeBuilder { public: StandardMazeBuilder(); void BuildMaze(); void BuildRoom(); void BuildDoor(); private: Maze* m_currentMaze; }; // // Maze.h and main are basicly empty // 

##### Share on other sites
Small update, here is the code to StandardMazeBuilder.cpp, its basicly empty as well but the thing is that if I comment out the constructor (but leave the StandardMazeBuilder.h file the same)
the code compiles.
So what is wrong about the contructor?

 //StanddardMazeBuilder.cpp #include "StandardMazeBuilder.h" StandardMazeBuilder::StandardMazeBuilder() { } void StandardMazeBuilder::BuildMaze() { } void StandardMazeBuilder::BuildRoom() { } void StandardMazeBuilder::BuildDoor() { } 

##### Share on other sites
The linker is complaining that it can't find the code for the MazeBuilder constructor. You have probably forgot to add it somewhere.

##### Share on other sites

The linker is complaining that it can't find the code for the MazeBuilder constructor. You have probably forgot to add it somewhere.

I find that hard to believe, the "#include "MazeBuilder.h"" is in all the headers.
Besides, why would it than suddenly work when I remove the StandardMazeBuilder constructor?

##### Share on other sites
Your linker error is a missing function definition: the part with the curly braces that specifies line by line what the function does. #includes don't provide function definitions, they provide function declarations (unless the function definition is inlined and placed in the header). If it links when you don't have the StandardMazeBuilder constructor that just means that nowhere else in your code is the MazeBuilder constructor used.

##### Share on other sites
I see, so when I start using an empty constructor for StandardMazeBuilder, rather than the default constructor, the MazeBuilder constructor is being called and the linker cannot find the definition of this constructor, am I right?
So than the mistake I made is not defining a code body for the protected MazeBuilder constructor? I tought it was not nessacary to define one, shouldn't the compiler do this automaticly? (default constructor).

Anyway, when I change the MazeBuilder.h file and write "MazeBuilder() {}" instead of "MazeBuilder();" it still doesn't work, so what should I do to make it work? :S

##### Share on other sites
Ah nevermind, actually that was the problem, but when I added the curly braces the linker started complaining about the other functions with no code body as well. After I added the empty braces there too it works. Thanks both for the help!

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 9
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633679
• Total Posts
3013300
×