cant get my class to work

im trying to create my first class. my compiler is giving me an error. i also have no idea is this will work at all if there are no syntax errors.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>

using namespace std;

int main()
{
    class Warrior
    {
        public:

        int CalcStats(int  &BaseHP, int &HP, int &Strength)
        {
            cout << "Enter Points in strength" << endl;
            cin >> Strength;

            cout << "enter points in health" << endl;
            cin >> HP;

            BaseHP = HP;

            return BaseHP, HP, Strength;
        }

         int BaseHP;
         int Strength;
         int HP;
    };

    Warrior larry;
 
  larry.CalcStats; //this is where the error is. its says statement            cannot resolve address of overloaded function
 
  cout << "your strength is:" << larry.Strength;
 
  return 0;
}
Because CalcStats is a function not a variable. When you call a function don't you put the brackets next to it with an argument list?
Also you should put your class definition globally outside of main unless you have special reason for making it a local class.
The parentheses aren't optional even when the function takes no parameters.

Your return on line 21 won't do what you're expecting. A function can't return more than one value.
Oh yes that is also true.
I presume that your function is to fill out your class. So you should take no parameters and do all the input in the function, ending up with something like this:
1
2
3
4
5
6
7
8
9
10
        void CalcStats()
        {
            cout << "Enter Points in strength" << endl;
            cin >> Strength;

            cout << "enter points in health" << endl;
            cin >> HP;

            BaseHP = HP;
        }

That already modifies the values of the class object on which it is called (the this pointer) so there is no point in returning any of those values or taking any arguments.
By the way, since your entire class is public, you can change the class keyword to the struct keyword instead. Structs are public by default where as classes are private by default. That should save you a line of code.
Last edited on

Also you should put your class definition globally outside of main unless you have special reason for making it a local class.


yeah its just a test. this is going to go in a header file for a text rpg game im making when i get it to work right.

wow it actually worked! thanks for the help.
So you should take no parameters and do all the input in the function
What the hell kind of advice is that? Input should be taken as close to main() as possible and then passed down to the functions that need it.
I'm not giving him advice. It's what his function does. Otherwise why would he be taking input in the function?
Topic archived. No new replies allowed.