Jump to content
  • Advertisement
Sign in to follow this  
GodFear

Class method with the same name?

This topic is 2299 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 folks,

Howdy?!
I need help on a C++ class methods.
Say my class is:


class CMyClass
{
public:
void MyMethod( int a, int b);
}
class CMyNextClass: public CMyClass
{
public:
void MyMethod( int a, int b, int c);
}
void main()
{
CMyNextClass* pMyClass = new CMyNextClass();

pMyclass->MyMethod( 1, 2, 3);
}



I get an compiler error saying MyMethod function does not take 3 arguments.
I am very new to C++ and classes and is still in learning process.
I am trying to google if what i am doing violates C++ rule but i can't find an answer. I appreciate if someone can help.
Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Class declaration needs ending semicolon!
And it's pMyClass, not pMyclass

class CMyClass
{
public:
void MyMethod( int a, int b);
}

;
class CMyNextClass: public CMyClass
{
public:
void MyMethod( int a, int b, int c);
}

;
void main()
{
CMyNextClass* pMyClass = new CMyNextClass();

pMy

Class->MyMethod( 1, 2, 3);
}

Share this post


Link to post
Share on other sites
That should actually give you a "class does not take two arguments" error when called with two arguments. Three arguments should be fine. If you want it to take two arguments then you would insert a using CMyClass::MyMethod; in the CMyNextClass definition.

Share this post


Link to post
Share on other sites
ok my bad, i really didn't copy and paste my code. instead i just manually typed it.
The real code has semi colons at the ending as you have shown.

Also pMyClass->MyMethod( 1, 2, 3 ) is what i actually have in my code.

The compiler error i am getting is that it says that "MyMethod" does not take 3 arguments. It seems like it is expecting the MyMethod function declared and defined in the base class CMyClass.
I am not sure if i am violating the rules of function overloading in my code, but i cannot figure it out.

Share this post


Link to post
Share on other sites

ok my bad, i really didn't copy and paste my code. instead i just manually typed it.
The real code has semi colons at the ending as you have shown.

Also pMyClass->MyMethod( 1, 2, 3 ) is what i actually have in my code.

The compiler error i am getting is that it says that "MyMethod" does not take 3 arguments. It seems like it is expecting the MyMethod function declared and defined in the base class CMyClass.
I am not sure if i am violating the rules of function overloading in my code, but i cannot figure it out.

Then show your real code.
Otherwise, you code is fine and should not cause 3 parameters error.

Share this post


Link to post
Share on other sites
That should actually give you a "class does not take two arguments" error when called with two arguments. Three arguments should be fine. If you want it to take two arguments then you would insert a using CMyClass::MyMethod; in the CMyNextClass definition.


You are correct! I think my brain just dried out figuring out this problem. so if the class that inherits a base class and declare a method with the same name(different parameters) as one in the base class, it is considered an violation then?

Share this post


Link to post
Share on other sites

You are correct! I think my brain just dried out figuring out this problem. so if the class that inherits a base class and declare a method with the same name(different parameters) as one in the base class, it is considered an violation then?

No, that's not violation.
But the function in your derived class will hide the visibility of the function in the base class.
That's why SiCrane suggested use "using BaseClass:SomeFunction;" in your derived class.

Share this post


Link to post
Share on other sites

[quote name='GodFear' timestamp='1333374764' post='4927489']
You are correct! I think my brain just dried out figuring out this problem. so if the class that inherits a base class and declare a method with the same name(different parameters) as one in the base class, it is considered an violation then?

No, that's not violation.
But the function in your derived class will hide the visibility of the function in the base class.
That's why SiCrane suggested use "using BaseClass:SomeFunction;" in your derived class.
[/quote]

Thank you.
So to clarify, i can overload a method within a class. but i cannot overload a method from a base class in the derived class.
Am i correct to say this? If yes, is this a rule? I am trying to figure out why would the derived class hide the visibility of MyMethod() function in the base class.


class CBaseClass
{
public:
void MyMethod( int a );
};
class CDerivedClass: public CBaseClass
{
public:
void MyMethod( int a, int b ); // error!
void AnotherMethod( int a );
void AnotherMethod( int a, int b ); // ok!
};

Share this post


Link to post
Share on other sites

Thank you.
So to clarify, i can overload a method within a class. but i cannot overload a method from a base class in the derived class.
Am i correct to say this? If yes, is this a rule? I am trying to figure out why would the derived class hide the visibility of MyMethod() function in the base class.
]

Not exactly.
This link may help you
http://stackoverflow.com/questions/1628768/why-does-an-overridden-function-in-the-derived-class-hide-other-overloads-of-the

Share this post


Link to post
Share on other sites

[quote name='GodFear' timestamp='1333375822' post='4927500']
Thank you.
So to clarify, i can overload a method within a class. but i cannot overload a method from a base class in the derived class.
Am i correct to say this? If yes, is this a rule? I am trying to figure out why would the derived class hide the visibility of MyMethod() function in the base class.
]

Not exactly.
This link may help you
http://stackoverflow...verloads-of-the
[/quote]

Wow! That link just clarified everything!
Thanks for the link, i really appreciate the help.

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!