invalid use of member 'foo' in static member function

Aug 3, 2009 at 7:34pm
When I try to compile this:
Shape.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
 * Shape.h
 *
 *  Created on: Aug 3, 2009
 *      Author: jordy
 */

#ifndef SHAPE_H_
#define SHAPE_H_

#include <SFML/Graphics/Shape.hpp>
#include <SFML/Graphics/Rect.hpp>
#include <SFML/System/Vector2.hpp>

namespace Solids
{

class Shape : public sf::Shape, public sf::Rect<float>
{
public:
	Shape();
	virtual ~Shape();

	static Shape Rectangle(float X, float Y, float Width, float Height);
};

}

#endif /* SHAPE_H_ */ 

Shape.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * Shape.cpp
 *
 *  Created on: Aug 3, 2009
 *      Author: jordy
 */

#include "Shape.h"

namespace Solids
{

Shape::Shape()
{
}

Shape::~Shape()
{
}

Shape Shape::Rectangle(float X, float Y, float Width, float Height)
{
	Shape Rect;

	Rect.Left   = X;
	Rect.Top    = Y;
	Rect.Right  = Left + Width;
	Rect.Bottom = Top + Height;

	Rect.AddPoint(Left, Top);
	Rect.AddPoint(Right, Top);
	Rect.AddPoint(Right, Bottom);
	Rect.AddPoint(Left, Bottom);

	return Rect;
}

}

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * main.cpp
 *
 *  Created on: Aug 3, 2009
 *      Author: jordy
 */

#include <SFML/Graphics.hpp>
#include "Shape.h"

int main()
{
	sf::RenderWindow App(sf::VideoMode(640, 480, 32), "Asteroids");

	App.UseVerticalSync(true);
	App.SetFramerateLimit(60);

	Solids::Shape Box = Solids::Shape::Rectangle(0, 0, 20, 20);

	while (App.IsOpened())
	{
		sf::Event Event;
		while (App.GetEvent(Event))
		{
			if (Event.Type == sf::Event::Closed)
				App.Close();
			if (Event.Type == sf::Event::KeyPressed)
			{
				if (Event.Key.Code == sf::Key::Q)
					App.Close();
			}
		}

		App.Clear();
		App.Draw(Box);
		App.Display();
	}
}


I get these errors:
1
2
3
4
5
6
7
8
9
10
11
12
Description	Resource	Path	Location	Type
from this location	Shape.cpp	/Asteroids	line 27	C/C++ Problem
from this location	Shape.cpp	/Asteroids	line 28	C/C++ Problem
from this location	Shape.cpp	/Asteroids	line 30	C/C++ Problem
from this location	Shape.cpp	/Asteroids	line 31	C/C++ Problem
from this location	Shape.cpp	/Asteroids	line 33	C/C++ Problem
invalid use of member ‘sf::Rect<float>::Bottom’ in static member function	Asteroids		line 115, external location: /usr/include/SFML/Graphics/Rect.hpp	C/C++ Problem
invalid use of member ‘sf::Rect<float>::Left’ in static member function	Asteroids		line 112, external location: /usr/include/SFML/Graphics/Rect.hpp	C/C++ Problem
invalid use of member ‘sf::Rect<float>::Right’ in static member function	Asteroids		line 114, external location: /usr/include/SFML/Graphics/Rect.hpp	C/C++ Problem
invalid use of member ‘sf::Rect<float>::Top’ in static member function	Asteroids		line 113, external location: /usr/include/SFML/Graphics/Rect.hpp	C/C++ Problem
make: *** [Shape.o] Error 1	Asteroids		line 0	C/C++ Problem
from this location	Shape.cpp	/Asteroids	line 32	C/C++ Problem


I'm not sure why this is happening, I don't have much experience with static member functions, but I'm pretty sure I should be using it.

I'm using the latest Eclipse CDT from eclipse.org with Ubuntu 9.04.

Any help would be appreciated. :)

Thanks,
Jordy
Aug 3, 2009 at 7:42pm
Static member functions are really global functions, just inside the class's namespace. That is, they don't have a 'this' pointer and therefore can't access any other members of the class (except for other static members).

This is the code that's the trouble:

1
2
3
4
5
6
7
	Rect.Right  = Left + Width;
	Rect.Bottom = Top + Height;

	Rect.AddPoint(Left, Top);
	Rect.AddPoint(Right, Top);
	Rect.AddPoint(Right, Bottom);
	Rect.AddPoint(Left, Bottom);


Left/Top/Right/Bottom here are all referring to this->Left, this->Top, etc. Except you don't have a this pointer because the function is static.

I'm going to assume all this code is suppose to use 'Rect'... therefore you'd need to change it to:

1
2
3
4
5
6
7
	Rect.Right  = Rect.Left + Width;
	Rect.Bottom = Rect.Top + Height;

	Rect.AddPoint(Rect.Left, Rect.Top);
	Rect.AddPoint(Rect.Right, Rect.Top);
	Rect.AddPoint(Rect.Right, Rect.Bottom);
	Rect.AddPoint(Rect.Left, Rect.Bottom);
Aug 3, 2009 at 7:59pm
Thanks! It works now. :)
Topic archived. No new replies allowed.