Jump to content
  • Advertisement
Sign in to follow this  
random_thinker

c++ using vectors in structs

This topic is 4869 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know this is a naff question. but I can't seem to find an answer to it. I have a structure: typedef struct material_summary { . . . vector<Powder> vPowder; . . . } Concrete; When I use this within a class LoadConcrete, I initialize as: class LoadConcrete { private: . . . vector<Concrete> V_; . . . public: . . . void Load(); . . . }; void LoadConcrete::Load() { . . . LoadPowder L; V_.vPowder = L.GetVector(); // GetVector() returns a vector of powder data. . . . } When compiling under g++, this results in the message: 'class std::vector<Concrete,std::allocator<Concrete> >' has no member named 'vPowder'. Am I missing something simple here?

Share this post


Link to post
Share on other sites
Advertisement
LoadConcrete::V_ is of type vector<Concrete>, so the line
V_.vPowder = L.GetVector();

will not function. You need to choose what Concrete struct to set, ie. by getting and using an iterator, or using vector::operator[].
Ie, when one Concrete is pushed onto V_, and you wish to load to it:

V_[0].vPowder = L.GetVector();


PS. There's no need for the typedef struct xx {} yy; pattern in C++.

Share this post


Link to post
Share on other sites
Yes, something really simple ;-)

V_ is your vector of elements from type concrete, to access one of those elements you need to use either [<index>], at( <index> ) or an iterator for V_

Share this post


Link to post
Share on other sites
Thank you Chaps!

The progran has now compiled and run correctly. Ive been moving back and forth between V_.<member name> and V_.size() etc and was too tired and too close to the 'trees' to see the problem.

Best Regards to All!

PS. I'll take your tip to remove the 'typedef' too as I've also recalled that a struct in C++ is just a class with all public elements by default.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!