我在做什么错了这个C++代码?
问题描述:
对不起,忘记了代码我在做什么错了这个C++代码?
这是不是的代码。 我一直试图让这个工作,但所有的逻辑运算符不起作用。
#include <iostream>
#include <string>
using namespace std;
string repeat;
string repeatnum;
string prompt = "|-[]->";
int main()
{
string entry;
bool Running = true;
while(Running == true)
{
cout << "\n";
cout << prompt;
cin >> entry;
if(entry == "Exit") return 0;
if(entry == "Help") cout << "HELP:\nThsi is a simple program, try an input";
if(entry == "ChangePrompt")
{
cout << "What do you want to change the prompt to?: ";
cin >> prompt;
}
if(entry == "Repeat" || "repeat")
{
cout << "What string do you want to repeat?: ";
cin >> repeat;
cout << "How many times do you want to repeat" << repeat << "(1-9)?: ";
cin >> repeatnum;
if(repeatnum > 0){}
}
}
char f;
cin >> f;
return 0;
}
这是我得到的错误。
Error:
C:\Users\Packard Bell\Desktop\test\main.cpp||In function 'int main()':|
C:\Users\Packard Bell\Desktop\test\main.cpp|29|error: no match for 'operator>' in 'repeatnum > 0'|
||=== Build finished: 1 errors, 0 warnings ===|
答
因为在main.cpp
29行,你试图做repeatnum > 0
和repeatnum
是没有超载operator >
一个类型。
+0
我不明白,你能不能解释一下? – Dasttann777 2012-07-10 10:35:03
答
现在看到代码后。 repeatnum是一个字符串。您读取字符串的输入,然后将其与整数进行比较。现在字符串没有运算符> - 定义为整数,因此您需要在比较之前将该字符串转换为整数。
atoi(repeatnum.c_str());
或者使用stringstream来做到这一点。
答
从给定的信息,我只能猜测变量repeatnum
是一个类或结构的对象,你不能用它直接与0
比较。如果repeatnum
的类型由您定义,请添加一个重载operator >
的成员函数并正确处理它。
class YourType
{
// Class definition
public:
int operator >(int var)
{
// Code for comparison
// return result
}
};
答
除了repeatnum问题,这段代码是不是做你想做
if(entry == "Repeat" || "repeat")
应该
if(entry == "Repeat" || entry == "repeat")
你没有张贴任何代码是什么。 – 2012-07-10 05:54:57
代码在哪里? – Naveen 2012-07-10 05:55:11
看来'repeatnum'不是一个数字。 – 2012-07-10 05:56:07