help with classes

heyo,
im working on classes in c++. i get the OOP concepts and have programmed in c# but am having some trouble with c++, can someone show me some examples, preferably in an IDE format.
thx for the tutorial, i get all the syntax. its just how to make it all work with multiple class files.
The same way you would with multiple files that weren't all class definitions. Preprocessor copies the headers into each file, each file then gets compiled into an object file, each object file is then given to the linker.
Last edited on
i am getting an error now, and i have no clue why


C:\Users\Jack Martin\Documents\myprograms\BattleGame\main.cpp||In function 'int main()':|
C:\Users\Jack Martin\Documents\myprograms\BattleGame\main.cpp|7|error: 'Hero' was not declared in this scope|
C:\Users\Jack Martin\Documents\myprograms\BattleGame\main.cpp|7|error: expected ';' before 'player'|
||=== Build finished: 2 errors, 0 warnings ===|



main.cpp
1
2
3
4
5
6
7
8
9
#include <iostream>

using namespace std;

int main()
{
        Hero player;
        return 0;
}


Hero.cpp
#inlcude "Hero.h"

Hero.h
1
2
3
4
5
6
7
8
9
class Hero
{
    public:
    //meathods
    void initialize();

    //variables
    int health, attackrating, attackdamage, defenserating, defense;
}
You're not #including "Hero.h" in your main class, it seems. You need to #include the header file for a class in every file where it's used.

Also, please note that you really should implement that initialize method for fear of getting a linker error.

-Albatross
You are missing a semicolon on line 9 in Hero.h
Last edited on
getting another error


main.cpp||In function 'int main()':|
main.cpp|9|error: statement cannot resolve address of overloaded function|
||=== Build finished: 1 errors, 0 warnings ===|


main.ccp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Hero.h"

using namespace std;

int main()
{
        Hero player;
        player.initialize;
        return 0;
}


Hero.cpp
1
2
3
4
5
6
#include "Hero.h"

void initialize()
{
health = 5;
}


Hero.h
1
2
3
4
5
6
7
8
9
class Hero
{
    public:
    //meathods
    void initialize();

    //variables
    int health, attackrating, attackdamage, defenserating, defense;
};

Try player.initialize();
2 problems.

1) Calling a function requires a parameter list

 
player.initialize();  // <- note the parenthesis 


2) your initialize function in Hero.cpp is global. That is not what you wanted. You have to put it in Hero's scope by using the :: scope operator:

1
2
3
4
void Hero::initialize()
{
  // ...
}
thx for all the help guys
Topic archived. No new replies allowed.