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

            Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

            Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

            How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page