Member function already defined in .obj (visual studio) - c++

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.

Related

Syntax error in c++ default-int [duplicate]

This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 4 years ago.
So I just began programming in c++ and I am planning to make a small game as my first project (with the SDL library).
So I have this really small piece of code in a header file that gives me an error that I can not solve.
main.h
#include "Screen.h"
#include "MainGame.h"
Screen* screen = nullptr; //main.h line 8
Screen.h
#pragma once
#include "SDL.h"
#include <string>
#include <stdio.h>
#include <iostream>
#include "GameState.h"
#include "Game.h"
using namespace std;
class Screen
{
public:
Screen(string name, int width, int height, GameState* state);
~Screen();
SDL_Window *window;
SDL_Surface *screen;
SDL_Renderer *renderer;
};
MainGame.h
#pragma once
#include "GameState.h"
#include <stdio.h>
class MainGame :
public GameState
{
public:
MainGame();
~MainGame();
void start();
void update();
void render();
void stop();
};
Game.h
#pragma once
#include "GameState.h"
#include "Screen.h"
#include "SDL.h"
#include "main.h"
class Game
{
public:
GameState* activestate;
Game(GameState state);
~Game();
void changeState(GameState newState);
bool isRunning;
void handleEvents();
void update();
void render();
void stop();
};
GameState.h
#pragma once
class GameState
{
public:
GameState();
~GameState();
virtual void start();
virtual void update();
virtual void stop();
virtual void render();
};
And it gives this errors:
Error C2143 syntax error: missing ';' before '* main.h 8
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int main.h 8
What do these errors mean and how do I solve them?
The error comes from your circular dependency. Some file includes Screen.h. Screen.h includes Game.h. Game.h includes main.h. main.h needs Screen.h but because of pragma once it can't include it. So it doesn't know class Screen. Either remove circular dependency (better solution if possible, here it's possible):
Remove #include "main.h" in Game.h
or use forward declaration:
Write class Screen; in main.h:7

How do I run a function from a class file in C++?

I am trying to run a function from a class file, but it is not working and I get the the following error messages:
error LNK1120: 1 unresolved externals
error LNK2019: unresolved external symbol "public: void __thiscall NS::Class1::test(void)" (?test#Class1#NS##QAEXXZ) referenced in function _main
//Main.cpp
#include<iostream>
#include<string>
#include<Windows.h>
#include "Class1.h"
int main(){
NS::Class1 E;
E.test();
return 0;
};
//Class1.cpp
#include <Windows.h>
#include <string>
namespace NS{
class Class1{
Class1(){
OutputDebugString(L"Created.");
}
void test(){
OutputDebugString(L"Hello World");
}
};
}
//Class1.h
#ifndef _Class1_H_
#define _Class1_H_
namespace NS{
class Class1{
public:
void test();
};
}
#endif
In your source file, you're redefining the class, rather than defining its member functions. That will give undefined behaviour, since you're only allowed to define the class once. The source file should look more like:
#include "Class1.h"
#include <Windows.h>
NS::Class1::Class1(){
OutputDebugString(L"Created.");
}
void NS::Class1::test(){
OutputDebugString(L"Hello World");
}
You'll also need to modify the class definition in the header, since it doesn't declare a constructor.
Also, make sure that your project is compiling and linking both source files.
Your header file contains reserved IDs for the include guard and misses the declaration of the constructor. It should look like this:
#ifndef CLASS1_H
#define CLASS1_H
namespace NS {
class Class1
{
public:
Class1();
void test();
};
}
#endif
The definition of the class should not include the class declaration, it should include it, and should look more like this:
#include <Windows.h>
#include "Class1.h"
namespace NS {
Class1::Class1()
{
OutputDebugString(L"Created.");
}
void Class1::test()
{
OutputDebugString(L"Hello World");
}
}
I think your .cpp file might be causing the issue. Since you already created the class definition in your header with class member declarations, just import the Class1.h header in the Class1.cpp file and scope the member functions then defining their behavior.
so maybe try:
#include <Class1.h>
NS::Class1::Class1(){}//constructor
void NS::Class1::test(std::cout << "hi"){}

Compiler error C2653: not a class or namespace name

So I have been having this extremely frustrating problem lately with Visual C++ 2012. Up until a few hours ago, I was writing code just fine and everything was working as intended, until I decided to optimize some things and deleted a few classes. I fixed all of the errors that were popping up because of that, e.g. false includes, etc. Unfortunately, after this the VS compiler went crazy. It started giving me errors such as:
Error 14 error C2653: 'Class' : is not a class or namespace name
or even
Error 5 error C2143: syntax error : missing ';' before '}'
Error 4 error C2059: syntax error : '>'
I've checked multiple times, and everything is in it's right place: all headers included, all symbols placed where they should be.
As far as I understand, the problem is not with my code but with the compiler itself... Visual Studio can be really annoying at times, I guess. Anyway, I would really be grateful if someone could help me out on this one.
(By the way, disabling precompiled headers did not work)
Relevant parts of code:
Error 14:
#include "PlayerEntity.h"
PlayerEntity::PlayerEntity(void) {} // This line causes the error
Error 5:
class GameScreen : public BaseScreen
{
public:
...
private:
...
}; // This line causes the error
Error 4:
private:
std::vector<BaseEntity*> _EntityList; // This line causes the error
Whole PlayerEntity.h file:
#ifndef PENTITY_H
#define PENTITY_H
#include "BaseEntity.h"
class PlayerEntity : public BaseEntity
{
public:
PlayerEntity(void);
PlayerEntity(float, float);
virtual ~PlayerEntity(void);
void render(sf::RenderWindow&);
void update();
private:
void init();
};
#endif
Whole GameScreen.h file:
#ifndef GSCREEN_H
#define GSCREEN_H
#include "BaseScreen.h"
#include "BaseEntity.h"
#include "PlayerEntity.h"
class GameScreen : public BaseScreen
{
public:
GameScreen(sf::Vector2u&);
virtual ~GameScreen(void);
void start();
void stop();
void render(sf::RenderWindow&);
void update(void);
void addEntity(BaseEntity*);
void destoryEntity(int id);
private:
std::vector<BaseEntity*> _EntityList;
sf::Vector2u _ScreenDimensions;
};
#endif
Whole BaseEntity.h file:
#ifndef BSENTITY_H
#define BSENTITY_H
#include "Input.h"
#include <SFML/Graphics.hpp>
class BaseEntity
{
public:
BaseEntity(void);
virtual ~BaseEntity(void);
sf::Vector2f position;
virtual void update(void);
virtual void render(sf::RenderWindow&);
void compare(BaseEntity*);
protected:
sf::Texture *_EntityTexture;
sf::Sprite _EntitySprite;
bool _isAlive;
int _id;
virtual void init();
};
#endif
Whole Input.h file:
#ifndef INPUT_H
#define INPUT_H
#include "ScreenSystem.h"
#include <SFML/Window.hpp>
class Input
{
public:
Input(void);
Input(sf::RenderWindow*);
virtual ~Input(void);
static bool keyPressed(int);
static bool keyReleased(int);
static bool mouseHeld(int);
static bool mouseReleased(int);
private:
static sf::RenderWindow *_Window;
};
#endif
Whole ScreenSystem.h file:
#ifndef GHANDLER_H
#define GHANDLER_H
#include "BaseScreen.h"
#include "MenuScreen.h"
#include "GameScreen.h"
#include <SFML/Window.hpp>
class ScreenSystem
{
public:
ScreenSystem(void);
ScreenSystem(sf::RenderWindow*);
virtual ~ScreenSystem(void);
BaseScreen *getCurrentScreen(void);
void setScreen(int);
private:
int _currentScreenID;
std::vector<BaseScreen*> _Screens;
sf::RenderWindow *_Window;
};
#endif
You have a circular dependency in your headers. BaseEntity.h includes Input.h, which includes ScreenSystem.h, which includes GameScreen.h, which in turn re-includes BaseEntity.h. This leads to class names appearing before they are declared, causing compilation failure.
To avoid this, do not include headers unnecessarily. For example, do not include Input.h from BaseEntity.h, since it's not needed at all; and do not include BaseScreen.h from ScreenSystem.h since only a declaration class BaseScreen; is needed, not the complete class definition.
Also, check that you do not have duplicate header guards. Some of them do not match the header name (e.g. GHANDLER_H for ScreenSystem.h), which makes me think that they may have been accidentally copied from other headers. Finally, don't use reserved names like _EntitySprite for your own symbols; for simplicity, avoid leading or double underscores.
Did you copy the error messages into your question or did you retype them? Because error 14 has 'Class' with a capital C which is almost certainly not right.
Also, you should use as few include directives in your header files as possible. For example, GameScreen doesn't use PlayerEntity, so you can remove that include and BaseEntity is only used via pointer so you can replace
#include "BaseEntity.h"
with a forward declaration
class BaseEntity;

Function returns enum defined in another class (fatal link error)

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.

compiler error when declaring constructor function

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.