Thread: Request for help with classes and the use of references on variables within them

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    Request for help with classes and the use of references on variables within them

    Hello all. I've been trying to make a class of functions that track certain responses that a player makes in my text adventure and modify the dialogue options based on each game character's opinion of the player. Unfortunately, I think I may have done something very wrong with the class definition.

    I defined the class and the functions within it like this:
    Code:
    class characters
    {
        public:
        characters();
        ~characters();
        int wind_choice_one;
        int wind_opinion;
        int wind_actions (int wind_opinion); // okay, so the idea here is that as Wind's opinion increases, new dialogue options are opened up.
    };
    
    characters::characters()
    {
        wind_choice_one = 0;
    }
    
    characters::~characters()
    {
    }
    
    int characters::wind_actions (int wind_opinion)
    {
        return (wind_opinion);
    }
    The definition seems to fit well with what this site's tutorial suggested, but when I defined an instance of the class in main like so:
    Code:
    characters game_characters;
    Then a reference to a variable within that class:
    Code:
    int& wind_oppinion_reference = game_characters.wind_oppinion;
    And then tried to use the statement:
    Code:
    wind_opinion_reference + 1;
    to increase it, I got a compiler warning stating that the statement had no affect.

    Even worse, when I tried to call a function from the class like:
    Code:
     cout << game_characters.wind_actions();
    I got a compiler error which stated:|error: no matching function for call to 'characters::wind_actions()'|

    I compared my code to the example code from the tutorial on classes on this site and couldn't see anything obviously wrong with it. I tried modifying the function definition and moving the lines outside of any switch cases and loops which might have prevented the statements from making modifications, but nothing seems to work.

    Does anything about the way I defined the class seem off?
    I would be deeply grateful for any help I could get.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    There's nothing wrong with your class definition - you've just made some silly mistakes:

    Quote Originally Posted by chaucer345
    And then tried to use the statement:
    Code:
    wind_opinion_reference + 1;
    to increase it, I got a compiler warning stating that the statement had no affect.
    Right. That's nothing to do with classes or references or anything. If you do
    Code:
       int i = 1;
       i + 1;
    You'll get the same warning. What you mean is:
    Code:
    wind_opinion_reference = wind_opinion_reference + 1; 
    or
    wind_opinion_reference++;
    or
    ++wind_opinion_reference;
    or
    wind_opinion_reference += 1;
    Also, you haven't initialised wind_opinion to anything at this point, so adding 1 to it isn't going to give you anything useful

    Quote Originally Posted by chaucer345
    Even worse, when I tried to call a function from the class like:
    Code:
     cout << game_characters.wind_actions();
    I got a compiler error which stated:|error: no matching function for call to 'characters::wind_actions()'|
    Function definition is:
    Code:
    int characters::wind_actions (int wind_opinion)
    {
        return (wind_opinion);
    }
    The function takes an argument. You're not passing an argument. A more helpful error would have been "'characters::wind_actions' : function does not take 0 arguments".

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    Awesome! I changed the syntax and added the reference as an argument to the function and now it's working. Thank you so much.

    I had actually already stated
    Code:
    game_characters.wind_opinion = 0;
    in main() and forgot to include it in my post. Sorry about that, I was cherry picking from a lot of code and forgot about it.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    You're welcome

    Syntax might be correct now, but I have to say that this seems a strange thing to do. If you need update wind_opinion from several different functions, I'd expect to use a reference or pointer to the whole class, not to a specific member. I'm not sure I can think of a valid reason for doing it like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 07-23-2012, 03:36 PM
  2. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  3. references to function in classes
    By Chrip in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2007, 11:39 AM
  4. References in classes
    By tigs in forum C++ Programming
    Replies: 5
    Last Post: 08-10-2004, 06:30 PM
  5. References to classes
    By gustavosserra in forum C++ Programming
    Replies: 3
    Last Post: 05-30-2003, 10:01 PM