samedi 22 octobre 2022

How to correctly access members of custom data types of C++ class?

I have wrote the following code.. My expected output is [4,11, 22, 33,44, 45, 46]

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <span>

/// FuntionMango class.
class FuntionMango
{
public:
    /// Getter of param types.
    const std::vector<uint8_t> &getParamTypes() const noexcept
    {
        return ParamTypes;
    }
    std::vector<uint8_t> &getParamTypes() noexcept { return ParamTypes; }

    /// Getter of return types.
    const std::vector<uint8_t> &getReturnTypes() const noexcept
    {
        return ReturnTypes;
    }
    std::vector<uint8_t> &getReturnTypes() noexcept { return ReturnTypes; }

private:
    /// \name Data of FuntionMango.

    std::vector<uint8_t> ParamTypes{11, 22, 33};

    std::vector<uint8_t> ReturnTypes{44, 45, 46};
};

// MangoType Class
class MangoType
{
    
public:
    /// Getter of content vector.

    std::vector<FuntionMango> &getContent() noexcept { return Content; }

private:
    /// \name Data of MangoType.

    std::vector<FuntionMango> Content;
};

// ValType Class

class Mango
{
public:
    const std::vector<MangoType> &getMangoType() const { return typeMan; }
    std::vector<MangoType> &getMangoType() { return typeMan; }

private:
    // There are many members of different types : I just mention one.
    std::vector<MangoType> typeMan;
};

std::vector<uint8_t> encoded(Mango &Man)
{
    std::vector<uint8_t> sok;
    std::vector<uint8_t> sap;
    for (int i = 0; i < Man.getMangoType().size(); i++)
    {
        for (int j = 0; j < Man.getMangoType()[i].getContent().size(); j++)
        {
            std::vector<uint8_t> saam = Man.getMangoType()[i].getContent()[j].getParamTypes();
            sok.insert(sok.end(), saam.begin(), saam.end());
            std::vector<uint8_t> sss = Man.getMangoType()[i].getContent()[j].getReturnTypes();
            sap.insert(sap.end(), sss.begin(), sss.end());
        }
    }

    std::vector<uint8_t> so;

    so.insert(so.end(), sok.begin(), sok.end());
    so.insert(so.end(), sap.begin(), sap.end());

    std::vector<uint8_t> result;

    result.push_back(4);
    result.insert(result.end(), so.begin(), so.end());

    return result;
}

std::vector<uint8_t> serial(Mango &Man)
{
    std::vector<uint8_t> s = encoded(Man);

    std::vector<uint8_t> ss;

    ss.insert(ss.end(), s.begin(), s.end());

    return ss;
}

int main()
{
    Mango Man; //Mango class object
    std::vector<uint8_t> seri = serial(Man);

    for (int i = 0; i < seri.size(); i++)
    {
        std::cout << unsigned(seri[i]) << " ";
    }
}

I'm not able to find the bug in this code. Currently I am getting the output [4] while my expected output [4,11, 22, 33,44, 45, 46]. Can someone show me the correct way of getting the above output by creating the object of Mango Class only?

Aucun commentaire:

Enregistrer un commentaire