Member functions file scope error

I'm new to C++ from Java and I'm trying to port a program from Java into C++.

I have a class called Rect (a rectangle of course) and I have a static function attached to it for testing intersections. Now I tried the following:

1
2
3
static int Rect::intersects(Rect& a, Rect& b){
	return !(a.x > b.x+(b.width-1) || a.x+(a.width-1) < b.x || a.y > b.y+(b.height-1) || a.y+(a.height-1) < b.y);
}


And it threw the following error (in Visual Studio here folks):
1
2
1>Rect.cpp
1>c:\documents and settings\aaron\my documents\cpp\of_prerelease_v0.05_windows_vs_fat\of_prerelease_v0.05_windows_vs_fat\app\breakout\engine\rect.cpp(22) : error C2724: 'Rect::intersects' : 'static' should not be used on member functions defined at file scope


Does this mean I can't use the type Rect in any static function extending from Rect then? It's a tad annoying because I find it easier to read having a function test two members than one comparing itself to another, plus I'd rather not clutter up my scope and keep these random functions tied to the class they're about.
Remove static.
From what I see you don't need it here. In c++ static means 'create only 1 instance' or in the case of functions 'can handle static members and variables' - unless your Rect &a and b are static, there is no reason your function should be.
Last edited on
Yes but I'm used to using the static keyword to add a scope to a function. In the above instance, (unless I'm mistaken) if I lose the static keyword I have to create an instance of Rect to operate a function that applies to two other Rects. Which is a bit silly.

Bit silly you might counter to refer to two other Rects statically from a Rect, but it allows me to put a function that only works on Rects in the Rect class file. Yes I could write a function in there separate, but then I've got a possible namespace issue. Sticking inside Rect just seems a lot tidier. And I can do this in every other language it seems except C++. How do I get this functionality back into my C++ work?
'static' can indeed be used in C++ to do what you want - to create a Static Member Function.
The compiler message is actually telling you that the 'static' keyword is not valid on the definition of the method, it should only be used in the class definition.
So the following compiles
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Rect
{
private:
	int x;
	int y;
	int width;
	int height;
public:
	static bool intersects(Rect& a, Rect& b);  //Use 'static' in declaration
};

bool Rect::intersects(Rect& a, Rect& b)    //no 'static' - compiler know that from class declaration
{
	return !(a.x > b.x+(b.width-1) || a.x+(a.width-1) < b.x || a.y > b.y+(b.height-1) || a.y+(a.height-1) < b.y);
}

int main()
{
	Rect myRect1,myRect2;
	bool theSame;
	theSame = Rect::intersects(myRect1,myRect2);
	return 0;
};

I have changed the result type to bool, as that appears to be your intent.
I'll leave it to one of the experts to explain why static should only appear in the declaration - I sometimes feel the real reason for things like that are to make life harder for people new to the language:-)
Last edited on
Thanks, that clears things up brilliantly.
Topic archived. No new replies allowed.