I'm working on my serious c++ program. It's been awhile since I've taken any classes, so I'm a little rusty. When starting the basic implementation of a game loop (After spending freaking FOREVER getting SFML to work), I continually ran into issues. After awhile, I've gotten my list of issues down to an error when trying to define a constructor function. I get the following error when trying to compile.
1>game.obj : error LNK2005: "public: __thiscall game::game(void)"
(??0game##QAE#XZ) already defined in main.obj
1>game.obj : error LNK2005: "public: void __thiscall
game::gameLoop(void)" (?gameLoop#game##QAEXXZ) already defined in
main.obj
My code in main is
#include <SFML/Graphics.hpp>
#include "game.cpp"
int main()
{
return 0;
}
in game.h it's
#ifndef _game_h
#define _game_h
class game
{
public:
game();
void gameLoop();
};
#endif
and in game.cpp it's
#include <iostream>
#include "game.h"
game::game()
{
std::cout << "Constructed thingie";
}
void game::gameLoop()
{
std::cout << "RAN LOOP!" << std::endl;
}
I don't know why I'm running into this error. Any help would be nice as I'd like to get started on my project.
You should include #include "game.h" in main, not game.cpp.
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
So I'm working on serialization (I have never done it before so this is a first for me) and what I have done is created a base class called serialisable that all my other classes that can serialize can inherent from:
#include <iostream>
class Serializable{
public:
Serializable();
virtual ~Serializable();
virtual void serialize();
virtual void deserialize();
};
I then have a class that inherits from it which is my AbstractChunk class:
#pragma once
#include <iostream>
#include <list>
#include <fstream>
#include "AbstractBlock.h"
#include "Serialisable.h"
using namespace std;
#ifndef ABSTRACTCHUNK_H
#define ABSTRACTCHUNK_H
class AbstractChunk: public Serializable{
public:
AbstractChunk();
AbstractChunk(int x, int y);
~AbstractChunk();
virtual int getXpos();
virtual int getYpos();
virtual bool unload();
void serialize();
void deserialize();
private:
list<AbstractBlock> blocks;
int xpos;
int ypos;
};
#endif
and then the .cpp for my AbstractChunk (I edited out all the non important stuff):
#include "AbstractChunk.h"
void AbstractChunk::serialize(){
ofstream chunkFile;
chunkFile.open("ChunkData/" + to_string(xpos) + "." + to_string(ypos) + ".chunk");
if (!chunkFile.good())
cout << "Problem Opening Chunk File" << xpos << "." << ypos << endl;
chunkFile << "xpos:" << xpos << "\n";
chunkFile << "ypos:" << ypos << "\n";
chunkFile.close();
}
void AbstractChunk::deserialize(){
}
So where is this error coming from? It's a linker error however I didn't mess with the dependencies or the project setup at all, I have a feeling I'm doing something stupid as usual.
EDIT
Here are the actual errors
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Serializable::Serializable(void)" (??0Serializable##QAE#XZ) referenced in function "public: __thiscall AbstractChunk::AbstractChunk(int,int)" (??0AbstractChunk##QAE#HH#Z) C:\Users\Magnus\Documents\Visual Studio 2013\Projects\Top Down Shooter\Top Down Shooter\AbstractChunk.obj Top Down Shooter
Error 2 error LNK2019: unresolved external symbol "public: virtual __thiscall Serializable::~Serializable(void)" (??1Serializable##UAE#XZ) referenced in function __unwindfunclet$??0AbstractChunk##QAE#HH#Z$0 C:\Users\Magnus\Documents\Visual Studio 2013\Projects\Top Down Shooter\Top Down Shooter\AbstractChunk.obj Top Down Shooter
You are not specifying the exact linking error but for sure you are missing some methods, you have declared:
class Serializable {
..
virtual void serialize();
virtual void deserialize();
}
as non pure virtual methods, and you are not implementing them. You should turn them to pure, since Serializable doesn't implement functionality but it's only an interface:
class Serializable {
..
virtual void serialize() = 0;
virtual void deserialize() = 0;
}
I've never had this problem in code blocks, but in visual studio classes are giving me error LNK2005 ... already defined in xx.obj. I've read many answers, most of which say to use 'extern' which I don't think is usable on member functions
Error: 1>Source.obj : error LNK2005: "public: __thiscall Game::Game(void)" (??0Game##QAE#XZ) already defined in game.obj
Error: fatal error LNK1169: one or more multiply defined symbols found
How can I fix this without brute forcing/allowing multiple definitions?
//game.h
#ifndef GAME_H
#define GAME_H
#include <SFML/Graphics.hpp>
class Game
{
public:
Game();
private:
sf::RenderWindow mWindow;
sf::CircleShape mPlayer;
};
#endif
.
//game.cpp
#include "..\Headers\game.h"
Game::Game()
: mWindow(sf::VideoMode(640, 480), "Beginning")
, mPlayer()
{
mPlayer.setRadius(40.f);
mPlayer.setPosition(100.f, 100.f);
mPlayer.setFillColor(sf::Color::Cyan);
}
.
//source.cpp
#include "game.cpp"
int main()
{
Game game;
}
Replace in your source.cpp
#include "game.cpp"
by
#include "game.h"
You should never include a *.cpp.
Please have a look at the following code
Main.cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
system("pause");
return 0;
}
Magic.h
#pragma once
class Magic
{
public:
Magic();
~Magic();
virtual void display()=0;
};
Spell.h
#pragma once
#include "Magic.h"
#include <iostream>
#include <string>
using namespace std;
class Spell :
public Magic
{
public:
Spell(void);
Spell(string words);
~Spell(void);
void display();
private:
string words;
};
Spell.cpp
#include "Spell.h"
#include "Magic.h"
#include <iostream>
#include <string>
using namespace std;
Spell::Spell(void)
{
}
Spell::Spell(string words)
{
this->words = words;
}
Spell::~Spell(void)
{
cout << "Delete Spell" << endl;
}
void Spell::display()
{
cout << "Spell Words: " << words << endl;
}
Here, I am getting the error
1>------ Build started: Project: Revision1_1, Configuration: Debug Win32 ------
1>Spell.obj : error LNK2019: unresolved external symbol "public: __thiscall Magic::~Magic(void)" (??1Magic##QAE#XZ) referenced in function __unwindfunclet$??0Spell##QAE#XZ$0
1>Spell.obj : error LNK2019: unresolved external symbol "public: __thiscall Magic::Magic(void)" (??0Magic##QAE#XZ) referenced in function "public: __thiscall Spell::Spell(void)" (??0Spell##QAE#XZ)
1>C:\Users\yohan\Documents\Visual Studio 2010\Projects\Revision1_1\Debug\Revision1_1.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I do not understand what to do here. Why is this happening? Please help! I am new to C++ anyway..
Magic is not implementing its constructor and destructor (which also should be virtual).
Don't even declare the constructor if not necessary, e.g.
class Magic {
public:
virtual ~Magic() {}
virtual void display() = 0;
};
Unrelated: I didn't know you can display magic.
You didn't implement
Magic();
~Magic();
You'll need to either implement them inline, in an implementation file, or mark them = default.
You have declared a destructor in your Magic class but did not define it. That's why the linker complains (and the compiler doesn't).
You don't have an implementation for Magic. If your intention is for Magic to be an abstract base class, then just change its declaration to:
#pragma once
class Magic
{
public:
virtual void display()=0;
};
Remember, any method that is not followed by = 0 in the interface must be implemented in the class.
I've recently started to program in C++ again, and for the purposes of education, I am working on creating a poker game. The weird part is, I keep getting the following error:
1>LearningLanguage01.obj : error LNK2019: unresolved external symbol "public: __thiscall PokerGame::Poker::Poker(void)" (??0Poker#PokerGame##QAE#XZ) referenced in function "void __cdecl `dynamic initializer for 'pokerGame''(void)" (??__EpokerGame##YAXXZ)
1>LearningLanguage01.obj : error LNK2019: unresolved external symbol "public: __thiscall PokerGame::Poker::~Poker(void)" (??1Poker#PokerGame##QAE#XZ) referenced in function "void __cdecl `dynamic atexit destructor for 'pokerGame''(void)" (??__FpokerGame##YAXXZ)
1>LearningLanguage01.obj : error LNK2019: unresolved external symbol "public: void __thiscall PokerGame::Poker::begin(void)" (?begin#Poker#PokerGame##QAEXXZ) referenced in function _wmain
1>C:\Visual Studio 2012\Projects\LearningLanguage01\Debug\LearningLanguage01.exe : fatal error LNK1120: 3 unresolved externals
I have done some research on the issue, and most point to the constructor and destructor definition in the header and .cpp not matching. I don't see any issues with the header and .cpp.
Here is the code for poker.h:
#pragma once
#include "Deck.h"
using namespace CardDeck;
namespace PokerGame
{
const int MAX_HAND_SIZE = 5;
struct HAND
{
public:
CARD cards[MAX_HAND_SIZE];
};
class Poker
{
public:
Poker(void);
~Poker(void);
HAND drawHand(int gameMode);
void begin();
};
}
And the code in the .cpp:
#include "stdafx.h"
#include "Poker.h"
using namespace PokerGame;
const int TEXAS_HOLDEM = 0;
const int FIVE_CARD = 1;
class Poker
{
private:
Deck deck;
Poker::Poker()
{
deck = Deck();
}
Poker::~Poker()
{
}
void Poker::begin()
{
deck.shuffle();
}
//Draws a hand of cards and returns it to the player
HAND Poker::drawHand(int gameMode)
{
HAND hand;
if(gameMode == TEXAS_HOLDEM)
{
for(int i = 0; i < sizeof(hand.cards); i++)
{
hand.cards[i] = deck.drawCard();
}
}
return hand;
}
};
Because of the comment below, I've rewritten what I had before.
The problem that the linker is complaining about is that you've declared your member functions in Poker, but haven't defined them. How is this? For starters, you're creating a new class and defining separate member functions in it.
Your header file Poker class exists in the PokerGame namespace and your cpp file Poker class exists in the global namespace. To fix that issue, put them in the same namespace:
//cpp file
namespace PokerGame {
class Poker {
...
};
}
Now that they're in the same namespace, you have another issue. You're defining your member functions inside the class body, but not the first one. The definitions simply can't go in the body of a class named the same way. Get rid of the whole class in the cpp file:
//cpp file
namespace PokerGame {
Poker::Poker() {
deck = Deck(); //consider a member initializer instead
}
//other definitions
}
One last thing: you put the private section of your class in the wrong spot. It was in that cpp file class that we just removed. It belongs with the other parts of your class:
//header file
namespace PokerGame {
class Poker {
public:
//public stuff
private:
Deck deck; //moved from cpp file
};
}
Another solution could be: check the cmake file and make sure it (such as in ADD_EXECUTABLE) includes the .cpp file you listed.
This seems like a problem that is common. I defined an enum in classA and then included classA in classB. Then, in classB I defined a function which returns the enum type defined in classA...see below. I get the following error:
aFirst.obj : error LNK2019: unresolved external symbol "public: enum justEnum::things_t __thiscall usesTheEnum::returnsThings(void)" (?returnsThings#usesTheEnum##QAE?AW4things_t#justEnum##XZ) referenced in function _wmain
1>C:\Documents and Settings\Ben\My Documents\Visual Studio 2010\Projects\aFirst\Debug\aFirst.exe : fatal error LNK1120: 1 unresolved externals
#pragma once
class justEnum
{
public:
justEnum(void);
~justEnum(void);
enum things_t{ONE, TWO};
};
#pragma once
#include "justEnum.h"
class usesTheEnum
{
public:
usesTheEnum(void);
~usesTheEnum(void);
justEnum::things_t returnsThings(void);
};
#include "StdAfx.h"
#include "usesTheEnum.h"
#include "justEnum.h"
usesTheEnum::usesTheEnum(void)
{
}
usesTheEnum::~usesTheEnum(void)
{
}
justEnum::things_t returnsThings()
{
return justEnum::ONE;
}
// tester.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include "justEnum.h"
#include "usesTheEnum.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
usesTheEnum aUser;
justEnum anEnum;
justEnum::things_t anotherEnum;
anotherEnum = justEnum::ONE;
aUser.returnsThings();
cout << anotherEnum;
return 0;
}
You need to specify that your definition of returnsThings() is part of the usesTheEnum class.
justEnum::things_t usesTheEnum::returnsThings()
{
return justEnum::ONE;
}
error LNK2019: unresolved external symbol "public: enum justEnum::things_t __thiscall usesTheEnum::returnsThings(void)"
The compiler is complaining that usesTheEnum::returnThings() is not defined, and I cannot see a definition in the code you posted. You should provide a definition for the function in one translation unit.
I don't think I can emphasize enough how important it to learn to read error messages. The compiler is doing it's best to tell you what is wrong.
not shure, but can't you just move the enum out of the class?
Or on the .cpp of the class write something like
extern enum classname::things_t;
just to have the enum added to the generated lib wich is what will be linked against.