'(70,58): error C2440: '<function-style-cast>': cannot convert from 'initializer list' to 'Diceset'
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
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
