C++:将向量传递给函数,然后在main中调用函数.错过了什么
C++: Passing vector to function and then calling function in main. Missing something
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
using namespace std;
void getAnswer(std::vector<std::string> &answers, int nAnswers)
{
int index = rand() % nAnswers;
}
int main()
{
std::vector<string> answers;
answers.push_back("Most Certainly");
answers.push_back("Absolutely");
answers.push_back("Yes");
answers.push_back("You Can Bet On It");
answers.push_back("Odds look good");
answers.push_back("Let's talk about that some other time");
answers.push_back("Odds don't look so good");
answers.push_back("I think you know the answer to that question");
answers.push_back("I don't think I'm qualified to answer that question");
answers.push_back("Absolutely Not");
answers.push_back("I Don't Think So");
answers.push_back("Um...no");
std::vector<string> qAnswers(answers);
answers.size();
string questionAsked;
bool pgExit = false;
srand((unsigned int)time(NULL));
cout << "nWelcome to the Magic 8Ball.n";
cout << "nAsk a question and I will predict the answer!n" << endl;
//loop and ask the user to enter a question or enter "x" to stop
while (!pgExit) {
cout << "What is your question? (Type question or Enter 'x' to exit) " << endl;
//use getline to get the question
getline(cin, questionAsked);
//call getAnswer with your array and number of possible answers to get an answer
getAnswer(answers, answers.size());
//output the answer
if (questionAsked.compare("x") == 0)
{
cout << "Maybe next time. Have a good day.";
pgExit = true;
}
if (questionAsked.compare("") != 0 && questionAsked.compare("x") != 0)
{
getAnswer;
std::cout << getAnswer(answers, answers.size()) << std::endl;
}
}
}
我遇到的问题是,当我编译时,它说"没有运算符匹配"<<"这些操作数。标准操作数为:std::ostream<lt;无效的
我不确定我是否理解。我将向量字符串和向量大小传递给void getAnswers,以获得答案的随机化。我错过了什么?
非常感谢您的帮助。
void getAnswer(std::vector<std::string> &answers, int nAnswers)
void
返回的"类型"明确表示此函数不返回任何值,因此您不能继续尝试在表达式中使用该返回值:
std::cout << getAnswer(answers, answers.size()) << std::endl;
假设你最终会从你的答案列表中返回一个随机答案(基于迄今为止的代码(,你应该做的第一件事情就是伪造一个重新生成的值:
std::string getAnswer(std::vector<std::string> &answers, int nAnswers)
{
// Just return last one for now (make sure you
// have at least one in the collection).
return answers[nAnswers - 1];
}
然后您可以稍后对其进行调整,以提供一个随机的。我本可以提供完整的功能,但由于这可能是一项教育工作,你可以通过自己做这件事学到更多(这个答案只是为了让你克服特定的问题(。
我在底部包含了一些示例代码,供您查看(以及可能没有做课堂作业的未来读者(,但我敦促您先尝试一下。
顺便说一句,您还应该意识到您的代码中有一些相当多余的行,特别是:
answers.size();
getAnswer(answers, answers.size());
getAnswer;
这些都没有任何用处,它们只是评估表达式并丢弃结果。
我也不明白你为什么要尝试从原始向量创建第二个向量,尤其是因为你在任何地方都不使用它:
std::vector<string> qAnswers(answers);
如前所述,我的示例代码如下。如果你重视自己的分数(或诚信(,请不要逐字逐句地使用它-任何称职的教育工作者都会使用剽窃检测工具:
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <vector>
// Anon namespace to hide this in translation unit.
namespace {
// No need to pass in size, vector has this.
const std::string &getAnswer(const std::vector<std::string>answers) {
return answers[std::rand() % answers.size()];
}
};
int main() {
srand(static_cast<unsigned int>(time(nullptr)));
// Can be const if initialised rather than each entry pushed.
const std::vector<std::string> answers = {
"Most Certainly", "Absolutely", "Yes", "You Can Bet On It",
"Odds look good", "Let's talk about that some other time",
"Odds don't look so good",
"I think you know the answer to that question",
"I don't think I'm qualified to answer that question",
"Absolutely Not", "I Don't Think So", "Um...no",
};
std::cout << "nWelcome to the Magic 8Ball.n";
std::cout << "nAsk a question and I will predict the answer!n";
// Infinite loop, use break to exit.
while (true) {
// Ask and get response.
std::cout << "nWhat is your question (x to exit)? " << std::endl;
std::string questionAsked;
std::getline(std::cin, questionAsked);
// Exit loop if told so.
if (questionAsked == "x") {
std::cout << "Maybe next time. Have a good day.nn";
break;
}
// Answer any non-blank question.
if (! questionAsked.empty()) {
std::cout << getAnswer(answers) << 'n';
}
}
}
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 代码在main()中运行,但在函数中出现错误
- 函数是否可以访问传递给main()的参数
- C++变量名(可以将 main 声明为变量,但对于其他函数名称则不然)
- 当我尝试在 Main 中调用插入函数时,它不是取数字?
- 为什么类和 main() 函数中也有动态内存分配
- "main"函数堆栈中的对象在第一个任务运行时被覆盖 (FreeRTOS)
- 为什么 'main' 函数中的局部 int 变量会自动初始化?
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- 如何在 C 中使用空的 main() 方法运行函数?
- 我在主函数的左括号上不断收到错误,消息为obj\Debug\main.o||在函数"ZN11linked_listC1Ev"中:|
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- 未解析的外部符号__imp_glewInit函数 main 中引用
- 函数main中的参数argv(int argc,char *argv [])
- 可以(通过编译器)使用多少个线程来初始化全局对象(函数main之前)
- 为什么我的编码出现错误无法在函数 main 中将 int 转换为 int *
- C/C++中函数main()的地址
- 无法在函数 main() 中创建抽象类的指针
- 无法在函数main中编译,需要Lvalue
- 如何在 c++ 中获取主函数"main()"的返回值?