Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Pass entire vector of objects to constructor by reference?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 ChainedHollow   Members   -  Reputation: 158

Like
0Likes
Like

Posted 20 January 2014 - 09:53 PM

I am needing to pass an entire vector of objects to a constructor of another class.  I have succeeded in doing this with a single vector object, but with the entire vector, it's giving me trouble.

 

Here is the code.

 

classes.h:

 
class Star: public Spell
{
     public:
          Star(std::vector<Hero>& hero_list): h_list(hero_list) {}
     protected:
          std::vector<Hero>& h_list;
}
 

spells.cpp:

 
Star::Star(std::vector<Hero>& hero_list): h_list(hero_list)
{
 
}
 

and then the call from main.cpp:

 
std::vector<Spell*> spells;
spells.push_back(new Star(*heroes));
 

Right now, it's not liking the call. I get: "Error: no operator "*" matches these operands"

I have similar code that is working just fine with passing a single object from the vector, but not this. 

 

I need to pass the entire vector for this particular spell sub-class so it can check values of multiple hero objects and make changes to them. 


Edited by ChainedHollow, 20 January 2014 - 09:55 PM.


Sponsor:

#2 Pachanoi   Members   -  Reputation: 736

Like
1Likes
Like

Posted 20 January 2014 - 10:56 PM

I'm going to assume that your variable heroes is not a pointer, in which case you should not use the * operator and just pass it directly to the constructor: spells.push_back(new Star(heroes));

 

It works for your single objects since they are pointers.

 



#3 ChainedHollow   Members   -  Reputation: 158

Like
0Likes
Like

Posted 20 January 2014 - 11:42 PM

That isnt working either : ( Thank you though. Im so confused!



#4 boogyman19946   Members   -  Reputation: 1097

Like
1Likes
Like

Posted 20 January 2014 - 11:50 PM

What is the type of the heroes object? Your compiler is complaining about you using operator* on heroes.


Edited by boogyman19946, 20 January 2014 - 11:51 PM.

"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#5 ChainedHollow   Members   -  Reputation: 158

Like
0Likes
Like

Posted 20 January 2014 - 11:54 PM

Oh yeah, the declaration of the heroes vector is

std::vector<Hero*> heroes;



#6 boogyman19946   Members   -  Reputation: 1097

Like
3Likes
Like

Posted 21 January 2014 - 12:05 AM

The vectors have incompatible types. Spell constructor is expecting std::vector<Hero>& and what you're giving it is... actually you're trying to perform an invalid operation on heroes so I'm not really sure what to say in this case.

 

There's a two main ways to go here. A) you change the std::vector<Hero>& inside of Spell to std::vector<Hero*>& and then change the push_back call to what Pachanoi said, or B) you change the type of std::vector<Hero*> to std::vector<Hero> and still change the push_back call to what Pachanoi said biggrin.png That will probably solve the problem. 

 

You might want to review the syntax for using pointers and references though to fully understand how to pass them to functions.


Edited by boogyman19946, 21 January 2014 - 12:06 AM.

"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#7 ChainedHollow   Members   -  Reputation: 158

Like
1Likes
Like

Posted 21 January 2014 - 12:07 AM

Thank you so much! It hit me after you asked what the type was. Pointers and references do still confuse me a bit but I am trying! Thanks for the help!



#8 Eck   Crossbones+   -  Reputation: 3467

Like
2Likes
Like

Posted 23 January 2014 - 09:08 AM

Don't feel bad. Pointers are one of those things most people have problems with at first. Keep messing with them, and it will eventually "click". And then you'll wonder to yourself, "Why the heck did I think that was so hard?" :)

 

- Eck






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS