Jump to content

  • Log In with Google      Sign In   
  • Create Account

Passing an object to constructor of another object? C++


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
8 replies to this topic

#1 ChainedHollow   Members   -  Reputation: 158

Like
0Likes
Like

Posted 10 January 2014 - 12:10 AM

Hi everyone! I am hoping someone can help me out with this. I've been looking around online all night and I am so confused.
 
I have 2 classes, and 1 sub-class. Spell, Heal::Spell, and Hero;
 
Spell is a friend class of Hero and Heal is a subclass of Spell.
I need Heal to be able to make changes to a Hero object, so I am wanting to pass a pointer to the Hero object to the Heal constructor. 
 
If someone can help me with this I would be so grateful! I will include my code, so far.
 
classes.h
Spell
{
public:
     // code
private:
     Hero &hero;
}
 
Heal(const Hero& h)
{
public:
     // code
protected:
     // code
}
 
classes.cpp
Heal::Heal(const Hero& h): hero(h)
{
     //code
}
Thats basically what I have for the classes. Any clarification on how to fix this???
 


#2 boogyman19946   Members   -  Reputation: 1449

Like
1Likes
Like

Posted 10 January 2014 - 12:26 AM

I am so thoroughly confused by the syntax you are using. Did you by any chance mean something more like this? 

class Hero
{
	// Awesome and clean code
};

class Spell
{
public:
	// Awesome and clean interface
private:
	friend class Hero; // Ouch, not too awesome

	Hero &hero;
};

class Heal : public Spell
{
public:
	Heal(const Hero&);
	
protected:
	// Helper functions
};

Well, in order to pass a modifiable Hero pointer to Heal, you need to watch out for a couple of things.

 

One, you are passing a const Hero, which means you won't be able to modify it. Remove the const keyword if you want to modify the object.

 

Two, as far as I can tell, you're passing Hero to the wrong object. The member variable hero in Spell is declared private and trying to assign a value to it from Heal will generate a compiler error. 

 

Try fixing those errors and see if you can get it to do what you want.


Edited by boogyman19946, 10 January 2014 - 12:27 AM.

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


#3 Servant of the Lord   Crossbones+   -  Reputation: 31876

Like
1Likes
Like

Posted 10 January 2014 - 12:28 AM

You can pass it into Heal's constructor, and Heal can pass it to Spell's constructor:
 
class Spell
{
public:
     Spell(const Hero& h) : hero(h) { }

protected:
     Hero &hero;
};
 
class Heal
{
public:
     Heal(const Hero& h) : Spell(h) { }

};
Though, really, you probably shouldn't pass in Hero until the spell is ready to be used:
class Spell
{
public:
     Spell() { }

     virtual void UseOn(Hero &hero) = 0;
};
 
class Heal
{
public:
     Heal() { }

     void UseOn(Hero &hero) override
     {
        float amountToHeal = float(hero.maxHealth) * 0.25f;
        hero.health = (int)amountToHeal;
     }
};

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal | [Fly with me on Twitter

#4 ChainedHollow   Members   -  Reputation: 158

Like
0Likes
Like

Posted 10 January 2014 - 12:42 AM

I see, thank you. It seems I have fixed the errors with the classes, but I don't if it's working yet because calling it is giving me an error now.

I removed const, as was mentioned, and I moved Hero& hero to the protected section of Heal. Then I have:

 

 

spells.push_back(new Heal(heroes.at(0)) );

 

spells is a vector made above. std::vector<Spell*> spells;

and heroes is the hero vector. std::vector<Hero*> heroes;

 

This just isn't working now. Am I calling it wrong?



#5 boogyman19946   Members   -  Reputation: 1449

Like
0Likes
Like

Posted 10 January 2014 - 12:52 AM

What error is it giving you?


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


#6 ChainedHollow   Members   -  Reputation: 158

Like
0Likes
Like

Posted 10 January 2014 - 01:01 AM

error C2664: 'Heal::Heal(Hero &)' : cannot convert parameter 1 from 'Hero **' to 'Hero &'

 

I've tried adding & and * but that doesn't work either.



#7 BitMaster   Crossbones+   -  Reputation: 7749

Like
0Likes
Like

Posted 10 January 2014 - 02:01 AM

'Hero **' means it's a pointer to a pointer to a Hero. '*expression' will turn it into a pointer to a Hero. '**expression' will turn it into a Hero which the compiler can convert into a reference to a Hero as desired.

However, a double pointer feels like you have gone too far down the rabbit hole. You should seriously consider taking several steps backwards and doing more exercises regarding types.

#8 boogyman19946   Members   -  Reputation: 1449

Like
0Likes
Like

Posted 10 January 2014 - 02:06 AM

You're passing in a pointer but the Constructor Is expecting a reference. Make the call

spells.push_back(new Heal(*heroes.at(0)));

You have to dereference the pointer.

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


#9 rip-off   Moderators   -  Reputation: 10520

Like
3Likes
Like

Posted 10 January 2014 - 05:58 AM

I am concerned about the ownership issues that this constructor is causing. How are you going to ensure the Hero reference remains valid? Can you guarantee that the Hero will not be deleted (presuming it is dynamically allocated), leaving an invalid reference? Servant's alternative approach is probably what you should be doing.

 

You should probably be using smart pointers or values, as it is clear you don't yet understand how raw pointers behave. You'll still need to learn the concepts, but smart pointers at least will help you start with good practises and, used exclusively, hopefully limit the kinds of mistakes that are possible.






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