Classes

I am having trouble with classes. When I run this through the compiler, It says "unqualified-id before '{' token" on line 8. What's wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

class Square
{
      public:
      int area(int a, int b);
{
      return a*b;
}
}rect;

int main()
{
    cout << rect.area(12,2);
    char responce;
    cin >> responce;
    return 0;
}
You have an extra ; at the end of line 7
Thank you! I suck at classes.
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
#include <iostream>
using namespace std;
      class drink
{
      public:
      int Caffeine;
      int Tast;
};
      class Person
{
      public:
      int Caffeine;
      int Thirst = 100;
};
      int main()
{
      Person Bob;
      cout << "My name is Bob" << "\n";
      cout << "I want some coffee" << "\n";
      cout << "Get my thirst to zero to win" << "\n";
      cout << "Watch my caffeine" << "\n";
      cout << "If I have to much caffeine, I will die" << "\n";
      drink Coffee;
      Coffee.Caffeine = 10;
      Coffee.Tast = 10;
      while (true)
{
      char a;
      cout << "Enter 'D' to drink: ";
      cin >> a;
      if (a == "d" | a == "D")
{
      Bob.Caffeine += Coffee.Caffeine;
}      
}
      cout << Bob.Caffeine;
      char responce;
      cin >> responce;
      return 0;
}
      
      
      
      


There's a whole bunch of output errors. Like I said, I suck at classes.
3 simple errors in your code that I see are:
a) you cannot initialize a data member while declaring it in class unless it is static const data member. You should use constructor for initializng any data member.

b) In line 31 you cannot compare a char to string. Corrected way should be using single quotes instead of double.

c) For comparison, OR operator is || and not single |

Hope this helps !
Last edited on
Topic archived. No new replies allowed.