(70,58):错误c2440:''>>':无法转换为初始化器列表'到Diceset'

发布于 2025-01-28 16:38:54 字数 2782 浏览 2 评论 0原文

新手在这里。我正在尝试获取一个对象向量,该对象由一个带有两个整数和一个向量的类定义,以接受我对新数据的推动,但是我所做的任何事情都会抛出此例外,很快我将任何内容都放入向量推动中。

我试图在整个网上进行搜索,但无法找到似乎有效的解决方案,也可以理解它的工作原理。 任何帮助将不胜感激!我试图尽我所能解释代码。

我正在使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文