Jump to content

  • Log In with Google      Sign In   
  • Create Account

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   Members   

649
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.


#2   Moderators   

10730
Like
0Likes
Like

Posted 26 May 2014 - 11:37 AM

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

#3   Members   

2113
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   GDNet+   

9598
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.

Hello to all my stalkers.


#5   Members   

649
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   Members   

1339
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   Members   

2113
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   Members   

10965
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   Members   

649
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   Members   

649
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   Members   

4157
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   Moderators   

21386
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 ;-)


Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]




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.