'No operator matches these operands' Error.
Apr 10, 2015 at 1:13pm UTC
Hello.
I am currently making an application which requires me to create a port docking system using the OOP approach (classes ect). As you can see from my header files I have used an array to create this dock (rows and spaces) and am currently trying to input string data into the array. Everything runs fine except for on line 95 (commented in bold below) I am unable to output my array elements to the console due to this error:
"IntelliSense: no operator "<<" matches these operands
operand types are: std::ostream << ship"
I have spent hours and hours trying to find a solution for this however I have been unsuccessful. Can anyone advise on this problem?
All code is below.
Many thanks.
dock 1.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#ifndef DOCK_1_H
#define DOCK_1_H
#include <iostream>
#include <string>
#include "ship.h"
class row
{
public :
ship small_row[5];
ship med_row[3];
ship large_row[2];
};
class dock
{
public :
row dock_rows [10];
};
#endif
ship.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#ifndef SHIP_H
#define SHIP_H
#include <iostream>
#include <string>
class ship
{
public :
std::string ship_name;
std::string ship_size;
};
#endif
main.cpp
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
#include <iostream>
#include <string>
#include "ship.h"
#include "dock 1.h"
using namespace std;
//dock key
//small ship = A
//medium ship = B
//large ship = C
int main()
{
//dock 1
dock myDock1;
//dock 2
dock myDock2;
//dock 3
dock myDock3;
//main menu
cout << "Welcome to the port docking system." << endl;
cout << "Please enter your choice below." << endl;
//variable for the user input
char userChoice;
string ship_Name;
string ship_Size;
//menu structure
cout << "Main Menu:" << endl;
cout << "***********************************" << endl;
cout << "Option 1 - Dock a ship" << endl;
cout << "Option 2 - Undock a ship" << endl;
cout << "Option 3 - View port status list" << endl;
cout << "Option 4 - View port waiting list" << endl;
cout << "Option 5 - Close program" << endl;
cout << "***********************************" << endl;
cout << "Please select one of the options above:" ;
// read the user input
cin >> userChoice;
//switch case statement for main menu
switch (userChoice)
{
//option 1
case '1' : { cout << "You have chosen 'Dock a ship'." << endl;
//creation of a new ship
ship newShip;
cout << "Please enter the ship name:" ;
//user inputs ship name
cin >> newShip.ship_name;
cout << "Please enter the ship size from the list below:" << endl;
cout << " Cargo" << endl;
cout << " Container" << endl;
cout << " Super" << endl;
//user inputs ship size
cin >> newShip.ship_size;
cout << endl;
//loops for identifing the ship size entered by user
if (newShip.ship_size == "Cargo" || newShip.ship_size == "cargo" )
{
cout << "You have chosen the size 'Cargo ship'" << endl;
cout << newShip.ship_name << endl;
cout << newShip.ship_size << endl;
}
if (newShip.ship_size == "Container" || newShip.ship_size == "container" )
{
cout << "You have chosen the size 'Container ship'" << endl;
cout << newShip.ship_name << endl;
cout << newShip.ship_size << endl;
for (int i = 0; i < 5; i++)
{
cout << myDock1.dock_rows[0].small_row[0] << endl; //<------ ERROR IS ON THIS LINE HERE
}
}
if (newShip.ship_size == "Super" || newShip.ship_size == "super" )
{
cout << "You have chosen the size 'Super container ship'" << endl;
cout << newShip.ship_name << endl;
cout << newShip.ship_size << endl;
}
cout << "Thankyou, this ship has been docked." << endl;
cin.get();
}
cin.get();
return 0;
}
Apr 10, 2015 at 1:18pm UTC
Your
ship
is a class you made. The compiler has no idea what you want to happen when you output it, because you have not told it. Look up information on how to overload the
stream insertion operator . It should look something like this:
1 2 3 4
std::ostream &operator <<(std::ostream &o, ship const &s)
{
return o << "Ship name is " << s.ship_name << " and size is " << s.ship_size;
}
Last edited on Apr 10, 2015 at 1:18pm UTC
Apr 10, 2015 at 1:21pm UTC
Thank you for the reply.
I will have a look into this further.
Many thanks.
Apr 10, 2015 at 1:22pm UTC
Try creating the ship outside of the switch statement, above it instead.
1 2
ship newShip;
switch (userChoice)
Edit: I took a few minutes before I pressed submit didint see the others answers, nvm t his
Last edited on Apr 10, 2015 at 1:27pm UTC
Apr 10, 2015 at 1:26pm UTC
@TarikNeaj: the error message does not mention an undefined variable, and I'm not sure what that change would do to help?
Topic archived. No new replies allowed.