• Advertisement
Sign in to follow this  

class array to list

This topic is 657 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi

the code below is ok but I wonder how to push an array of the Ausfaelle class to list and read it back?

Many thanks.

 

int mListe ()
{
    std::list<Ausfaelle> points;

    points.push_back(Ausfaelle(1,1));

    std::list<Ausfaelle>::iterator iter;

    for(iter = points.begin(); iter != points.end(); ++iter)
    {
        Ausfaelle test = *iter;
        std::string s = std::to_string(test.active);
        char const *pchar = s.c_str();
        XPLMDebugString(pchar);
    }

  return 0;
}

Share this post


Link to post
Share on other sites
Advertisement

You iterate your array and add each element to your list. Alternatively, you may want to check signatures (4) and (5) here

Share this post


Link to post
Share on other sites

Thanks. How should that look for a 2 dimensional array like:

 

Ausfaelle Ausfaelle_Line[100]={
{ 1, 2, 0, "OUTSID", 1, "EXTERNAL CHECKS    PAGE 3", 0, 1, "sim/cockpit2/annunciators/passenger_oxy_on", NULL },

{ 1, 2, 0, "OUTSID", 1, "EXTERNAL CHECKS    PAGE 3", 0, 1, "sim/cockpit2/annunciators/passenger_oxy_on", NULL },

{ 1, 2, 0, "OUTSID", 1, "EXTERNAL CHECKS    PAGE 3", 0, 1, "sim/cockpit2/annunciators/passenger_oxy_on", NULL },

...

...

}

Share this post


Link to post
Share on other sites

You shouldn't really be using this kind of arrays though. You may also want to take a look at range-based for.

Edited by Kercyn

Share this post


Link to post
Share on other sites

That's what I just tried but no luck:

 

    std::list<Ausfaelle>line[10];
    for( int x = 10; x < 20; x = x + 1 )
    {
    line[x].push_back(Ausfaelle(bremsen,1));
    }

    std::list<Ausfaelle>::iterator iter;
    for( int y = 10; y < 20; y = y + 1 )
    {
    for(iter = line[y].begin(); iter != line[y].end(); ++iter)
    {
        Ausfaelle test = *iter;
        std::string s = std::to_string(test.active);
        char const *pchar = s.c_str();
        XPLMDebugString(pchar);
        //std::cout << test.active << "; ";
    }

}

Share this post


Link to post
Share on other sites

Well, my answer assumed that you want to add an array of Ausfaelle to your existing list. The code you have now does not work because on the first iteration you're attempting to access line[10], which does not exist. It might be better explaining what the actual problem is and what you're trying to achieve instead of throwing stuff against the wall and then wondering why they don't stick.

Share this post


Link to post
Share on other sites

First things first: you can't access line[10] or anything higher than 10 because line has only 10 elements and counting starts from 0 so it only has indicies from 0 to 9. So never access indicies higher than 9 because even if it might seem to work it is accessing some random memory there, could be anything.

 

Other than that from what I understood u want to push arrays to the list. Am i right? There is multiple ways to do that depending on what information you have on the size of the arrays. If they are fixed size you can simply do std::list<Ausfaelle*> and make a list of pointers instead. Now when initialising the array you do Ausfaelle* temp = new Ausfaelle[size_of_the_array] and then push_back that pointer. If however the arrays are not of fixed size you will need to do things a bit different. You could either do a small struct as a helper:

struct AusfaelleArray {
    Ausfaelle* start;
    size_t array_size;
}

and make a list of that or you can make a list of std::vector<Ausfaelle> so it's std::list<std::vector<Ausfaelle>> line;

Initializing that would be like this:

AusfaelleArray element = {0}; // Initializing the struct with zero (0) to avoid errors by memory faults
element.start = new Ausfaelle[size_of_array]; // creating the array on the heap. don't forget to delete later
element.array_size = size_of_array; 

When you want to access that then you can iterate through your list as normal and when accessing you can just do:

for(iter = line.begin(); iter != line.end(); ++iter)
{
    std::vector<Ausfaelle> temp = *iter; // Use AusfaelleArray if you decided to go that way
    for(int i = 0; i < temp.size(); i++) { // if u gone the other way instead of temp.size() use temp.array_size
        Ausfaelle test = temp[i];
        // do your stuff
    }
}

Please don't just copy and paste over as it's been a while since i worked with C++ and I'm not 100% sure if the code works exactly as stated. It should give you a hint on the conept though.

Be aware that when allocating memory on the heap with new you need to delete it manually aswell.

 

As of how you are doing it now you make an array of std::list<Ausfaelle> with 10 elements (10 lists) in it. if you want it that way arround you need to be aware that your array as it is now has exactly 10 elements and will always have unless you create a new one and copy the old one over. Again you can use the std::vector instead that automatically does that for you in the background. But you will still need to use indicies that are in range of the array/vector.

 

I hope that helped you a bit finding a solution to your problem. If you still have issues I suggest you give a more percise description of what you want to do, because it makes it easier for people to help you.

Share this post


Link to post
Share on other sites

Thanks. Now first I try to keep it simple. First push and second read values:

 

    std::list<Ausfaelle>line[10];
    for( int x = 0; x < 10; x = x + 1 )
    {
    line[x].push_back(Ausfaelle(x,1,22,bremsen));
    }

    if (mldg == 0){
    std::list<Ausfaelle>::iterator iter;
    for( int y = 0; y < 10; y = y + 1 )
    {
    for(iter = line[y].begin(); iter != line[y].end(); ++iter)
    {
        Ausfaelle test = *iter;
        if (test.drValue == 1){

...

 

 

-------

Ausfaelle::Ausfaelle(int a, int b, int c, int d) {
    a = index;
    b = active;
    c = color;
    d = drValue;
}

 

 

test.drValue is alway 0 so I need to find my errors.

Thanks

Share this post


Link to post
Share on other sites

Ausfaelle::Ausfaelle(int a, int b, int c, int d) {
    a = index;
    b = active;
    c = color;
    d = drValue;
}


You are assigning to local variables (the parameters to the constructor) and everything you do there is lost when the function returns.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement