Thread: Need help with errors

  1. #1
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up Need help with errors

    I'm trying to make a simple text RPG with Dev C++ that has spells as a form of attack. Anyways, I have a class for the spells in a .h file but it comes up with errors saying:

    "'std::string spell::name is private' within this context"

    I'm relatively new to classes and this makes next to no sense to me. This is the code its coming up with errors on:

    Code:
    class spell
    {
          private:
                  string name; //spell name
                  int damage; //damage
                  bool fire; //element
    };
    
    inline void load()
    {
                  spell fire1;
                  fire1.name=("Inferno");
                  fire1.damage=1;
                  fire1.fire=true;
    }
    Thanks in advanced.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Free functions cannot access non-public members of a class unless they are friends of the class. It looks like you may want to provide a constructor for your class and use that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up

    Alright, so I added the constructor & destructor (I'm not great with these either) but now I get a new error saying:

    "[Linker Error] undefined reference to 'spell::~spell()'"

    Code:
    class spell
    {
          public:
                  spell();
                  ~spell();
          private:
                  string name; //spell name
                  int damage; //damage
                  bool fire; //element
    };
    
    spell::spell()
    {
                  spell fire1;
                  fire1.name=("Inferno");
                  fire1.damage=1;
                  fire1.fire=true;
    }
    
    spell::~spell()
    {
    }
    What am I doing wrong this time?

  4. #4
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Remove spell fire1; from the constructor. Your constructor is building another spell object, which will build another spell object, which will build another... and so on.

    Your constructor should look like this:

    Code:
    spell::spell()
    {
                  name=("Inferno");
                  damage=1;
                  fire=true;
    }

  5. #5
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204
    I see. Thanks a bunch, all.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should really be using the initializer list in your constructors to initialize the variables like this:
    Code:
    spell::spell() : name("Inferno"),  damage(1), fire(true)
    {
    }

  7. #7
    Not stupid, just stupider yaya's Avatar
    Join Date
    May 2007
    Location
    Earthland
    Posts
    204

    Thumbs up

    Alright, I have no idea what to do. I'll just give you the entire .h file with all the spells and stuff because I'm still getting the same linker error.

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    There's a couple of problems with this. Firstly, exactly what laserlight said - the variables in the class are declared as private - this means that only member functions of the class or friend functions can access them. 'load' is not a member function or friend function, so that's where the error comes from. To fix it you would probably do something like this:

    Code:
    class spell
    {
    public:
         spell();
         ~spell();
         void load (parameters);	// this is how to declare a member function
         // or use
         void friend load (parameters)	// this is declaring the global function as a friend
    
    private:
          string name;
          int damage;
      		//catagories
          bool fire;
    	  // etc.
    };
    
    // Then you'd do this
    
    void spell::load(parameters)	// if using the member method
    {
    	// Code
    }
    
    // or
    
    void load (parameters)	// if using the friend approach
    {
    	// Code
    }
    Secondly, I notice that you declare a number of instances of spells in 'load', which is perfectly allowed - it looks like you want to use the function to declare and setup all the variables. The problem is that it's probably a bit useless in your program because of scope - if you write a function and declare variables in it, that data can't be accessed from other functions unless you return a value from the function. What you can do is declare variables the calling function (eg if you are in main and are calling load, main is the calling function) and pass them by pointer/reference to load if using the friend approach, or just us the member function approach and call the method - much easier for this type of thing:

    Code:
    // headers and stuff
    
    // Load is a member of spell
    
    int main (void)
    {
    	spell var1, var2;	// etc;
    
    	// Code that determines the values to use (eg read in from user)
    
    	var1.load(values);
    	var2.load(values);	// etc
    
    	return 0;
    }
    This is essentially the same idea as using a constructor - in fact a constructor is the preferred method for this since declaration and initialisation occur in a nice single statement.

    Lasty, you probably don't need to use inline...
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Help me with these errors... :-(
    By major_small in forum C++ Programming
    Replies: 6
    Last Post: 09-07-2003, 08:18 PM
  5. errors in class(urgent)
    By ayesha in forum C++ Programming
    Replies: 2
    Last Post: 11-10-2001, 06:51 PM