Jump to content
  • Advertisement
Sign in to follow this  
DanielSefton

[solved] Inheritance, Overriding and cannot convert parameter

This topic is 3670 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

Hi, I have a simple inheritance and function overriding problem that I'm struggling to solve. InputHandler has the function keyReleased which obviously is used to check for key strokes. However, most of the functions I need to access (e.g. win) are in PlayState. So I'd like to use the InputHandler class in PlayState to check for key strokes there as well. How would I go about overriding the keyReleased class from InputHandler in PlayState? Do I need keyReleased to be virtual? Or can I just inherit InputHander directly, like I'm trying below? The issue before I can even test the overriding is that "InputHandler(pSystem)," in PlayState.cpp causes the error: 1>PlayState.cpp(61) : error C2664: 'InputHandler::InputHandler(const InputHandler &)' : cannot convert parameter 1 from 'CEGUI::System *' to 'const InputHandler &' 1> Reason: cannot convert from 'CEGUI::System *' to 'const InputHandler' 1> No constructor could take the source type, or constructor overload resolution was ambiguous I can't for the life of me think why that error occurs. The code I have included below is the basic outline of my source to illustrate the issue, so please don't get confused about how it works.
==Input.h==

#pragma once

#include <OIS/OIS.h>

namespace CEGUI
{
	class System;
}

class InputHandler : public OIS::KeyListener
{
private:
	CEGUI::System* m_pSystem;

public:
	InputHandler(CEGUI::System* pSystem); 
	~InputHandler();
	
	// KeyListener
	bool keyReleased(const OIS::KeyEvent &evt);
};

==Input.cpp==

#include "input.h"

#include "CEGUISystem.h"

InputHandler::InputHandler(CEGUI::System* pSystem) :
	m_pSystem(pSystem)
{
	//Arguments
}

InputHandler::~InputHandler() {
	//Arguments
}

bool InputHandler::keyReleased(const OIS::KeyEvent &evt) {
	if (evt.key == OIS::KC_ESCAPE)
	{
		//Shutdown
	}

	return m_pSystem->injectKeyUp(evt.key);
}

==PlayState.h==

#include "input.h"

#include <CEGUI/CEGUISystem.h>
#include "OgreCEGUIRenderer.h"

class PlayState : public InputHandler //Inherit InputHandler class
{
public:
	PlayState(Ogre::RenderWindow* win,CEGUI::System* pSystem);
	~PlayState()
	{
	}

	bool keyReleased(const OIS::KeyEvent &evt);
	
protected:
	CEGUI::System* m_pSystem;
	Ogre::RenderWindow* mWindow;
};


==PlayState.cpp==

#include "PlayState.h"

PlayState::PlayState(Ogre::RenderWindow* win,CEGUI::System* pSystem) :
	InputHandler(pSystem), //Problem line
	m_pSystem(pSystem),
	mWindow(win)
{
	//Arguments
}

bool PlayState::keyReleased(const OIS::KeyEvent &evt)
{
	if (evt.key == OIS::KC_A)
	{
		mWindow->setWindowExtents();
	}
}
I hope I've explained it well enough. Let me know if you want me to explain it further. Cheers, Dan. [Edited by - DanielSefton on June 7, 2008 6:47:14 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Shakedown
Try making the keyReleased function virtual.


Yeah. Though I can't test it until I get the "cannot convert parameter" error solved.

Thanks!

Share this post


Link to post
Share on other sites
Quote:
Original post by DanielSefton
Quote:
Original post by Shakedown
Try making the keyReleased function virtual.


Yeah. Though I can't test it until I get the "cannot convert parameter" error solved.

Thanks!


Ha yeah. Also, try removing that trailing comma at the end of your initializer list. Perhaps that screwing up the constructor (never ran into the problem before).

Share this post


Link to post
Share on other sites
namespace CEGUI
{
class System;
}

Is this a copy and paste error or did you forget the ; at the end of your namespace?

Share this post


Link to post
Share on other sites
Yeah - copy&paste typos probably. I'm sure the compiler would detect those kind of errors.

It's more to do with the way I have structured the code, and how to inherit the InputManager class properly.

...And of course why on earth it's returning this error:

cannot convert parameter 1 from 'CEGUI::System *' to 'const InputHandler &'

Thanks for the help so far. =]

Share this post


Link to post
Share on other sites
Quote:
Original post by stonemetal
namespace CEGUI
{
class System;
}

Is this a copy and paste error or did you forget the ; at the end of your namespace?


C++ namespaces are not required to end with a semicolon.

I believe the reason classes and structs are required is because C allowed you to do this:

struct Foo { int x; int y; } foo;

It isn't used much anymore (except for the typedef struct idiom in C).

Share this post


Link to post
Share on other sites
Also, you're including CEGUI::System* m_pSystem twice in your PlayState class. You're inheriting it from the InputHandler parent class, and then declaring it again in the PlayState class itself. I'm not sure if this could cause any problems since the inherited variable is declared private in the InputHandler so the PlayState class wouldn't know about it, but perhaps it could be something to look into for other purposes.

Share this post


Link to post
Share on other sites
Quote:
Original post by Shakedown
Also, you're including CEGUI::System* m_pSystem twice in your PlayState class. You're inheriting it from the InputHandler parent class, and then declaring it again in the PlayState class itself. I'm not sure if this could cause any problems since the inherited variable is declared private in the InputHandler so the PlayState class wouldn't know about it, but perhaps it could be something to look into for other purposes.


If I don't declare m_pSystem in PlayState.h, it returns: "illegal member initialization: 'm_pSystem' is not a base or member". It doesn't seem to be causing any problems.

It must not be as simple as I thought. So I take it I am inheriting InputManager correctly? If so, there must be some other obscure reason...

Normally the "cannot convert parameter" error occurs e.g. when a function expects an int, and you give it a char. I just have no idea what it means in the case of: "from 'Class *' to 'const Class &'". I have never come across it in this situation.

Share this post


Link to post
Share on other sites
Have you tried a full rebuild? It would appear that the compiler doesn't believe you have a constructor "InputHandler(CEGUI::System* pSystem);". Because you call a constructor with a single argument: "InputHandler(pSystem)", the compiler complains because the best match, the (default, compiler generated) InputHandler copy constructor.

Either that or the code you posted doesn't accurately reflect what the compiler sees. Can you verify that the InputHandler class still looks like what you posted?

Shakedown is correct, you should remove the duplicate System pointer. Also remove the line "m_pSystem(pSystem)" in the PlayState constructor to fix the other error you were having: "illegal member initialization: 'm_pSystem' is not a base or member".

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!