Home > database >  I can't find how to add an object to a list of objects
I can't find how to add an object to a list of objects

Time:01-24

I'm trying to add an object to a list of object the equivalent of myList.append() in python. I've tried insert(), push_back() doesn't work because it's a list of objects. I've tried new() and delete but doesn't work and i don't understand how it works. there's some code missing cause else i cant send th message(too many code)

struct point{
    int x;
    int y;
    int xp;
    int yp;
};

point lines[1];
point pre[1];


int main(){
    while(window.isOpen()){
        while(window.pollEvent(event)){
            if((event.type == Event::KeyPressed) && (event.key.code == Keyboard::Enter) && (!fullscreen)) {
                fullscreen = true;
                window.create(VideoMode(1920, 1080), "Project_1", (fullscreen ? Style::Fullscreen : Style::Resize|Style::Close));
                text.setString("Press 'Escape' to exit fullscreen!");
            }
            else if((event.type == Event::KeyPressed) && (event.key.code == Keyboard::Escape) && (fullscreen)) {
                fullscreen = false;
                window.create(VideoMode(1920, 1080), "Project_1", (fullscreen ? Style::Fullscreen : Style::Resize|Style::Close));
            }
            if(event.type == Event::Closed){
                window.close();
            }
            if(event.type == Event::MouseButtonPressed){
                point * lines = new point[int(sizeof(lines)/sizeof(lines[0])) 1];
                delete lines;
                text.setString(to_string(sizeof(lines)/sizeof(lines[0])));
                cout<<to_string(sizeof(lines)/sizeof(lines[0]))<<endl;
            }
        

CodePudding user response:

You can not add to an array in c and other low level languages. You will need to use a vector for your case https://www.geeksforgeeks.org/vector-in-cpp-stl/

Vectors are dynamic arrays which can be added to and removed from.

Define like so: vector<point> lines;

And use the following cmds to manipulate a vector from the above reference:

push_back()
pop_back()
insert()
  •  Tags:  
  • Related