Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Can you do this with a class?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 LAURENT*   Members   -  Reputation: 243

Like
-1Likes
Like

Posted 26 May 2014 - 11:27 AM

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*, 26 May 2014 - 11:28 AM.


Sponsor:

#2 rip-off   Moderators   -  Reputation: 8762

Like
0Likes
Like

Posted 26 May 2014 - 11:37 AM

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

#3 DiegoSLTS   Members   -  Reputation: 1917

Like
0Likes
Like

Posted 26 May 2014 - 11:38 AM

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.



#4 Lactose!   GDNet+   -  Reputation: 3834

Like
0Likes
Like

Posted 26 May 2014 - 11:40 AM

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.


Edited by Lactose!, 26 May 2014 - 11:43 AM.


#5 LAURENT*   Members   -  Reputation: 243

Like
0Likes
Like

Posted 26 May 2014 - 11:47 AM

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



#6 ProtectedMode   Members   -  Reputation: 1286

Like
0Likes
Like

Posted 26 May 2014 - 12:07 PM

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.



#7 DiegoSLTS   Members   -  Reputation: 1917

Like
0Likes
Like

Posted 26 May 2014 - 12:11 PM

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, 26 May 2014 - 12:12 PM.


#8 Nypyren   Crossbones+   -  Reputation: 4823

Like
0Likes
Like

Posted 26 May 2014 - 12:13 PM

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, 26 May 2014 - 12:14 PM.


#9 LAURENT*   Members   -  Reputation: 243

Like
0Likes
Like

Posted 26 May 2014 - 12:33 PM

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. 



#10 LAURENT*   Members   -  Reputation: 243

Like
0Likes
Like

Posted 26 May 2014 - 12:45 PM

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



#11 dejaime   Crossbones+   -  Reputation: 4119

Like
0Likes
Like

Posted 27 May 2014 - 09:07 AM

//CPP file 1
Game:Game() // Function Definition
{
    //variable = value;    //**Function code*/
}

//CPP file 2
//Declarations
Game::Game(); //Function Declaration
{
    //variables = value;    //**Random Global Code*/
}

 

Just to clarify:

 

The first Game::Game() is a function definition, so it associates the code block {} under it as its own functionality.

The second Game::Game(); is a function declaration, that doesn't define it. The variable assignments inside the {} block will probably run at global initialization.


Edited by dejaime, 27 May 2014 - 09:09 AM.


#12 ApochPiQ   Moderators   -  Reputation: 16414

Like
3Likes
Like

Posted 27 May 2014 - 09:24 AM

Code blocks at file-level are illegal in C++, so no, that code would not run at all - because it wouldn't compile ;-)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS