Thread: Global variables used in a linked class, getting errors.

  1. #1
    1479
    Join Date
    Aug 2003
    Posts
    253

    Global variables used in a linked class, getting errors.

    Alright I am continuing my text based game. I have created a class for weapons and while all the code is in one single file it compiles and runs fine. However, recently I have been reading up on classes and found out that it's better to put class definitions in a .h file, put the member definitions in a different .cpp file and simply #include it with your main program. Both my .h and .cpp file inlude the "weapons.h" but it is giving me errors. I think this is because I am using global variables in the int main(); program in my .cpp file cause that is where I am getting a bunch of 'undeclared' errors. Should I just make another class for all my global variables and use inheritance or make my global variables in int main(); pointers?
    Knowledge is power and I want it all

    -0RealityFusion0-

  2. #2
    1479
    Join Date
    Aug 2003
    Posts
    253
    Ok I solved that problem. Turns out I just made a typo when I included weapon.h. My new problem is that its giving me another error 'string' does not name a type. Here is my weapon.h file.
    Code:
    #ifndef Weapon_H
    #define Weapon_H
    
    class Weapons {
          public:
                 //Weapons();
                 string getWeaponValue(); //Legendary, Epic, unique, rare, magical, normal
                 string getWeaponType(); //Sword, mace, staff
                 string getWeaponName(); //Name the weapon
                 int getWeaponStats(); // damage, +mods and durability
                 int getMagicStats();
                 int getWeaponLevel(); // level of the weapon
                 void whoCanDrop();  //check to see which mobs can drop it
          private:
                  string WeaponName;
                  string WeaponType;
                  string WeaponValue;
                  int WeaponStats;
                  int WeaponDamage;
                  int WeaponDurability;
                  int WeaponLevel;
                  int WeaponMod1; //Str
                  int WeaponMod2;//Hp's
                  int WeaponMod3;//Mp's
                  int WeaponMod4;//agi
                  int WeaponMod5; //armor
                  int WeaponMod6; //damage
                  char CanIDropIt;
                  };
    #endif
    The error, however, is catching it in main and highlights the #include "weapon.h".
    Code:
    #include <stdio.h> 
    #include <windows.h>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    
    
    #include "Weapon.h"  <----Error at this line
    using namespace std;
    
    int main()
    {
      HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
      WORD wOldColorAttrs;
      CONSOLE_SCREEN_BUFFER_INFO csbiInfo; 
      
      /*
       * First save the current color information
       */
      GetConsoleScreenBufferInfo(h, &csbiInfo);
      wOldColorAttrs = csbiInfo.wAttributes; 
      
        int x;
        string blah;
        srand(time(0));
        StartGame();
        system ("cls");
        cout <<"Welcome " <<YourName <<" to the Age of Spire!" <<endl;
        cout <<endl;
          system ("cls"); 
          cout <<"You open your eyes to find yourself in a small shack.  You get out of bed and"<<endl;
          cout <<"walk towards the door.As you open the door you can hear the sound of metal"<<endl;
          cout <<"clanging and the chatter and bustle of city life. Judging by the brightness" <<endl;
          cout <<"of the sun and the heat you assume that its around noon." <<endl;
          
          
          //getWeaponValue();
         
      SetConsoleTextAttribute ( h, wOldColorAttrs);
         AnyKey();
     }
    Gives me a the following errors:
    In file included from TestAosduh.cpp
    'string' does not name a type <----6 of this same error

    Any help would be apprieciated Im so confused.
    Knowledge is power and I want it all

    -0RealityFusion0-

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Since you use string in Weapons.h, you should include <string> in Weapons.h. Also, you need to specify the std namespace in Weapons.h. The best way to do that is of course to use std::string in the header file.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Also, you need to specify the std namespace in Weapons.h
    Bad idea - everything which includes the header then gets the namespace whether they want it or not.

    In Weapons.h, as you said,
    #include <string>
    ...
    std::string getWeaponValue();
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. static class variable vs. global variable
    By nadamson6 in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2005, 03:31 PM
  3. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM