0

Hi,
I got this error while trying to access the method that I've posted here.
Main program:
cin>>sname;
string x;
Sports x = Sports.getName();//error:a nonstatic member reference must be relative to a specific object.
loc=x.find(sname);

In Sports.cpp:
string Sports::getName() {return name;}

I've declared getName() function as public.Still I'm getting this error. How to access string from getName() method.

Thanks for any help.

3
Contributors
5
Replies
11
Views
5 Years
Discussion Span
Last Post by Ancient Dragon
0

The compiler is right. Sports is a class name, and you can't use it to call a non-static method. To call getName() you first need to create an object of the Sports class.

0

Just what do you expect getName() to return in that line? The statement Sport::getName() means that getName() is a static method of the class and name would also have to be a static member of the class.

Probably what you want is this->getName();

Edited by Ancient Dragon: n/a

0

The compiler is right. Sports is a class name, and you can't use it to call a non-static method. To call getName() you first need to create an object of the Sports class.

creating an object is done by Sports x; right?
and how to call getName()? x=Movie.getName(); this is not the correct way?

0

Just what do you expect getName() to return in that line? The statement Sport::getName() means that getName() is a static method of the class and name would also have to be a static member of the class.

Probably what you want is this->getName();

No, we haven't got the pointers yet. So we are not able to use pointers. Is there other way to call without using pointers?

0

creating an object is done by Sports x; right?

Yes

and how to call getName()? x=Movie.getName(); this is not the correct way?

Yes, but that's not the code you posed. You can't expect people to help you if you don't post the correct code.

Edited by Ancient Dragon: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.