gpt4 book ai didi

c++ - 循环中的 "Ambiguous Overload"问题和故障 cin

转载 作者:行者123 更新时间:2023-11-28 06:02:10 25 4
gpt4 key购买 nike

我正在构建一个带有成员函数的菜单创建类,该类应该显示菜单、从用户那里获得选择、测试它是否是一个有效的菜单项并返回项目的编号。出于某种原因,编译器在下面的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com