user3163612
user3163612

Reputation: 149

cin >> "no operator matches these operands"

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; 
};

Upvotes: 14

Views: 61566

Answers (3)

WayneX
WayneX

Reputation: 79

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

Upvotes: 7

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153830

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.

Upvotes: 10

Keeler
Keeler

Reputation: 2150

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.

Upvotes: 31

Related Questions