Virtual Print function and base class pointer array

I'm currently trying to get a series of classes to output certain information using a virtual print function that reads out an array of base class pointers. I'm having a problem with the pointer array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{

	Ship shMinnow("Minnow", "1964"), shQueenAnne("Queen Anne's Revenge", "1710");
	Cruiseship crMajesty("Majesty of the Seas", 2744), crDisney("Disney Magic", 2400);
	Cargoship caColombo("Colombo Express", 93750), caSavannah("Savannah Express", 107000);

	Ship *boats[6] = { &shMinnow, &shQueenAnne, &crMajesty, &crDisney, &caColombo, &caSavannah };

	for (int count = 0; count < 6; count++)
	{
		cout << *boats[count]->Info() << endl;
	}

	return 0;
}


In the line cout << *boats[count]->Info() << endl; VS is highlighting the boats pointer and giving me errors saying 'the operand of * must be a pointer', which, based on the examples I've been told to follow, it should already be. I'm also getting errors saying 'boats is an undeclared identifier' and 'left of -> Info() must point to a class/struct/union/generic type'

In the example I was instructed to follow, the pointer is constructed the same way as I have constructed mine, so I'm just sort of stumped as to what the problem could be. Does anyone have any insight?
You can write it either so

cout << boats[count]->Info() << endl; // Note: no *

or so

cout << *boats[count].Info() << endl; // Note: . instead of ->

The reason is that in this case the * dereferences the pointer (means it's not longer a pointer but an object)
Thanks a lot! :)
But now it's giving me an error for the symbol (<<) right before boats, saying no operator matches the operands.

I also tried it with . instead of -> and that still got me an error saying boats has to have a class type. I thought the type was Ship, since that's the class I declared the pointer with.
The code that uses . instead of -> should have been
cout << (*boats[count]).Info() << endl; // Note: parentheses needed

What is the return type of Info()?
The return type for Info() is void. It has to output string, or string and int if it's reading out the info for Cargoship or Cruiseship. The Ship class only returns to string objects (the name of the ship and year it was built.

Here are the implementations for the virtual Info() objects.

Ship.h
1
2
3
4
5
virtual void Info()
	{
		cout << "The ship's name is: " << getName() << endl;
		cout << "The year the ship was built: " << getYear() << endl;
	}


Cruiseship.h
1
2
3
4
5
virtual void Info()
	{
		cout << "The ship's name is: " << getName() << endl;
		cout << "The ship's passenger capacity is: " << getPassengers() << endl;
	}


Cargoship.h
1
2
3
4
5
virtual void Info()
	{
		cout << "The ship's name is: " << getName() << endl;
		cout << "The ship's cargo capacity (in tons) is: " << getCapacity() << endl;
	}
Last edited on
Okay, scratch my last comment. I realized my problem was the void return type, and that I need Info() to return a value.

The thing is, two of the virtual print functions have to have two different types of output each, a string and an int, and I'm not sure how to output both from the same function, other than casting the int into a string. I'm not really sure if that falls outside the boundaries of my assignment.
If Info() is doing the printing you don't need to use cout when calling the function. Just call it as boats[count]->Info(); and let it return void as you have it.
That did it! Program runs perfectly now. Big thanks to you guys, Peter and coder! Also cire and helios, who answered a couple questions I had yesterday. :)
Topic archived. No new replies allowed.