[英]No match for operators error
I just started to learn C++ 2 days ago. 我两天前才开始学习C ++。 I am just learning the basic syntax now. 我现在正在学习基本语法。 I just learned if statements, and i was trying to practice them. 我只是学习了if语句,并且正在尝试练习它们。 My problem is that at the "if(temp >= 60 && temp <= 70)" and "if(temp >= 75)" parts of the code, I get the error no match for 'operator' error. 我的问题是,在代码的“ if(temp> = 60 && temp <= 70)”和“ if(temp> = 75)”部分,出现的错误与“ operator”错误不匹配。 I tried looking this up. 我尝试查找。 people with similar problems are using code that i simply cannot follow. 有类似问题的人正在使用我根本无法遵循的代码。 I tried implementing their solutions, but i get even more error messages.If someone could please help, i would appreciate it, because i don't know anyone in real like that knows C++ or even anyone that codes. 我尝试实现他们的解决方案,但收到了更多错误消息。如果有人可以提供帮助,我将不胜感激,因为我不认识像C ++这样的人,甚至不知道任何编码人员。 Thanks in advance.Here is the code. 在此先感谢,代码在这里。 Sorry if its messy: 很抱歉,如果它凌乱:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//Get seed color
string seedColor = "";
cout << "Enter seed color (Red or Blue): \n";
cin >> seedColor;
//Get Temperature
string temp = "";
cout << "Enter the temperature (F): \n";
cin >> temp;
//Get soil moisture
string soilMoisture = "";
cout <<"Enter the soil moisture (wet or dry): \n";
cin >> soilMoisture;
//if red seed
if(seedColor == "red")
{
//if temp is >= 75
if(temp >= 75)
{
//if soil is wet
if(soilMoisture == "wet")
{
//get sunflower
cout << "A sunflower grew!";
}
//if soil is dry
else
{
//get dandelion
cout << "A dandelion grew!";
}
}
else
{
//otherwise
//get mushroom
cout << "A mushroom grew!";
}
}
//if blue seed
if(seedColor == "blue")
{
//if temp is between 60 and 70
if(temp >= 60 && temp <= 70)
{
//if soil is wet
if(soilMoisture == "wet")
{
//get dandelion
cout << "You grew a dandelion!";
}
else
{
//if soil is dry
cout << "You grew a sunflower!";
//get sunflower
}
}
else
{
//Otherwise
cout<< "You grew a mushroom!";
//Mushroom
}
} } }}
This line in your code causes the error: 您代码中的这一行会导致错误:
if(temp >= 75)
temp
is of type std::string
and 75 is considered a int
value. temp
类型为std::string
并且75被认为是int
值。 The compiler is right, there's no intrinsic operator 编译器正确,没有内在运算符
bool operator>=(const std::string&,int);
declared. 宣布。
You probably want to change your definition of the temp
variable to 您可能想将temp
变量的定义更改为
int temp = 0;
or 要么
double temp = 0.0;
Also you had some small formatting problems. 另外,您还有一些小的格式化问题。 See the fixed code . 请参阅固定代码 。
You cannot implicitly compare a std::string
with an int
, if you keep temp
as an int
then use std::stoi(temp)
to convert it to an int
and store this value somewhere. 您不能将std::string
与int
进行隐式比较,如果将temp
保持为int
则可以使用std::stoi(temp)
将其转换为int
并将此值存储在某个地方。 Or if you change it to a double
as the other answer suggests then use std::stod(temp)
to convert to a floating point number. 或者,如果您将其更改为其他答案建议的double
,则使用std::stod(temp)
转换为浮点数。 Eg: 例如:
std::string temp_str = "";
std::cin >> temp_str;
int temp = std::stoi(temp_str);
// then do comparions/arithmetic with temp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.