Page 1 of 1

Tutorial 9: Classes help

Posted: October 5th, 2017, 8:10 pm
by Radical
"a nonstatic member reference must be relative to a specific object."

This is the error message I get when I try to run the following code:

Code: Select all

#include "Platform.h"
#include "Graphics.h"

void Platform::DrawPlatform(int whatever)
{
	Graphics::PutPixel(whatever);
}
It seems I can not include PutPixel in my Platform.cpp file. I tried the same with other variables I created in Game.h and the same error popped up. (After making them public of course)

Re: Tutorial 9: Classes help

Posted: October 5th, 2017, 8:15 pm
by Yumtard
PutPixel isn't a static function, so you need to call it on an instance of the class. Use the gfx object.

gfx.PutPixel()

Re: Tutorial 9: Classes help

Posted: October 5th, 2017, 8:17 pm
by Radical
But then identifier gfx is undefined. Hold on, I will mess around and see if there's something I can do.

Re: Tutorial 9: Classes help

Posted: October 5th, 2017, 8:18 pm
by Yumtard
gfx is created in game.cpp You can pass a reference to it

void Platform::DrawPlatform(int whatever, Graphics& gfx)

Re: Tutorial 9: Classes help

Posted: October 5th, 2017, 8:39 pm
by Radical

Code: Select all

void Platform::DrawPlatform(int &x, int y, int w, int r, int g, int b, Graphics& gfx)
1>Platform.cpp
1>d:\libraries\documents\chili framework\platformer\engine\Platform.h(6): error C2061: syntax error: identifier 'Graphics'
1>Platform.cpp(7): error C2511: 'void Platform::DrawPlatform(int &,int,int,int,int,int,Graphics &)': overloaded member function not found in 'Platform'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I have the same code in the header file, so I'm not sure what it's talking about. Thanks for continuing to help troubleshoot.

Re: Tutorial 9: Classes help

Posted: October 5th, 2017, 8:47 pm
by Yumtard
It looks like you're including "Graphics.h" inside the cpp file.

Instead, you need to include it in the h file. Otherwise, the compiler wont know what you're talking about when you declare your function that takes a reference to a graphics object. Remember the cpp file is including the h file, not the other way around :)

So yeah, just move #include "Graphics.h" from the cp file to the h file

Re: Tutorial 9: Classes help

Posted: October 5th, 2017, 9:03 pm
by Radical
Ah, thank you! That did the trick. :)

Re: Tutorial 9: Classes help

Posted: October 6th, 2017, 3:16 am
by chili
Yumtard is on fire with the troubleshooting help. Much appreciated bro :)

Re: Tutorial 9: Classes help

Posted: October 6th, 2017, 8:56 am
by albinopapa
He'll be able to take over my role soon.

Re: Tutorial 9: Classes help

Posted: October 6th, 2017, 11:00 am
by Yumtard
@Chili np! Seems like a good way to improve/get some extra practice.
albinopapa wrote:He'll be able to take over my role soon.
haha, I'll never be able to do that