Class error

can somebody figure out what went wrong and fix the error please?
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
class Monster
{
      public:      
      // constructor
      Monster() { cout << "\n\tBuilding a Monster"; }
      ~Monster() { cout << "\n\tDestroying a Monster"; }
      //Memeber Methods      
      void DisplayStats()
      {
           cout << "\n\n\t--------Monster Stats--------";
           cout << "\n\tYou sent out a " << Type << " type.";
           cout << "\n\tIts stats are";
           cout << "\n\tAttack" << Attack;
           cout << "\n\tDefense" << Defense;
           cout << "\n\tHealth" << Health;
           cout << "\n\tAdvantage" << Advantage;
           cout << "\n\tDisadvantage" << Health;
           cout << "\n\t-------------------------------";
      } 
      //Accessor Methods
      string GetType() { return Type; }
      void SetType(string x) { Type = x; }
      
      int GetAttack() { return Attack; }
      void SetAttack(int x) { Attack = x; }
      
      int GetDefense() { return Defense; }
      void SetDefense(int x) { Defense = x; }
      
      int GetHealth() { return Health; }
      void SetHealth(int x) { Health = x; }
      
      string GetAdvantage() { return Advantage; }
      void SetAdvantage(string x) { Advantage = x; }
      
      string GetDisadvantage() { return Disadvantage; }
      void SetDisadvantage(string x) { Disadvantage = x; }
      
      string GetMonsterName() { return MonsterName; }
      void SetMonsterName(string x) { MonsterName = x; }
      
      private:
      string Type;
      int Attack;
      int Defense;
      int Health;
      string Advantage;
      string Disadvantage;
      string MonsterName;
}; 
int main(int argc, char *argv[])
{
Monster * YourFireType = new Monster;
YourFireType->SetType("Fire");
YourFireType->SetAttack(8);
YourFireType->SetDefense(1);
YourFireType->SetHealth(40);
YourFireType->SetAdvantage("Nature");
YourFireType->SetDisadvantage("Water");
YourFireType->SetMonsterName("YourFireType");
YourFireType->DisplayStats();
system("PAUSE")
return 0
};
Well you're missing semicolons after system("PAUSE") and return 0, and have one that shouldn't be there after main's closing brace.

Also, instead of just saying "fix the problem" how about you tell us at least what the error is.
Last edited on
ok edited it, ands the specific error is something about string not working
Error Messages over some little mistake
[26 C:\Dev-Cpp\Untitled7.cpp `string' does not name a type code]
27 C:\Dev-Cpp\Untitled7.cpp variable or field `SetType' declared void
27 C:\Dev-Cpp\Untitled7.cpp expected `;' before '(' token
29 C:\Dev-Cpp\Untitled7.cpp expected `;' before "int"
38 C:\Dev-Cpp\Untitled7.cpp `string' does not name a type
39 C:\Dev-Cpp\Untitled7.cpp variable or field `SetAdvantage' declared void
39 C:\Dev-Cpp\Untitled7.cpp expected `;' before '(' token
41 C:\Dev-Cpp\Untitled7.cpp expected `;' before "string"
41 C:\Dev-Cpp\Untitled7.cpp `string' does not name a type
42 C:\Dev-Cpp\Untitled7.cpp variable or field `SetDisadvantage' declared void
42 C:\Dev-Cpp\Untitled7.cpp expected `;' before '(' token
44 C:\Dev-Cpp\Untitled7.cpp expected `;' before "string"
44 C:\Dev-Cpp\Untitled7.cpp `string' does not name a type
45 C:\Dev-Cpp\Untitled7.cpp variable or field `SetMonsterName' declared void
45 C:\Dev-Cpp\Untitled7.cpp expected `;' before '(' token
47 C:\Dev-Cpp\Untitled7.cpp expected `;' before "private"
48 C:\Dev-Cpp\Untitled7.cpp `string' does not name a type
52 C:\Dev-Cpp\Untitled7.cpp `string' does not name a type
53 C:\Dev-Cpp\Untitled7.cpp `string' does not name a type
54 C:\Dev-Cpp\Untitled7.cpp `string' does not name a type
C:\Dev-Cpp\Untitled7.cpp In constructor `Monster::Monster()':
10 C:\Dev-Cpp\Untitled7.cpp `cout' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
C:\Dev-Cpp\Untitled7.cpp In destructor `Monster::~Monster()':
11 C:\Dev-Cpp\Untitled7.cpp `cout' undeclared (first use this function)
C:\Dev-Cpp\Untitled7.cpp In member function `void Monster::DisplayStats()':
15 C:\Dev-Cpp\Untitled7.cpp `cout' undeclared (first use this function)
16 C:\Dev-Cpp\Untitled7.cpp `Type' undeclared (first use this function)
21 C:\Dev-Cpp\Untitled7.cpp `Advantage' undeclared (first use this function)
C:\Dev-Cpp\Untitled7.cpp In function `int main(int, char**)':
59 C:\Dev-Cpp\Untitled7.cpp 'class Monster' has no member named 'SetType'
63 C:\Dev-Cpp\Untitled7.cpp 'class Monster' has no member named 'SetAdvantage'
64 C:\Dev-Cpp\Untitled7.cpp 'class Monster' has no member named 'SetDisadvantage'
65 C:\Dev-Cpp\Untitled7.cpp 'class Monster' has no member named 'SetMonsterName'

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
class Monster
{
      public:      
      // constructor
      Monster() { cout << "\n\tBuilding a Monster"; }
      ~Monster() { cout << "\n\tDestroying a Monster"; }
      //Memeber Methods      
      void DisplayStats()
      {
           cout << "\n\n\t--------Monster Stats--------";
           cout << "\n\tYou sent out a " << Type << " type.";
           cout << "\n\tIts stats are";
           cout << "\n\tAttack" << Attack;
           cout << "\n\tDefense" << Defense;
           cout << "\n\tHealth" << Health;
           cout << "\n\tAdvantage" << Advantage;
           cout << "\n\tDisadvantage" << Health;
           cout << "\n\t-------------------------------";
      } 
      //Accessor Methods
      string GetType() { return Type; }
      void SetType(string x) { Type = x; }
      
      int GetAttack() { return Attack; }
      void SetAttack(int x) { Attack = x; }
      
      int GetDefense() { return Defense; }
      void SetDefense(int x) { Defense = x; }
      
      int GetHealth() { return Health; }
      void SetHealth(int x) { Health = x; }
      
      string GetAdvantage() { return Advantage; }
      void SetAdvantage(string x) { Advantage = x; }
      
      string GetDisadvantage() { return Disadvantage; }
      void SetDisadvantage(string x) { Disadvantage = x; }
      
      string GetMonsterName() { return MonsterName; }
      void SetMonsterName(string x) { MonsterName = x; }
      
      private:
      string Type;
      int Attack;
      int Defense;
      int Health;
      string Advantage;
      string Disadvantage;
      string MonsterName;
}; 
int main(int argc, char *argv[])
{
Monster * YourFireType = new Monster;
YourFireType->SetType("Fire");
YourFireType->SetAttack(8);
YourFireType->SetDefense(1);
YourFireType->SetHealth(40);
YourFireType->SetAdvantage("Nature");
YourFireType->SetDisadvantage("Water");
YourFireType->SetMonsterName("YourFireType");
YourFireType->DisplayStats();
system("PAUSE");
return 0;
};
Last edited on
using std::string ;

cire (1141) Oct 5, 2012 at 8:55pm
using std::string ;


Yeah, the compiler flips out when you don't do that.
Having public get & set functions for every private member variable is the same as having all the variables public!!!!

To avoid the set functions, set the variables with constructors - that is the purpose of a constructor - to initialise variables.

To change the value of member variables in an object after initialisation, think about what functions you might need that reflect the real situation.

HTH
thanks for these good type of information....
Topic archived. No new replies allowed.