(70,58):错误c2440:''>>':无法转换为初始化器列表'到Diceset'
新手在这里。我正在尝试获取一个对象向量,该对象由一个带有两个整数和一个向量的类定义,以接受我对新数据的推动,但是我所做的任何事情都会抛出此例外,很快我将任何内容都放入向量推动中。
我试图在整个网上进行搜索,但无法找到似乎有效的解决方案,也可以理解它的工作原理。 任何帮助将不胜感激!我试图尽我所能解释代码。
我正在使用C ++ 20的Visual Studio 22
class Die{
public:
int sides{};
std::vector<int> alleyes;
};
class Diceset : public Die {
public:
int amount{};
};
void func_getdicesets() {
// Get the user input example 2d{2,5,7,8} an count the number of d´s in the string
std::cin >> userinput;
int64_t dcount = std::count(userinput.begin(), userinput.end(), 'd');
// Initialize the vector for the class Diceset dicesets and set the amount of d´s as reserve
std::vector<Diceset> dicesets;
dicesets.reserve(dcount);
// For the amount of d´s (dice sets) found in the input, break them up into diceset objects
for (int i = 0; i < dcount; i++) {
// Find the next d and open braket
int64_t nextdindex = userinput.find("d");
int64_t braketindex = userinput.find("{");
// If a braket comes after the next d, it signals a special dice and it will be handeld by this
if (braketindex - nextdindex == 1) {
// Fill the dice amount of the set into amount and remove the numbers and d from the string
int dicethrown = std::stoi(userinput.substr(0, nextdindex));
userinput = userinput.erase(0, nextdindex + 1);
// Set the amount of sides a single die has and filter the eyes on each side into a vector
int64_t operatorindex = userinput.find_first_of("+-");
std::string specialdiesides;
std::vector<int> eyes;
// If no +/- has been found length is the whole string, else the index of the next operator
if (operatorindex >= 1) {
specialdiesides = userinput.substr(1, operatorindex - 2);
}
else {
specialdiesides = userinput.substr(1, userinput.length() - 2);
}
// The amount of sides a die has, is calculated by rounding the special die input up
int diesides = lround(specialdiesides.length() / 2);
// This gets set as reserve for eyes vector
eyes.reserve(diesides);
// After which the eyes on the die get filterd into the eyes vector
for (i = 0; i <= diesides * 2; i += 2) {
eyes.push_back(int(std::stoi(specialdiesides.substr(i, 1))));
}
// The amount of dice thrown, die sides and vector of all eyes on a die get pushed into the objects vector dicesets of Diceset
dicesets.push_back(Diceset(diesides, eyes, dicethrown)); // The error shows here
}
}
}
newbie here. I'm trying to get a vector of objects, that is defined by a class with two integers and one vector, to accept my push of new data but anything I do throws this exception as soon I place anything into the vector push.
I tried to searched all over the net but can`t find a solution that seems to work or that I understand how it works.
Any help would be much appreciated! I tried to explain the code to the best of my abilities.
I´m running visual studio 22 with c++ 20
class Die{
public:
int sides{};
std::vector<int> alleyes;
};
class Diceset : public Die {
public:
int amount{};
};
void func_getdicesets() {
// Get the user input example 2d{2,5,7,8} an count the number of d´s in the string
std::cin >> userinput;
int64_t dcount = std::count(userinput.begin(), userinput.end(), 'd');
// Initialize the vector for the class Diceset dicesets and set the amount of d´s as reserve
std::vector<Diceset> dicesets;
dicesets.reserve(dcount);
// For the amount of d´s (dice sets) found in the input, break them up into diceset objects
for (int i = 0; i < dcount; i++) {
// Find the next d and open braket
int64_t nextdindex = userinput.find("d");
int64_t braketindex = userinput.find("{");
// If a braket comes after the next d, it signals a special dice and it will be handeld by this
if (braketindex - nextdindex == 1) {
// Fill the dice amount of the set into amount and remove the numbers and d from the string
int dicethrown = std::stoi(userinput.substr(0, nextdindex));
userinput = userinput.erase(0, nextdindex + 1);
// Set the amount of sides a single die has and filter the eyes on each side into a vector
int64_t operatorindex = userinput.find_first_of("+-");
std::string specialdiesides;
std::vector<int> eyes;
// If no +/- has been found length is the whole string, else the index of the next operator
if (operatorindex >= 1) {
specialdiesides = userinput.substr(1, operatorindex - 2);
}
else {
specialdiesides = userinput.substr(1, userinput.length() - 2);
}
// The amount of sides a die has, is calculated by rounding the special die input up
int diesides = lround(specialdiesides.length() / 2);
// This gets set as reserve for eyes vector
eyes.reserve(diesides);
// After which the eyes on the die get filterd into the eyes vector
for (i = 0; i <= diesides * 2; i += 2) {
eyes.push_back(int(std::stoi(specialdiesides.substr(i, 1))));
}
// The amount of dice thrown, die sides and vector of all eyes on a die get pushed into the objects vector dicesets of Diceset
dicesets.push_back(Diceset(diesides, eyes, dicethrown)); // The error shows here
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论