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
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
|
show 2 more comments
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
vector1[i]
yields a pointer like any other. Just use->
like you would for a regularClass1*
. 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
|
show 2 more comments
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
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
c++ c++11 c++14 sdl stdvector
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 regularClass1*
. 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
|
show 2 more comments
vector1[i]
yields a pointer like any other. Just use->
like you would for a regularClass1*
. 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
|
show 2 more comments
1 Answer
1
active
oldest
votes
for(int i = 0; i < vector1.size(); i++)
vector1[i]->width += offset;
Dereference the pointer with ->
as you normally would.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
for(int i = 0; i < vector1.size(); i++)
vector1[i]->width += offset;
Dereference the pointer with ->
as you normally would.
add a comment |
for(int i = 0; i < vector1.size(); i++)
vector1[i]->width += offset;
Dereference the pointer with ->
as you normally would.
add a comment |
for(int i = 0; i < vector1.size(); i++)
vector1[i]->width += offset;
Dereference the pointer with ->
as you normally would.
for(int i = 0; i < vector1.size(); i++)
vector1[i]->width += offset;
Dereference the pointer with ->
as you normally would.
answered Mar 7 at 15:33
ExaExa
3,14643554
3,14643554
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
vector1[i]
yields a pointer like any other. Just use->
like you would for a regularClass1*
. 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