简体   繁体   English

C++:将向量传递给 function 然后在 main 中调用 function。 遗漏了什么

[英]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;
        }
    } 

}

The issue I am having is when I compile, it is saying 'no operator matches "<<" these operands.我遇到的问题是当我编译时,它说'没有运算符匹配“<<”这些操作数。 Standard operands are: std::ostream << void'标准操作数是:std::ostream << void'

I am not sure I understand.我不确定我是否理解。 I am passing the vector string and the vector size to void getAnswers to get the randomize for the answers.我将向量字符串和向量大小传递给 void getAnswers 以获得答案的随机化。 What am I missing?我错过了什么?

Any help is greatly appreciated.任何帮助是极大的赞赏。

void getAnswer(std::vector<std::string> &answers, int nAnswers)

The void return "type" states explicitly that this function returns nothing so you cannot then go and attempt to use that return value in an expression: void返回“类型”明确指出此 function 不返回任何内容,因此您不能然后 go 并尝试在表达式中使用该返回值:

std::cout << getAnswer(answers, answers.size()) << std::endl;

Assuming that you will eventually return a random answer from your list of answers (based on code to date), the first thing you should do is dummy up a reurn value:假设您最终会从您的答案列表中返回一个随机答案(基于迄今为止的代码),您应该做的第一件事是模拟一个 reurn 值:

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];
}

Then you can later adapt it to provide a random one.然后您可以稍后对其进行调整以提供随机的。 I could have just provided the complete function but, since this is probably educational work, you'll learn a lot more by doing this yourself (this answer is just to get you over your specific problem).我本可以提供完整的 function 但是,由于这可能是教育工作,因此您自己会学到很多东西(这个答案只是为了让您解决具体问题)。

I have included some sample code at the bottom for you to look over (and for future readers who may not be doing the classwork) but I urge you to try yourself first.底部包含了一些示例代码供您查看(以及可能不做课堂作业的未来读者),但我敦促您先尝试一下。


As an aside, you should also be aware that you have some rather superfluous lines in your code, specifically:顺便说一句,您还应该意识到您的代码中有一些相当多余的行,特别是:

answers.size();
getAnswer(answers, answers.size());
getAnswer;

None of these do anything useful, they simply evaluate the expression and throw away the result.这些都没有做任何有用的事情,它们只是评估表达式并丢弃结果。

I'm also not why you attempt to create a second vector from the original, especially since you don't use it anywhere:我也不是您尝试从原始向量创建第二个向量的原因,尤其是因为您不在任何地方使用它:

std::vector<string> qAnswers(answers);

As mentioned earlier, my sample code follows.如前所述,我的示例代码如下。 Please do not use it verbatim if you value your marks (or integrity) - any educator worth their salt will be using plagiarism detection tools:如果您重视自己的分数(或正直),请不要逐字使用它 - 任何称职的教育工作者都会使用抄袭检测工具:

#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.\n\n";
            break;
        }

        // Answer any non-blank question.

        if (! questionAsked.empty()) {
            std::cout << getAnswer(answers) << '\n';
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM