gpt4 book ai didi

c++ - 在类中使用 vector 创建类

转载 作者:行者123 更新时间:2023-11-28 07:13:57 25 4
gpt4 key购买 nike

我有一个包含 vector if strings 的类,因此我可以获得无限的答案(我使用它来通过本质上进行模拟测试来学习)。但是,当我创建 thingy 时,它会在尝试在 vector 中包含值时生我的气。我已经尝试了很多方法让它工作,但它不能。

#include <iostream>
#include <vector>

using namespace std;

string input;

class answer {
public:
vector<string> answers;
};

class qANDa {
public:
string question;
answer answers;
string correct;
};

void askQuestion (qANDa bob) {
cout << bob.question;
getline(cin, input);
input[0] = tolower(input[0]);
if (input == bob.correct) {
cout << "Correct!\n";
} else {
cout <<"Incorrect. Study more. Loser.\n";
};
}

vector<qANDa> thingys;

int main(int argc, const char * argv[]) {

qANDa thingy = {"The correct answer is \"A\". What's the correct answer.", {} "A"}

askQuestion(thingys.at(0));
}

我试过将字符串放在方括号内,我试过在方括号内使用圆括号,我试过将字符串放在方括号内的圆括号内,但都不起作用。

最佳答案

您的类 answer 无法仅从空括号 {} 初始化,但您可以提供默认构造的右值引用:

qANDa thingy = 
{ "The correct answer is \"A\". What's the correct answer."
, answer()
, "A" }

另请注意,在您调用时

 askQuestion(thingys.at(0));

thingys 不包含任何元素。将其更改为

 qANDa thingy = 
{ "The correct answer is \"A\". What's the correct answer."
, answer()
, "A"};
thingys.push_back(thingy);
askQuestion(thingys.at(0));

关于c++ - 在类中使用 vector 创建类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20532998/

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