Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cin >> "no operator matches these operands"

Tags:

c++

cin

I've been working on a C++ project in visual studio 2012 console mode and I keep getting this strange persistent error with the cin function.

Under the >> I get a red line and the program tells me no operator matches these operands. I have initialized all the array elements in a separate method.

Here's a snippet example (The actual code contains many more variables):

for (int i = 0; i < 14; i++)
{
    cout << "For taxi: " << i+1 << "Please input the taxi rank they are currently parked at, if at all ('Train Station', 'South Walls' or 'Chell Road')" << endl;
    cin >> allTaxiDetails[i].taxiRank;
}

allTaxiDetails is an array, of data type "taxiDetails" which is this structure:

struct taxiDetails { 
    string taxiDriverSurname; 
    int taxiID; 
    int taxiCoordinates; 
    int numberOfSeats; 
    bool taxiContainsCustomerYesNo; 
    bool WheelChairAccessibleVehicle; 
    string taxiRank; 
    fareDetails fareDetailsForTaxi; 
    bool taxiAvaliable; 
};
like image 955
user3163612 Avatar asked Jan 09 '14 20:01

user3163612


3 Answers

Issue is saying that string doesn't have the operator>> method, but it does...

  1. Did you forget #include <string> at the top of that file?
  2. Maybe try using getline(std::cin, allTaxiDetails[I].taxiRank, '\n');.
  3. Define operator>> for your struct.
like image 81
Keeler Avatar answered Oct 19 '22 22:10

Keeler


It seems you are trying to read an object of type std::string for which you somehow obtained the class definition (e.g. by including <iostream>) but you didn't get all the necessary operations! Make sure you are including <string>:

#include <string>
// ...

It is quite common that some headers provide definitions of some other classes but actually don't use the entire header to get the definitions. Since std::string is in some places needed to declare IOStreams it is fairly likely that it will be defined but probably not by including its full header.

like image 28
Dietmar Kühl Avatar answered Oct 19 '22 21:10

Dietmar Kühl


I believe forget #include < string > this critical header is the error source. It happened to me as a beginner of C++.

like image 7
WayneX Avatar answered Oct 19 '22 22:10

WayneX