r/StackoverReddit • u/Mighty555 • Jun 19 '24
C++ Error: a non static member reference must be relative to a specific object
In C++, how to you avoid "a non static member reference must be relative to a specific object" error when calling a function from a derived class in a base class without creating an instance of the derived class or make the function in the derived class static?
Basically, I'm asking if there are ways around this. I'm thinking of creating a copy constructor in base where I can pass the instance of derived class by reference but I'm not sure if this is possible.
I've the instance of the derived class in my int man function and I don't want to create another derived class instance in my class header file.
Can I use a base class pointer to the address of the derived object?
2
u/[deleted] Jun 20 '24
I've had errors like these before. I don't know if this is the fix you're looking for but this is what I was doing wrong.
Say I have a function defined in myHeader.h
This creates my function as a regular void meaning it doesn't return any value.
Now say I write code for this function and I want to use it in my main.cpp file. What I believe you're trying to do is write it like:
But this wont work as this isn't a static object, you'd have to write it like:
or make your function static like:
and then write it like this in you're main function:
I'm sorry if this wasn't the answer you were looking for. It would be a huge help if you could post the code throwing the problem.
1
u/Mighty555 Jun 21 '24
Update: Implemented suggestions from u/jaynabonne code works well!
1
u/Maypher Moderator Jun 20 '24
What exactly are you trying to accomplish?
A method of a class is meant to use/manipulate the data belonging to an instance of a class and static methods are basically functions inside the class' namespace.
So you either want to manipulate the data of the instance and use a regular method or want to associate a function to the class to do something like a factory pattern or singleton.
There shouldn't be a reason you aren't able to access a parent's method from a child class...
1
u/jaynabonne Jun 20 '24 edited Jun 20 '24
I'm not really sure what you're trying to do here, so I'm going to have to throw some thoughts out.
First, it's unclear why the base Button class is interacting with Upload data at all. Is this something common to all buttons (e.g. the PauseButton and PlayButton as well)? If not, then you really don't want to be doing it there.
I'm also unsure whether the Upload data you want is meant to come from the actual UploadButton instance that has that Button as its base type (in other words, you want to get the upload data from itself - which only works if the Button happens to be an UploadButton) or whether you're trying to get it from a separate object. Regardless of which, you might be better off having a way to pass the data in that you want rather than muck around with actual UploadButton instances. The base class really just cares about the data, not where it's coming from. And you have coupled Button to UploadButton in a way that can't be good, especially since UploadButton is itself a Button... (my head hurts)
To be honest, when I first read your question, I was trying to figure out how the Button class could reference a derived class since the derived class would have to reference Button to begin with. A nice circular reference. You might be better off separating out the part Button wants from UploadButton and then derive UploadButton from that, so that Button only works with the subset of UploadButton it cares about.
But this all really depends on what you're trying to do. :)
1
u/BobbyThrowaway6969 Jun 20 '24
Avoid it by using the function the way it was intended. If it's non-static, it needs an instance to operate on:
not
1
u/chrisrko Moderator Aug 08 '24
INFO!!! We are moving to r/stackoverflow !!!!
We want everybody to please be aware that all future posts and updates from us will from now on be on r/stackoverflow
We made an appeal to gain ownershift of r/stackoverflow because it has been abandoned, and it got granted!!
So please migrate with us to our new subreddit r/stackoverflow ;)