2014-10-28 6 views
-4
#include <iostream> 
#include <string> 
using namespace std; 

class Ship 
{ 
     string shipName; 
     string shipYear; 
    public: 
     Ship(string n = "Titanic", string y = "1980") 
     { 
      shipName = n; 
      shipYear = y; 
     } 
     void setShipName(string n) { shipName = n; } 
     void setShipYear(string y) { shipYear = y; } 
     string getShipName() const { return shipName; } 
     string getShipYear() const { return shipYear; } 
     virtual void print() const {} 
}; 

class Cruiseship : public Ship 
{ 
     int numPass; 
    public: 
     Cruiseship(int numPass) : Ship("Hawaii-ish", "1948") 
     { this->numPass = numPass; } 
     void setNumPass(int num) { numPass = num; } 
     int getNumPass() const { return numPass; } 
     void print() const 
     { cout << this->getShipName() << this->getNumPass(); } 
}; 

class CargoShip : public Ship 
{ 
     int cargoCapacity; 
    public: 
     CargoShip(int cargoCapacity) : Ship("USBEB", "2050") 
     { this->cargoCapacity = cargoCapacity; } 
     void setCargoCapacity(int cc) { cargoCapacity = cc; } 
     int getCargoCapacity() const { return cargoCapacity; } 
     void print() const 
     { cout << this->getShipName() << this->getCargoCapacity(); } 
}; 

int main() 
{ 
    const int NUM_SHIPS = 3; 
    Ship *ships[NUM_SHIPS] = 
     { 
      new Ship, 
      new Cruiseship(2000), 
      new CargoShip(900) 
     }; 
    for (int i=0; i<3; i++) 
     cout << ships[i]->print(); 
    for (int i=0; i<3; i++) 
     delete ships[i]; 
} 

방향 :C++. "연산자에 대한 없음 일치"

Design a Ship class that has the following members:
--A member variable for the name of the ship (a string)
--A member variable for the year that the ship was built (a string)
--A constructor and appropriate accessors and mutators
--A virtual print function that displays the ship's name and the year it was built.

Design a CruiseShip class that is derived from the Ship class.
The CruiseShip class should have the following members:
--A member variable for the maximum number of passengers (an int)
--A constructor and appropriate accessors and mutators
--A print function that overrides the print function in the base class. The CruiseShip class's print function should display only the ship's name and the maximum number of passengers.

Design a CargoShip class that is derived from the Ship class.
The CargoShip class should have the following members:
--A member variable for the cargo capacity in tonnage (an int).
--A constructor and appropriate accessors and mutators.
--A print function that overrides the print function in the base class. The CargoShip class's print function should display only the ship's name and the ship's cargo capacity.

Demonstrate the classes in a program that has an array of Ship pointers. The array elements should be initialized with the addresses of dynamically allocated Ship, CruiseShip, and CargoShip objects.

이 내가 지금까지 무엇을하지 않습니다. 임지고 오류 :(

+1

어떤 오류가 있습니까? 어떻게 컴파일합니까? – Deduplicator

+0

@Deduplicator 메인에 cout << ships[i]-> 인쇄(); – user3453084

+3

'cout << ships[i]-> print()'이게 뭐가 잘못 됐는지 알아낼 수있을거야. 선언문과 본문을보세요. – Radiodef

답변

0

변경 :

for (int i=0; i<3; i++) 
    cout << ships[i]->print(); 

에 :?

당신은 ... 당신은 오류 메시지를 게시 할 수 컴파일러를 사용하는 컴파일러 오류를 이해할 필요가
for (int i=0; i<3; i++) 
    ships[i]->print(); 

내 컴파일러의 상태 :

main.cpp: In function int main(): main.cpp:56:14: error: no match for operator<< (operand types are std::ostream {aka std::basic_ostream} and void) cout << ships[i]->print();

이것은 coutchar의 스트림을 기다리고 있지만 void을 얻고 있다는 것을 의미합니다. print에 잠깐 살펴보고 문제를 잡습니다. 희망이 도움이됩니다!

관련 문제