C++ cin >> "没有运算符匹配这些操作数"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21030214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cin >> "no operator matches these operands"
提问by user3163612
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.
我一直在 Visual Studio 2012 控制台模式下处理一个 C++ 项目,并且我一直在使用该cin
函数遇到这个奇怪的持续错误。
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.
在>>
I 下有一条红线,程序告诉我no operator matches these operands
。我已经在一个单独的方法中初始化了所有的数组元素。
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:
allTaxiDetails
是一个数组,数据类型为“ taxiDetails
”,它是这个结构:
struct taxiDetails {
string taxiDriverSurname;
int taxiID;
int taxiCoordinates;
int numberOfSeats;
bool taxiContainsCustomerYesNo;
bool WheelChairAccessibleVehicle;
string taxiRank;
fareDetails fareDetailsForTaxi;
bool taxiAvaliable;
};
回答by Keeler
Issue is saying that string
doesn't have the operator>>
method, but it does...
问题是说string
没有operator>>
方法,但它确实......
- Did you forget
#include <string>
at the top of that file? - Maybe try using
getline(std::cin, allTaxiDetails[I].taxiRank, '\n');
. - Define
operator>>
for your struct.
- 你忘记
#include <string>
在那个文件的顶部了吗? - 也许尝试使用
getline(std::cin, allTaxiDetails[I].taxiRank, '\n');
. operator>>
为您的结构定义。
回答by Dietmar Kühl
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>
:
似乎您正在尝试读取一个类型的对象std::string
,您以某种方式获得了类定义(例如,通过包含<iostream>
),但您没有获得所有必要的操作!确保您包括<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.
一些头文件提供了一些其他类的定义,但实际上并不使用整个头文件来获取定义,这是很常见的。由于std::string
在某些地方需要声明 IOStreams,因此很可能会定义它,但可能不会通过包含其完整标头来定义。
回答by WayneX
I believe forget #include < string >this critical header is the error source. It happened to me as a beginner of C++.
我相信忘记 #include <string>这个关键的头是错误源。作为 C++ 的初学者,它发生在我身上。