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.

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.

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();

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?

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.