Jump to content
  • Advertisement
Sign in to follow this  
Toadhead

Linking Error [C++]

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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?
I'm realy confused about this.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!