Jump to content
  • Advertisement
Sign in to follow this  
LAURENT*

Can you do this with a class?

This topic is 1663 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

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?

Edited by LAURENT*

Share this post


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

Share this post


Link to post
Share on other sites

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;
}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Edited by DiegoSLTS

Share this post


Link to post
Share on other sites
Can you post ACTUAL code? What you posted isn't even remotely legal C++, which means we can't help you spot errors. Edited by Nypyren

Share this post


Link to post
Share on other sites

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. 

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!