Advertisement

Can you do this with a class?

Started by May 26, 2014 05:27 PM
10 comments, last by ApochPiQ 10 years, 8 months ago

The code is kinda heavy but the error is very specific. I got an LNK2005 error that telling me my initiation of my variable in my header file is already defined in a CPP file. In the header file all I have is a class named Game. I initialized my variables in two different CPP files like such

Game::Game()

{

//Initialize variables from class

}

// void or int function from class

{

ect......

}

It didn't give me this error in the first cpp file but it giving me problems in the second. Does anyone have any idea how to fix this?

Can you post the header, source files and the exact error message?
Advertisement

It sounds like a syntax problem but with such little code it's really hard to tell you something. At least show the lines that refer to that variable in each file.

You're not including enough information to tell for certain, but this might be of some help:

http://stackoverflow.com/questions/622229/error-lnk2005-already-defined-c

Edit: Super-ninja'd.

Hello to all my stalkers.

The error was very specific and I think I already figured out the problem. Does this make sense?

Header file
Class
{
Game();
~Game();
}

CPP file 1
//Declarations
Game:Game()
{
//variable = value;
}

CPP file 2
//Declarations
Game::Game();
{
//variables = value;
}

The error was very specific and I think I already figured out the problem. Does this make sense?

Header file
Class
{
Game();
~Game();
}

CPP file 1
//Declarations
Game:Game()
{
//variable = value;
}

CPP file 2
//Declarations
Game::Game();
{
//variables = value;
}

What doesn't make sense? You define the same function twice... Just create one header file and one implementation file.

Advertisement

You can't define the same function of a class in 2 cpp files and use both at the same time. But you're code is still bad to answer the question (or it isn't related to class attributes as you said in the first post), can you write a simple working class to reproduce the issue and paste that code instead?

If you want different constructors you have other working options:

* define multiple Game() methods in the headers file (with different args) each for different uses

* use a #ifdef to use the one you need (if the reason to have 2 definitions is only for testing) in each situation

* create an abstract constructor on a base class and create 2 more clases that implement the constructor in different ways

Can you post ACTUAL code? What you posted isn't even remotely legal C++, which means we can't help you spot errors.

You can't define the same function of a class in 2 cpp files and use both at the same time. But you're code is still bad to answer the question (or it isn't related to class attributes as you said in the first post), can you write a simple working class to reproduce the issue and paste that code instead?

If you want different constructors you have other working options:

* define multiple Game() methods in the headers file (with different args) each for different uses

* use a #ifdef to use the one you need (if the reason to have 2 definitions is only for testing) in each situation

* create an abstract constructor on a base class and create 2 more clases that implement the constructor in different ways

I was

You can't define the same function of a class in 2 cpp files and use both at the same time. But you're code is still bad to answer the question (or it isn't related to class attributes as you said in the first post), can you write a simple working class to reproduce the issue and paste that code instead?

If you want different constructors you have other working options:

* define multiple Game() methods in the headers file (with different args) each for different uses

* use a #ifdef to use the one you need (if the reason to have 2 definitions is only for testing) in each situation

* create an abstract constructor on a base class and create 2 more clases that implement the constructor in different ways

The 1st and 3rd options sounds good. Thanks I'll going to see if this works.

Yay! It works........ but I mess up my code's syntax in the process. Thanks All.

This topic is closed to new replies.

Advertisement