ifndef not working.

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2339 Views - Last Post: 23 August 2008 - 07:44 PM

#1 Rezert  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 71
  • Joined: 17-August 08

ifndef not working.

Post icon  Posted 23 August 2008 - 07:43 AM

For some reason #ifndef isn't working. I'm trying to make a C++ CLI RPG. Here's the files.

main.cpp:
#include <iostream>
#include "Globals.h"

using namespace std;

bool Decisions();

int main() {
	cout << "\n\nAlien Abudction" << endl;
	cout << "created by Rezert" << endl;
	cout << "\nWhat is your name?" << endl;
	cout << ">";
	cin >> name;
	cout << "\nWelcome to Alien Abudction, " << name << "." << endl;
	cout << "\nYou wake up to find yourself in an alienship and you can't\nremember anything.  You break the cords you're bound by\nand realize you must escape." << endl;
	
	Decisions();
	
	system("pause");
	return EXIT_SUCCESS;
}



Shop.cpp:
#include <iostream>
#include "Globals.h"

using namespace std;

bool Shop() {
	 cout << "Name:   " << name << endl;
	 cout << "Weapon: " << wep << endl;
	 cout << "Damage: " << wdmg << endl;
	 cout << "\nShop features coming soon..." << endl;
	 return true;
}



Decisions.cpp:
#include <iostream>
#include "Globals.h"

using namespace std;

int choice = 10;
bool Shop();

bool Decisions() {
	 while(choice != 0) {
				  cout << "Where would you like to go?" << endl;
				  cout << "0. Exit Game" << endl;
				  cout << "1. Medical Room" << endl;
				  cout << "2. Shop" << endl;
				  
				  if(level > 0) {
						   cout << "3. Fight Alien Larva (Mission)" << endl;
				  }
				  if(level > 1) {
						   cout << "4. Fight Baby Aliens (Mission)" << endl;
				  }
				  if(level > 2) {
						   cout << "5. Fight Gaurd Aliens (Mission)" << endl;
				  }
				  if(level > 3) {
						   cout << "6. Fight Mother Alien (Mission)" << endl;
				  }
	 
				  cin >> choice;
				  
				  switch(choice) {
								 case 1:
									  cout << "This option is not available yet." << endl;
								 break;
								 case 2:
									  Shop();
								 break;
								 case 3:
									  cout << "This option is not available yet." << endl;
								 break;
								 case 4:
									  cout << "This option is not available yet." << endl;
								 break;
								 case 5:
									  cout << "This option is not available yet." << endl;
								 break;
								 case 6:
									  cout << "This option is not avilable yet." << endl;
								 break;
				  }
	 }
	 return true;
}



Globals.h:
#ifndef GLOBALS_H
#define GLOBALS_H

char name[36] = "Abducted Human";
char wep[32]  = "Knife";
int  wdmg	 = 1;
int  level	= 1;

#endif



I'm using Bloodshed Dev-C++. (code::blocks doesn't compile for me.) For some reason, it continues to say "multiple definition of `name`", "multiple definition of `wep`", "multiple definition of `wpdmg`", "multiple definition of `level`".

I'm a n00b at C++ so I have no idea what's wrong.

Any help would be greatly appreciated.

This post has been edited by Rezert: 23 August 2008 - 07:48 AM


Is This A Good Question/Topic? 0
  • +

Replies To: ifndef not working.

#2 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3199
  • View blog
  • Posts: 19,230
  • Joined: 14-September 07

Re: ifndef not working.

Posted 23 August 2008 - 07:59 AM

You declare name to be Abducted Human, but then ask for more information inside of main(). Scope resolution won't allow to do that. Either change the way you have globals set up (which at this point is bad programming practice) or change the input variable sequence's name.
Was This Post Helpful? 0
  • +
  • -

#3 Rezert  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 71
  • Joined: 17-August 08

Re: ifndef not working.

Posted 23 August 2008 - 08:03 AM

So that would be like this now?

char name[36];


That doesn't fix any of the errors.

This post has been edited by Rezert: 23 August 2008 - 08:03 AM

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3199
  • View blog
  • Posts: 19,230
  • Joined: 14-September 07

Re: ifndef not working.

Posted 23 August 2008 - 08:04 AM

For name input try

cin.getline(name, 255);


Was This Post Helpful? 0
  • +
  • -

#5 Rezert  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 71
  • Joined: 17-August 08

Re: ifndef not working.

Posted 23 August 2008 - 08:08 AM

Still getting that error.
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3199
  • View blog
  • Posts: 19,230
  • Joined: 14-September 07

Re: ifndef not working.

Posted 23 August 2008 - 08:11 AM

The multiple defined error? What does your Globals.h file look like now?
Was This Post Helpful? 0
  • +
  • -

#7 Rezert  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 71
  • Joined: 17-August 08

Re: ifndef not working.

Posted 23 August 2008 - 08:15 AM

Yes, the multiple define error.

main.cpp:
#include <iostream>
#include "Globals.h"

using namespace std;

bool Decisions();

int main() {
	cout << "\n\nAlien Abudction" << endl;
	cout << "created by Rezert" << endl;
	cout << "\nWhat is your name?" << endl;
	cout << ">";
	cin.getline(name, 32);
	cout << "\nWelcome to Alien Abudction, " << name << "." << endl;
	cout << "\nYou wake up to find yourself in an alienship and you can't\nremember anything.  You break the cords you're bound by\nand realize you must escape." << endl;
	
	Decisions();
	
	system("pause");
	return EXIT_SUCCESS;
}



Decisions.cpp:
#include <iostream>
#include "Globals.h"

using namespace std;

int choice = 10;
bool Shop();

bool Decisions() {
	 while(choice != 0) {
				  cout << "Where would you like to go?" << endl;
				  cout << "0. Exit Game" << endl;
				  cout << "1. Medical Room" << endl;
				  cout << "2. Shop" << endl;
				  
				  if(level > 0) {
						   cout << "3. Fight Alien Larva (Mission)" << endl;
				  }
				  if(level > 1) {
						   cout << "4. Fight Baby Aliens (Mission)" << endl;
				  }
				  if(level > 2) {
						   cout << "5. Fight Gaurd Aliens (Mission)" << endl;
				  }
				  if(level > 3) {
						   cout << "6. Fight Mother Alien (Mission)" << endl;
				  }
	 
				  cin >> choice;
				  
				  switch(choice) {
								 case 1:
									  cout << "This option is not available yet." << endl;
								 break;
								 case 2:
									  Shop();
								 break;
								 case 3:
									  cout << "This option is not available yet." << endl;
								 break;
								 case 4:
									  cout << "This option is not available yet." << endl;
								 break;
								 case 5:
									  cout << "This option is not available yet." << endl;
								 break;
								 case 6:
									  cout << "This option is not avilable yet." << endl;
								 break;
				  }
	 }
	 return true;
}



Shop.cpp:
#include <iostream>
#include "Globals.h"

using namespace std;

bool Shop() {
	 cout << "Current stats..." << endl;
	 cout << "Name:   " << name << endl;
	 cout << "Weapon: " << wep << endl;
	 cout << "Damage: " << wdmg << endl;
	 cout << "\nShop features coming soon..." << endl;
	 return true;
}



Globals.h:
#ifndef GLOBALS_H
#define GLOBALS_H

char name[32];
char wep[32] = "Knife";
int  wdmg	= 1;
int  level   = 1;
int  gold	= 100;

#endif


Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3199
  • View blog
  • Posts: 19,230
  • Joined: 14-September 07

Re: ifndef not working.

Posted 23 August 2008 - 08:24 AM

Ok, i ran the code and now know the issue. Each .cpp creates its own .obj file and the globals are already defined in main's object file.I moved Decisions to a header file and ahd it include your globals:

//Globals.h
#ifndef GLOBALS_H
#define GLOBALS_H

char name[32];
char wep[32] = "Knife";
int  wdmg    = 1;
int  level   = 1;
int  gold    = 100;

#endif



//Decisions.h
#include <iostream>
#include "Globals.h"


using namespace std;

int choice = 10;
bool Shop();

bool Decisions() {
     while(choice != 0) {
                  cout << "Where would you like to go?" << endl;
                  cout << "0. Exit Game" << endl;
                  cout << "1. Medical Room" << endl;
                  cout << "2. Shop" << endl;
                  
				  if(::level > 0) {
                           cout << "3. Fight Alien Larva (Mission)" << endl;
                  }
                  if(::level > 1) {
                           cout << "4. Fight Baby Aliens (Mission)" << endl;
                  }
                  if(::level > 2) {
                           cout << "5. Fight Gaurd Aliens (Mission)" << endl;
                  }
                  if(::level > 3) {
                           cout << "6. Fight Mother Alien (Mission)" << endl;
                  }
    
                  cin >> choice;
                  
                  switch(choice) {
                                 case 1:
                                      cout << "This option is not available yet." << endl;
                                 break;
                                 case 2:
                                   //   Shop();
                                 break;
                                 case 3:
                                      cout << "This option is not available yet." << endl;
                                 break;
                                 case 4:
                                      cout << "This option is not available yet." << endl;
                                 break;
                                 case 5:
                                      cout << "This option is not available yet." << endl;
                                 break;
                                 case 6:
                                      cout << "This option is not avilable yet." << endl;
                                 break;
                  }
     }
     return true;
}



You need break; statements in your switch cases otherwise all instances will run through no matter what the decision is.

//main.cpp
#include <iostream>
#include "Decisions.h"

using namespace std;

bool Decisions();

int main() {
    cout << "\n\nAlien Abudction" << endl;
    cout << "created by Rezert" << endl;
    cout << "\nWhat is your name?" << endl;
    cout << ">";
    cin.getline(name, 32);
    cout << "\nWelcome to Alien Abudction, " << name << "." << endl;
    cout << "\nYou wake up to find yourself in an alienship and you can't\nremember anything.  You break the cords you're bound by\nand realize you must escape." << endl;
    
    Decisions();
    
    system("pause");
    return EXIT_SUCCESS;
}



This is one solution. the other is to not use globals in that manner, but rather local variables declared in main or other functions. You want to keep scope as small as possible to prevent obscure bugs.

Was this post helpful? ;) ->
Was This Post Helpful? 1
  • +
  • -

#9 Rezert  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 71
  • Joined: 17-August 08

Re: ifndef not working.

Posted 23 August 2008 - 08:42 AM

Thanks a lot. I ended up making it so that the functions took arguments that were needed.

Thanks,
Rezert
Was This Post Helpful? 0
  • +
  • -

#10 Rezert  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 71
  • Joined: 17-August 08

Re: ifndef not working.

Posted 23 August 2008 - 08:54 AM

I have come across another problem.

I have code like this:

char choice[1];
...
cin >> choice;
...
if(choice.compare('y') == 0 || choice.compare('Y') == 0) {



It gives me this error:

Quote

request for member `compare' in `choice', which is a non-class type `char[1]'

Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3199
  • View blog
  • Posts: 19,230
  • Joined: 14-September 07

Re: ifndef not working.

Posted 23 August 2008 - 09:29 AM

make choice an integer instead of a char array.
Was This Post Helpful? 0
  • +
  • -

#12 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: ifndef not working.

Posted 23 August 2008 - 10:56 AM

View PostKYA, on 23 Aug, 2008 - 08:24 AM, said:

Ok, i ran the code and now know the issue. Each .cpp creates its own .obj file and the globals are already defined in main's object file.I moved Decisions to a header file and ahd it include your globals:


NO NO NO! KYA! I mean! Really!

You are correct that the problem is that the "global" variables are all being defined in the object files - but making one of the the solution is NOT to reduce the number of object files!

When you create a "global" variable in a file it is global to that file... to export/import it to other files you need to declare it as "extern"

So if you made Global.h into
#ifndef GLOBALS_H
#define GLOBALS_H

extern char name[36];
extern char wep[32];
extern int  wdmg;
extern int  level;

#endif


and then added the definitions to only one file (in this case I added a file called "Globals.cpp":
#include "Globals.h"

char name[36] = "Abducted Human";
char wep[32]  = "Knife";
int  wdmg	 = 1;
int  level	= 1;


Then ensure you choose to "Rebuild All" rather than just compile.

HOWEVER! I should warn you that using global variables is BAD! It is the devil's candy! It makes things easy, but it WILL end up biting you. You will eventually find yourself curled up in a corner with the remains of your computer scattered around you since in a fit of absolute frustration on why some clean and clear easy logic did not work you smashed your computer only to realize what you had done... smashed the computer BEFORE you made a backup copy of your work!

Global variables are bad because they allow any function to edit a value. Which means that while your logic may seem clean and clear, one of the OTHER functions may be changing the value one of the global values. The more complicated your program gets the harder it becomes to keep track of what functions edit what values, and before long you are going mad trying to figure out where in the hell things are going wrong.

I am warning you, for your own sanity -- DO NOT USE GLOBAL VALUES (unless they are constants)!!!

They may seem useful and neat but they are the Devil's candy and will drive you mad in the end.
Was This Post Helpful? 0
  • +
  • -

#13 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3199
  • View blog
  • Posts: 19,230
  • Joined: 14-September 07

Re: ifndef not working.

Posted 23 August 2008 - 01:01 PM

i thought about the use of extern, but he said he was brand new to cpp and i thought moving it into a header file would be simpler.
Was This Post Helpful? 0
  • +
  • -

#14 Rezert  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 71
  • Joined: 17-August 08

Re: ifndef not working.

Posted 23 August 2008 - 04:43 PM

Yes, I ended up using arguments to functions instead of globals. However, I have two questions.

1. Is the difference between char* and char that the length is more than 1 for char*?

How can I declare a variable in main.cpp from a function? e.g.

#include <iostream>

gold = 100;

bool ChangeGold(int amt) {
gold = amt;
}



That wouldn't work...how could I make it work?

This post has been edited by Rezert: 23 August 2008 - 04:54 PM

Was This Post Helpful? 0
  • +
  • -

#15 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3199
  • View blog
  • Posts: 19,230
  • Joined: 14-September 07

Re: ifndef not working.

Posted 23 August 2008 - 04:54 PM

char* is a pointer to a char. You can add memory to make it an array. A char is simple a character variable.

char* ptr = 0; 
ptr = malloc(4*sizeof(char)); // memory for 3 chars + end character or 4 chars, w/e
//do stuff
free ptr;



edited for code example


edited again for my stupidity

This post has been edited by KYA: 23 August 2008 - 07:59 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2