- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个带有成员函数的菜单创建类,该类应该显示菜单、从用户那里获得选择、测试它是否是一个有效的菜单项并返回项目的编号。出于某种原因,编译器在下面的 run() 成员函数中的一个简单 cin 语句上给我一个“运算符 '>>' 的模糊重载”错误。运行时,该函数会正确捕获无效输入,但随后会考虑无效输入之后的所有输入。如果第一个输入是正确的,程序将立即终止。这是我的代码:
#include <iostream>
#include <vector>
using namespace std;
class NumericalMenu {
private:
string prompt;
vector<string> options;
string canceltext;
string errortext;
bool repeatprompt;
int sel;
public:
NumericalMenu() {
prompt = "Choose an option:";
canceltext = "Cancel";
errortext = "Error!";
repeatprompt = true;
sel = 0;
};
void setPrompt(string text) {
prompt = text;
};
int size() const {
int size = options.size() + 1;
return size;
};
int addOption(string text) {
options.push_back(text);
int position = options.size() - 1;
return position;
};
void setCancelText(string text) {
canceltext = text;
};
void setRepeatPromptOnError(bool repeat) {
repeatprompt = repeat;
};
void setErrorText(string text) {
errortext = text;
};
int run() const{
cout << prompt << "\n\n";
for (unsigned i=0; i<options.size(); i++) {
cout << i+1 << " - " << options[i] << "\n";
}
int errorpos = this->size();
cout << errorpos << " - " << canceltext << "\n\n";
cin.clear();
cin.ignore();
cin >> sel;
if(cin.fail() || sel<=0 || sel>errorpos) {
cout << "\n" << errortext << "\n\n";
if(repeatprompt == true) {
cin.clear();
cin.ignore();
this->run();
}
}
if (sel == errorpos) {
return -1;
} else {
return sel;
}
};
};
int main() {
NumericalMenu menu;
menu.setPrompt("Choose an option:");
menu.addOption("Enter new values");
menu.addOption("Help");
menu.addOption("Save");
menu.setCancelText("Exit");
menu.run();
}
编辑:知道了!感谢大家。工作标题:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class NumericalMenu {
private:
string prompt;
vector<string> options;
string canceltext;
string errortext;
bool repeatprompt;
public:
NumericalMenu() {
prompt = "Choose an option:";
canceltext = "Cancel";
errortext = "Error!";
repeatprompt = true;
};
void setPrompt(string text) {
prompt = text;
};
int size() const{
int size = options.size() + 1;
return size;
};
int addOption(string text) {
options.push_back(text);
int position = options.size() - 1;
return position;
};
void setCancelText(string text) {
canceltext = text;
};
void setRepeatPromptOnError(bool repeat) {
repeatprompt = repeat;
};
void setErrorText(string text) {
errortext = text;
};
int run() const{
cout << prompt << "\n\n";
for (unsigned i=0; i<options.size(); i++) {
cout << i+1 << " - " << options[i] << "\n";
}
int errorpos = this->size();
cout << errorpos << " - " << canceltext << "\n\n";
int sel;
cin.clear();
cin >> sel;
if(cin.fail() || sel<=0 || sel>errorpos) {
cout << "\n" << errortext << "\n\n";
if(repeatprompt == true) {
cin.clear();
cin.ignore(1000, '\n');
int sele = this->run();
return sele;
}
}
if (sel == this->size()) {
return -1;
}
else {
return sel;
}
};
};
最佳答案
您声明了您的 run()
用作const
并防止对成员变量的修改由编译器强制执行。
class NumericalMenu {
private:
int sel;
...
int run() const {
cin >> sel; // Not allowed
如果需要修改成员变量,去掉const
在你的内联 run()
函数定义。
此外,作为一种良好做法,请尝试在代码中包含您直接使用的所有 header (即 <string>
)。
关于c++ - 循环中的 "Ambiguous Overload"问题和故障 cin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33048134/
我使用 cin.peek() 方法获得了这段代码。我注意到奇怪的行为,当程序输入看起来像 qwertyu$[Enter] 时,一切正常,但是当它看起来像 qwerty[Enter]$ 时,它仅在我键入
#include #include using namespace std; int main(){ ofstream fout("student.dat",ios::out); char name[
我是 C++ 编程新手,遇到了障碍。这是我的代码: #include using namespace std; int main(){ int sum = 0, a; cout >
我有一个简单的程序,当输入完全符合预期时,它名义上可以工作。 #include using namespace std; int main () { int a, b; char a
int main () { int num1, num2; int cnt = 1; if (cin >> num1){ while (cin >> num2){
我有这个程序,但 cin 随机跳过..我的意思是有时它会,有时它不会。有什么解决办法吗? int main(){ /** get course name, number of
我是一名正在准备期末考试的 C++ 初学者。我用两种方式写了一个程序。第一个代码使用 cin.getline() 并且不能正常工作。第二个代码使用 cin.get() 和 cin >> 并正确执行所有
据我所知,在 cin 之后使用 getline() 时,我们需要先刷新缓冲区中的换行符,然后再调用 getline(),我们通过调用 cin.ignore() 来完成此操作。 std::string
我正在尝试以下代码: int main() { char str1[20]; int a; cout > a; cout > 忽略空格(即 ' ', '\t', '\n
我刚开始阅读 C++ 11 入门书,目前在 if 语句部分。我以前有使用 Java 的经验,但 C++ 输入/输出确实让我感到困惑。为了方便起见,我将多个练习放在一个文件中。但是,似乎自从我调用了 w
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: c++ cin input not working? 我一直在尝试使用以下 C++ 代码在整数后输入字符串。
需要 I/O 重定向,我不能使用 fstream。我用 I/O通过使用 ">line)通过 I/O 读取文件重定向。然后我需要 cin>>x ,但这次是在运行时进行用户输入,但会被跳过。 我试过了 c
我正在尝试使用 getline() 但在输入第一个记录光标后不要等待第二个国家/地区名称,它会跳过并跳转到首都名称。我的错误在哪里。如果我输入 国家:印度 首都:德里 Capita:57382 它可以
我有以下循环。它应该读取数字直到 EndOfFile,或者用户输入 -999 int arr[100]; int index; for (index = 0; index > arr[index];
以下两个循环和每个循环什么时候停止有什么区别? #include #include #include using namespace std; int main() { int x,y;
在这个程序的每次输入中,我希望用户能够输入他们想要的任意数量的“单词”(意思是用空格/制表符分隔的文本)......但是不允许用户输入单词超出当前 cin。例如:如果我键入 John Smith Do
int main() { int oddeven = 1; int modulotwo; // Simple loop since no reason to check 0
我写了一个 switch 语句,并创建了一个默认值,它会简单地说用户选择了一个错误的选项并重复输入。我想确保如果出现问题,它会首先清除缓冲区,所以我使用了 cin.sync(),但输入 'a' 仍然会
我是一个新手程序员,正在练习下面的这个程序。无论出于何种原因,在我的程序结束时,cin.ignore() 被编译器完全跳过并直接移动到 cin.get(),但是之前已经有一个按键,所以编译器完全跳过它
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我是一名优秀的程序员,十分优秀!