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.
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?
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 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
virtualvoid 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
virtualvoid 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
virtualvoid Info()
{
cout << "The ship's name is: " << getName() << endl;
cout << "The ship's cargo capacity (in tons) is: " << getCapacity() << endl;
}
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. :)