Error C2100

Alright I'm having problem with this program where I have three different classes but the functions don't seem to be working because of it being a pointer or I'm doing something wrong

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

//Global Variable
const int SIZE = 3;

class Ship
{
private: 
		string name;
	    string yearBuilt;
public:
	Ship();
	//Overloaded Constructor
	Ship(string n, string y)
	{
		name = n;
		yearBuilt = y;
	}
	//Accessors
	string getName()
	{return name;}
	string getYearBuilt()
	{return yearBuilt;}

	//Mutator that gets info from user
	virtual void setInfo()
	{
		string n;
		string y;
		cout <<"Please enter the ship name: ";
		cin >>n;
		cout <<"Please enter the year the ship was built: ";
		cin >>y;
		name = n;
		yearBuilt = y;
	}
	//Print info for this function
	virtual void print()
	{
		cout <<"Ship"<<endl;
		cout <<"Name: "<<name<<endl
			<<"Year Built: "<<yearBuilt<<endl;
	}

};//end of ship

class CruiseShip : public Ship
{
private:
	int passengers;
public:
	CruiseShip();
	//Overloaded constructor that has inherited variables
	CruiseShip(string n, string y, int p): Ship(n,y)
	{
		passengers = p;
	}
	//Mutator that gets info  from user
	virtual void setInfo()
	{
		int p;
		cout <<"Please enter the number of passengers: ";
		cin >>p;
		passengers = p;
	}
	//print function
	virtual void print()
	{
		cout <<"Cruise Ship"<<endl;
		cout <<"Name: "<<getName()<<endl
			<<"Maximum Passanger: "<<passengers<<endl;
	}

};// end of CruiseShip
class CargoShip : public Ship
{
	int tonnage;
public:
	CargoShip();
	//Overloaded constructor that has inherited variables
	CargoShip(string n, string y, int t):Ship(n,y)
	{
		tonnage = t;
	}
	//Mutator that gets info  from user
	virtual void setInfo()
	{
		int t;
		cout <<"Please enter the cargo capacity: ";
		cin >>t;
		tonnage = t;
	}
	//Print info for this class
	virtual void print()
	{
		cout <<"Cargo Ship"<<endl;
		cout <<"Name: "<<getName()<<endl
			<<"Cargo Capacity: "<<tonnage<<endl;
	}
}; //end of CargoShip


int main()
{
	Ship *ships[SIZE] = {new Ship(), new CruiseShip(), new CargoShip()};
	int index; //Used with For loops
	for(index = 0; index < SIZE; index++)
	{
		*ships[index]->setInfo();
		cout <<endl;
	}

	for(index = 0; index < SIZE; index++)
	{
		*ships[index]->print();
		cout <<endl;
	}
	return 0;
}
Last edited on
You have too many "dereferences" on lines 112 and 118.

ships[index] is a Ship*.
Therefore *ship[index] is a Ship.
Ship does not overload the member operator (->).

Get rid of the initial *.

Also, you would be wise to add a virtual destructor in Ship.
Well it needs to be a array of Ship pointers where the array elements should be initialized with the address of dynamically allocated objects of the three classes.

I can understand the virtual destructor part
After playing around with it for a while I got it to work, but it's not quite as versatile as I originally wanted, but it works for its purpose. Thanks for the help.
Topic archived. No new replies allowed.