C++:将向量传递给函数,然后在main中调用函数.错过了什么

C++: Passing vector to function and then calling function in main. Missing something

本文关键字:函数 main 调用 什么 错过了 然后 向量 C++      更新时间:2023-10-16
#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';
}
}
}