Class in Header file with no implementation

I created a class in Visual studio 2010 in a header file.
.h file:
1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once
#include <SFML/Graphics.hpp>

class Projectile
{
public:
	sf::Sprite sprite;

	Projectile(void);
	Projectile(sf::Texture txt, sf::Vector2f pos);
	~Projectile(void);
};

.cpp file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include "Projectile.h"

Projectile::Projectile(void)
{
}

Projectile::Projectile(sf::Texture txt, sf::Vector2f pos)
{
	sprite.setTexture(txt);
	sprite.setPosition(pos);
}

Projectile::~Projectile(void)
{
}


I include the .h file in my main but when I try to use any of the methods of the class they are not implemented meaning they don't do anything. Any suggestions?
I include the .h file in my main but when I try to use any of the methods of the class they are not implemented meaning they don't do anything.


Do you get a linker error or what?
I'm assuming your .h is named projectile.h and your .cpp is named projectile.cpp. In your program's .cpp, usually main.cpp, you have #include "projectile.h" but aren't linking correctly to the projectile.cpp. This can happen if you're using a project and have forgotten to add the projectile.cpp file to the project.

Two solutions are possible:
1) Add projectile.cpp to the project so it's included in the linking (possibly wrong term)
or 2) add this piece of code to the bottom of your projectile.h file:
#include "projectile.cpp"

This should correct your issue. I don't have a lot of experience with MSVS software though so you might need to add some more things in there. You should also research header guards to make sure you don't have multiple declarations/definitions.

I include the .h file in my main but when I try to use any of the methods of the class they are not implemented meaning they don't do anything.


Do you get a linker error or what?


No, it compiles correctly but the methods do nothing when I execute it.

projectile.cpp and .h are in the source and header folders in my solution explorer.

Visual studio uses "#pragma once" as a header guard ( I think).

When I include the .cpp in the main I get compiler errors:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Error	4	error LNK1169: one or more multiply defined symbols found	
C:\Users\Flame\Documents\server_app\Chess\Debug\Chess.exe	1	1	Chess

Error	3	error LNK2005: "public: __thiscall Projectile::~Projectile(void)" (??
1Projectile@@QAE@XZ) already defined in Main.obj	
C:\Users\Flame\Documents\server_app\Chess\Chess\Projectile.obj	Chess

Error	2	error LNK2005: "public: __thiscall Projectile::Projectile(class sf::Texture,class 
sf::Vector2<float>)" (??0Projectile@@QAE@VTexture@sf@@V?$Vector2@M@2@@Z) already 
defined in Main.obj	C:\Users\Flame\Documents\server_app\Chess\Chess\Projectile.obj	
Chess

Error	1	error LNK2005: "public: __thiscall Projectile::Projectile(void)" (??
0Projectile@@QAE@XZ) already defined in Main.obj	
C:\Users\Flame\Documents\server_app\Chess\Chess\Projectile.obj	Chess


Last edited on
When I include the .cpp in the main I get compiler errors:

Do not include the cpp in the main. You only need to make sure it is in your "project" or whatever it is your IDE calls it.

No, it compiles correctly but the methods do nothing when I execute it.

Only one of your methods has any code in, and it doesn't do anything you'll see. What did you expect to happen? What does it not do that you think it should do?
Last edited on
When I include the .cpp in the main I get compiler errors:

Do not include the cpp in the main. You only need to make sure it is in your "project" or whatever it is your IDE calls it.


It is in my project. I guess I should ask in a Visual studio forum about this.

No, it compiles correctly but the methods do nothing when I execute it.

Only one of your methods has any code in, and it doesn't do anything you'll see. What did you expect to happen? What does it not do that you think it should do?


I used to have more methods but I thought I f*cked something up in the .cpp file so I deleted both files and started fresh.

The constructor is supposed to set a Texture to the Sprite and then give it a position so it can be drawn on the screen. The code works fine if I put it in the main but it doesn't do anything when I create an object with the constructor or a any of the methods.
Topic archived. No new replies allowed.