Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do I initiate a list-type pointer and push back a dynamically allocated item into the list? down is the general code, not that whole entire actual code.

What I have tried:

I have tried something like
C++
std::list<unsigned short>* Function::Function(unsigned short value) const{
    Type* temp = m_head;
    //initializing myList
    std::list* myList;
    while{
    if(conditional){
       (*myList).push_back(temp->getValue());
        }
}
    return myList;
}

it happens that if I do this it will tell me mylist is not initialized


BUT if i do something like
C++
std::list<unsigned short> *item = new std::list<unsigned short>;

it wont let me push back

C++
(*myList).push_back(item);


??
Posted
Updated 15-Jun-21 5:25am
v2

You are misusing pointer declarations and access. Use one of the following:
C++
// 1. A list created on the local stack, i.e. inside its enclosing function
//    Will be deleted automatically when it goes out of scope.
    std::list<unsigned short> myList;
    myList.push_back(item); // note access is by 'dot'


// 2. A pointer to a list created on the heap. NB must be deleted when no longer needed
    std::list<unsigned short>* pMyList = new std::list<unsigned short>;
    pMyList->push_back(item);  // note access is by 'arrow'
 
Share this answer
 
Comments
Mango JellyBeans 15-Jun-21 13:58pm    
lets say I have a value being passed into the function,
i created the
std::list<unsigned short="">* pMyList = new std::list<unsigned short="">;
how exactly would i assign the value being passed into the function in this allocated spot? it seems that if i try to push_back, it wouldnt let me even if i did object.value inside.
Mango JellyBeans 15-Jun-21 14:00pm    
its the pointer to a list.
Richard MacCutchan 16-Jun-21 4:04am    
Option 2 above is how you do it. But item must be an unsigned short value (not a pointer), because that is how you have declared your list.
Review the types of the various objects in your program. In the above, both myList and item are both pointers to a list of unsigned shorts. Since myList is expecting an object of unsigned short, trying to use it with an object of std::list<unsigned short>* is never going to work. What is the type of Type.getValue()? It should be unsigned short, in which case the following should work:
C++
Type* temp = m_head;
std::list<unsigned short>* myList = new std::list<unsigned short>;
while(condition)
{
   myList->push_back(temp->get_value());
}
 
Share this answer
 
Comments
Mango JellyBeans 15-Jun-21 13:53pm    
yeah so I tried
std::list<unsigned short="">* myList;
myList->push_back(temp->getValue());
before but then the IDE would just say my list was not initialized
k5054 15-Jun-21 15:16pm    
the statement std::list<unsigned short>* myList; declares the variable myList but it does not initialize it. Assume, for the purposes of discussion, that the compiler gives myList the value NULL when declared, unless assigned at declaration time. What you have then is the equivalent of
 (NULL)->push_back(val)
. You need to assign a value to myList, either through new (or a smart pointer), or by assigning the pointer to an already existing list of a conforming type. Just declaring a pointer variable, and then trying to use it without pointing at something first is a programming error.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900