Just a problem with my classes

Well I decided to make my own program that very breifly simulates shooting a gun. The problem is that I keep getting compiler errrors

This is the main file
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* 
 * File:   Main.cpp
 * Author: Cameron
 *
 * Created on 1 June 2012, 4:20 PM
 */

#include "Rifle.hpp"
#include "Shotgun.hpp"
using namespace std;

Rifle::Rifle(int initialRAmmo)
{
    setRAmmo(initialRAmmo);
}

Rifle::~Rifle()
{
    cout << "You lower the Rifle\n";
}

void Rifle::setRAmmo(int newRAmmo)
{
    RAmmo = newRAmmo;
}

Shotgun::Shotgun(int initialSAmmo)
{
    setSAmmo(initialSAmmo);
}

Shotgun::~Shotgun()
{
    cout << "You lower the shotgun\n";
}

void Shotgun::setSAmmo(int newSAmmo)
{
    SAmmo = newSAmmo;
}

int main()
{
    int RifleOrShotgun, decision;
	Rifle Rifle1();
	Shotgun Shotgun1();
    cout << "You see a shotgun and a Rifle on the table.\n";
    cout << "Press 1, to pick up the Rifle, or 2. to pick up the Shotgun.\n";
    cin >> RifleOrShotgun;
    if (RifleOrShotgun == 1)
    {
        cout << "Press 1. to shoot, 2. to reload and 3. to put it down.\n";
        cin >> decision;
        while (decision < 3)
        {
			
            if (decision == 1)
            {
                Rifle1.shootRifle();
            }
            else
            {
                Rifle1.reloadRifle();
            }
            cin >> decision;
        }
    }
    else
    {
        cout << "Press 1. to shoot, 2. to reload and 3. to put it down.\n";
        cin >> decision;
        while (decision < 3)
        {
            if (decision == 1)
            {
                Shotgun1.shootShotgun();
            }
            else
            {
                Shotgun1.reloadShotgun();
            }
            cin >> decision;
        }
    }
    return 0;
}


Here is Rifle.hpp
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
/* 
 * File:   Rifle.hpp
 * Author: Cameron
 *
 * Created on 1 June 2012, 3:59 PM
 */

#include <iostream>
using namespace std;

class Rifle
{
public:
    Rifle(int initialRAmmo);
    ~Rifle();
    int getRAmmo() const { return RAmmo; }
    void setRAmmo(int RAmmo);
    void shootRifle()
    {
        if (RAmmo > 1)
        {
            setRAmmo(RAmmo - 1);
            cout << "Shooting the Rifle...\n";
        }
        else
        {
            cout << "Out of ammo you need to reload.\n";
        }
    }
    void reloadRifle()
    {
        if (RAmmo != 6)
        {
            setRAmmo(RAmmo = 6);
            cout << "Reloading the Rifle...\n";
        }
        else
        {
            cout << "You don't need to reload, you have a full clip\n";
        }
    }   
private:
    int RAmmo;
};


And here is Shotgun.hpp
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
/* 
 * File:   Shotgun.hpp
 * Author: Cameron
 *
 * Created on 1 June 2012, 4:10 PM
 */

#include <iostream>
using namespace std;

class Shotgun
{
public:
    Shotgun(int initialAmmo);
    ~Shotgun();
    int getSAmmo() const { return SAmmo; }
    void setSAmmo(int SAmmo);
    void shootShotgun()
    {
        if (SAmmo < 1)
        {
            setSAmmo(SAmmo - 1);
            cout << "Shooting the Shotgun\n";
        }
        else
        {
            cout << "You need to reload the shotgun first\n";
        }
    }
    void reloadShotgun()
    {
        if (SAmmo != 6)
        {
            setSAmmo(SAmmo = 6);
            cout << "Reloading the Shotgun\n";
        }
        else
        {
            cout << "You don't need to reload the shotgun\n";
        }
    }
private:
    int SAmmo;
};



editOh the errors are in the main one at Rifle1.shootRifle etc
Last edited on
What's the error?
1>c:\users\cameron\documents\visual studio 2010\projects\gunzerama\gunzerama\main.cpp(59): error C2228: left of '.shootRifle' must have class/struct/union
1>c:\users\cameron\documents\visual studio 2010\projects\gunzerama\gunzerama\main.cpp(63): error C2228: left of '.reloadRifle' must have class/struct/union
1>c:\users\cameron\documents\visual studio 2010\projects\gunzerama\gunzerama\main.cpp(77): error C2228: left of '.shootShotgun' must have class/struct/union
1>c:\users\cameron\documents\visual studio 2010\projects\gunzerama\gunzerama\main.cpp(81): error C2228: left of '.reloadShotgun' must have class/struct/union
1
2
3
4
5
6
7
8
int main()
{
    int RifleOrShotgun, decision;
	// Rifle Rifle1(); // *** this declares a function
	// Shotgun Shotgun1(); // *** and this too
        
        Rifle Rifle1 ; // this defines an object
        Shotgun Shotgun1 ;
I see thank you
Topic archived. No new replies allowed.