Vector of Class-Object pointers: How to get value at pointer address? C++ and SDL_Rect2019 Community Moderator ElectionHow can I profile C++ code running on Linux?c++ vector.push_back error: request for member 'push_back'…, which is of non-class type 'vector(char, allocator(char)) ()()'C++ Iterators, interfaces and pointersTrying to use std::vector<MyClass>How to store a vector of objects of an abstract class which are given by std::unique_ptr?Does std::vector<Object> reserve method need a copy constructor for Object class?Why should I use a pointer rather than the object itself?Passing vector pointer in c++Expression must have class type. - Vector of class objectsC++: Vector of Pointers to Objects from another Vector

Why did it take so long to abandon sail after steamships were demonstrated?

New passport but visa is in old (lost) passport

Why do newer 737s use two different styles of split winglets?

Why Choose Less Effective Armour Types?

Brexit - No Deal Rejection

How difficult is it to simply disable/disengage the MCAS on Boeing 737 Max 8 & 9 Aircraft?

What is the adequate fee for a reveal operation?

Recruiter wants very extensive technical details about all of my previous work

Adventure Game (text based) in C++

Why does overlay work only on the first tcolorbox?

Knife as defense against stray dogs

How to make healing in an exploration game interesting

What did “the good wine” (τὸν καλὸν οἶνον) mean in John 2:10?

Book about superhumans hiding among normal humans

What exactly is this small puffer fish doing and how did it manage to accomplish such a feat?

How to write cleanly even if my character uses expletive language?

Why one should not leave fingerprints on bulbs and plugs?

What is the relationship between relativity and the Doppler effect?

The German vowel “a” changes to the English “i”

How are passwords stolen from companies if they only store hashes?

I got the following comment from a reputed math journal. What does it mean?

About the actual radiative impact of greenhouse gas emission over time

How to deal with taxi scam when on vacation?

et qui - how do you really understand that kind of phraseology?



Vector of Class-Object pointers: How to get value at pointer address? C++ and SDL_Rect



2019 Community Moderator ElectionHow can I profile C++ code running on Linux?c++ vector.push_back error: request for member 'push_back'…, which is of non-class type 'vector(char, allocator(char)) ()()'C++ Iterators, interfaces and pointersTrying to use std::vector<MyClass>How to store a vector of objects of an abstract class which are given by std::unique_ptr?Does std::vector<Object> reserve method need a copy constructor for Object class?Why should I use a pointer rather than the object itself?Passing vector pointer in c++Expression must have class type. - Vector of class objectsC++: Vector of Pointers to Objects from another Vector










-2















I've got a vector of class-object pointers. I declare the vector in main..:



std::vector<Class1*> vector1;
vector1.push_back(&object1);
vector1.push_back(&object2);


The below error occurs. This is just the simplest variant of trying to access data contained within the class pointed to by the vector.



Ex:



Main.cpp:



std::cout << vector1[0]->rect->w << std::endl;


Class1.h



SDL_Rect rect100, 100, 50, 50;


The above results in a red underline of the word 'Vector1' with the error "Expression must have a valid pointer type".



EDIT:



Forgot to mention that I'm trying to get to a SDL_Rect contained within the Class-Objects pointed to by the vector... Not sure how/if that changes anything.
I'll provide more complete code when I'm off work.



The code I've provided works if I simply specify a variable of a normal type such an int (where 'age' below is an int):



 std::cout << vector1[0]->age << std::endl;


But since I'm specifying an SDL_Rect in the example from Main.cpp I get the error I mentioned. Anyone know what the peculiarity is?










share|improve this question
























  • vector1[i] yields a pointer like any other. Just use -> like you would for a regular Class1*. Edit : Actually, I'm just assuming. Your function isn't actually legal c++ so it's not possible to say with certainty at this point.

    – François Andrieux
    Mar 7 at 15:30







  • 3





    exampleFunction(vector1, int offset 5) this is invalid C++ code, please edit and clarify

    – Jabberwocky
    Mar 7 at 15:31












  • instead of vector1[i].width += offset; do vector1[i]->width += offset;

    – user2320641
    Mar 7 at 15:33






  • 1





    Please read Minimal, Complete, and Verifiable example for guidelines on providing complete and helpful examples. Please only share real code that you've tried yourself.

    – François Andrieux
    Mar 7 at 15:34






  • 1





    @Arianax Then I don't know what the problem is. Please post a MCVE (see my previous comment for a link) and then we might be able to help you.

    – François Andrieux
    Mar 7 at 15:41















-2















I've got a vector of class-object pointers. I declare the vector in main..:



std::vector<Class1*> vector1;
vector1.push_back(&object1);
vector1.push_back(&object2);


The below error occurs. This is just the simplest variant of trying to access data contained within the class pointed to by the vector.



Ex:



Main.cpp:



std::cout << vector1[0]->rect->w << std::endl;


Class1.h



SDL_Rect rect100, 100, 50, 50;


The above results in a red underline of the word 'Vector1' with the error "Expression must have a valid pointer type".



EDIT:



Forgot to mention that I'm trying to get to a SDL_Rect contained within the Class-Objects pointed to by the vector... Not sure how/if that changes anything.
I'll provide more complete code when I'm off work.



The code I've provided works if I simply specify a variable of a normal type such an int (where 'age' below is an int):



 std::cout << vector1[0]->age << std::endl;


But since I'm specifying an SDL_Rect in the example from Main.cpp I get the error I mentioned. Anyone know what the peculiarity is?










share|improve this question
























  • vector1[i] yields a pointer like any other. Just use -> like you would for a regular Class1*. Edit : Actually, I'm just assuming. Your function isn't actually legal c++ so it's not possible to say with certainty at this point.

    – François Andrieux
    Mar 7 at 15:30







  • 3





    exampleFunction(vector1, int offset 5) this is invalid C++ code, please edit and clarify

    – Jabberwocky
    Mar 7 at 15:31












  • instead of vector1[i].width += offset; do vector1[i]->width += offset;

    – user2320641
    Mar 7 at 15:33






  • 1





    Please read Minimal, Complete, and Verifiable example for guidelines on providing complete and helpful examples. Please only share real code that you've tried yourself.

    – François Andrieux
    Mar 7 at 15:34






  • 1





    @Arianax Then I don't know what the problem is. Please post a MCVE (see my previous comment for a link) and then we might be able to help you.

    – François Andrieux
    Mar 7 at 15:41













-2












-2








-2








I've got a vector of class-object pointers. I declare the vector in main..:



std::vector<Class1*> vector1;
vector1.push_back(&object1);
vector1.push_back(&object2);


The below error occurs. This is just the simplest variant of trying to access data contained within the class pointed to by the vector.



Ex:



Main.cpp:



std::cout << vector1[0]->rect->w << std::endl;


Class1.h



SDL_Rect rect100, 100, 50, 50;


The above results in a red underline of the word 'Vector1' with the error "Expression must have a valid pointer type".



EDIT:



Forgot to mention that I'm trying to get to a SDL_Rect contained within the Class-Objects pointed to by the vector... Not sure how/if that changes anything.
I'll provide more complete code when I'm off work.



The code I've provided works if I simply specify a variable of a normal type such an int (where 'age' below is an int):



 std::cout << vector1[0]->age << std::endl;


But since I'm specifying an SDL_Rect in the example from Main.cpp I get the error I mentioned. Anyone know what the peculiarity is?










share|improve this question
















I've got a vector of class-object pointers. I declare the vector in main..:



std::vector<Class1*> vector1;
vector1.push_back(&object1);
vector1.push_back(&object2);


The below error occurs. This is just the simplest variant of trying to access data contained within the class pointed to by the vector.



Ex:



Main.cpp:



std::cout << vector1[0]->rect->w << std::endl;


Class1.h



SDL_Rect rect100, 100, 50, 50;


The above results in a red underline of the word 'Vector1' with the error "Expression must have a valid pointer type".



EDIT:



Forgot to mention that I'm trying to get to a SDL_Rect contained within the Class-Objects pointed to by the vector... Not sure how/if that changes anything.
I'll provide more complete code when I'm off work.



The code I've provided works if I simply specify a variable of a normal type such an int (where 'age' below is an int):



 std::cout << vector1[0]->age << std::endl;


But since I'm specifying an SDL_Rect in the example from Main.cpp I get the error I mentioned. Anyone know what the peculiarity is?







c++ c++11 c++14 sdl stdvector






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 16:23







Arianax

















asked Mar 7 at 15:30









ArianaxArianax

13




13












  • vector1[i] yields a pointer like any other. Just use -> like you would for a regular Class1*. Edit : Actually, I'm just assuming. Your function isn't actually legal c++ so it's not possible to say with certainty at this point.

    – François Andrieux
    Mar 7 at 15:30







  • 3





    exampleFunction(vector1, int offset 5) this is invalid C++ code, please edit and clarify

    – Jabberwocky
    Mar 7 at 15:31












  • instead of vector1[i].width += offset; do vector1[i]->width += offset;

    – user2320641
    Mar 7 at 15:33






  • 1





    Please read Minimal, Complete, and Verifiable example for guidelines on providing complete and helpful examples. Please only share real code that you've tried yourself.

    – François Andrieux
    Mar 7 at 15:34






  • 1





    @Arianax Then I don't know what the problem is. Please post a MCVE (see my previous comment for a link) and then we might be able to help you.

    – François Andrieux
    Mar 7 at 15:41

















  • vector1[i] yields a pointer like any other. Just use -> like you would for a regular Class1*. Edit : Actually, I'm just assuming. Your function isn't actually legal c++ so it's not possible to say with certainty at this point.

    – François Andrieux
    Mar 7 at 15:30







  • 3





    exampleFunction(vector1, int offset 5) this is invalid C++ code, please edit and clarify

    – Jabberwocky
    Mar 7 at 15:31












  • instead of vector1[i].width += offset; do vector1[i]->width += offset;

    – user2320641
    Mar 7 at 15:33






  • 1





    Please read Minimal, Complete, and Verifiable example for guidelines on providing complete and helpful examples. Please only share real code that you've tried yourself.

    – François Andrieux
    Mar 7 at 15:34






  • 1





    @Arianax Then I don't know what the problem is. Please post a MCVE (see my previous comment for a link) and then we might be able to help you.

    – François Andrieux
    Mar 7 at 15:41
















vector1[i] yields a pointer like any other. Just use -> like you would for a regular Class1*. Edit : Actually, I'm just assuming. Your function isn't actually legal c++ so it's not possible to say with certainty at this point.

– François Andrieux
Mar 7 at 15:30






vector1[i] yields a pointer like any other. Just use -> like you would for a regular Class1*. Edit : Actually, I'm just assuming. Your function isn't actually legal c++ so it's not possible to say with certainty at this point.

– François Andrieux
Mar 7 at 15:30





3




3





exampleFunction(vector1, int offset 5) this is invalid C++ code, please edit and clarify

– Jabberwocky
Mar 7 at 15:31






exampleFunction(vector1, int offset 5) this is invalid C++ code, please edit and clarify

– Jabberwocky
Mar 7 at 15:31














instead of vector1[i].width += offset; do vector1[i]->width += offset;

– user2320641
Mar 7 at 15:33





instead of vector1[i].width += offset; do vector1[i]->width += offset;

– user2320641
Mar 7 at 15:33




1




1





Please read Minimal, Complete, and Verifiable example for guidelines on providing complete and helpful examples. Please only share real code that you've tried yourself.

– François Andrieux
Mar 7 at 15:34





Please read Minimal, Complete, and Verifiable example for guidelines on providing complete and helpful examples. Please only share real code that you've tried yourself.

– François Andrieux
Mar 7 at 15:34




1




1





@Arianax Then I don't know what the problem is. Please post a MCVE (see my previous comment for a link) and then we might be able to help you.

– François Andrieux
Mar 7 at 15:41





@Arianax Then I don't know what the problem is. Please post a MCVE (see my previous comment for a link) and then we might be able to help you.

– François Andrieux
Mar 7 at 15:41












1 Answer
1






active

oldest

votes


















2














for(int i = 0; i < vector1.size(); i++)

vector1[i]->width += offset;



Dereference the pointer with -> as you normally would.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047420%2fvector-of-class-object-pointers-how-to-get-value-at-pointer-address-c-and-sd%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    for(int i = 0; i < vector1.size(); i++)

    vector1[i]->width += offset;



    Dereference the pointer with -> as you normally would.






    share|improve this answer



























      2














      for(int i = 0; i < vector1.size(); i++)

      vector1[i]->width += offset;



      Dereference the pointer with -> as you normally would.






      share|improve this answer

























        2












        2








        2







        for(int i = 0; i < vector1.size(); i++)

        vector1[i]->width += offset;



        Dereference the pointer with -> as you normally would.






        share|improve this answer













        for(int i = 0; i < vector1.size(); i++)

        vector1[i]->width += offset;



        Dereference the pointer with -> as you normally would.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 7 at 15:33









        ExaExa

        3,14643554




        3,14643554





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047420%2fvector-of-class-object-pointers-how-to-get-value-at-pointer-address-c-and-sd%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

            2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

            Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme