Why won't this compile ?

Hi,

Here's a simple class that I can't get to work. I can't for the life of me see where the problem is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class v_Rect
{
public:
    wxPoint m_Pos;
    wxSize  m_Size;

public:
    v_Rect() { }

    v_Rect( const wxPoint& aPos, const wxSize& aSize ) :
            m_Pos( aPos ), m_Size( aSize )
    {}

    wxPoint Centre()
    {
        return wxPoint( m_Pos.x + (m_Size.x >> 1), m_Pos.y + (m_Size.y >> 1) );
    }
}


In code where I use it for example:

someFunc(v_Rect * aRect);

Compiler error is:
error: ‘v_Rect’ was not declared in this scope.

and,

v_Rect aRect;

Compiler error is:
error: ‘v_Rect’ does not name a type.

I don't have much experience at writing classes, can someone please point out my problem ?

I'm using Linux GCC compiler.
Last edited on
You need a ';' after the last '}'.
Are someFunc(v_Rect * aRect) and v_Rect aRect declared on the same scope of your v_Rect class?
I wish it was that simple !! there is a ';' in the real code, otherwise I would have picked up on the error message.

file1.h contains class definition
file2.h creates an object with "v_Rect m_Box;"
file2.cpp:
1
2
3
    v_Rect screen_rect;
    screen_rect = mBox;
    LineFunc( &mBox, dc, Cursor.x - dx, Cursor.y, Cursor.x + dx, Cursor.y );       


file3.h has the function definition.
1
2
void 
    void LineFunc(v_Rect * mBox, wxDC * dc, int x1, int y1, int x2, int y2, int width);


file1 has #include directives for file2 and file3.
Ok, it sounds like you might have a circular dependency. Does file1.h include file2.h and vice versa?

Yeah, jsmith is probably right...if they are, use header protection (or don't have them include each other...)

Ex:
1
2
3
4
5
//header x
#ifndef HEADER_H
#define HEADER_H
//code
#endif 
No, it's not a case of circular dependency and my header files all have header protection.

I did find that file2 was missing #include "file1.h" and that fixed some problems.

But what I have is file1 depends on a class in file2 and file2 depends on another class in file1.
If I compile file2, it stops because some class in file1 has not been declared, and visa versa.
It's the chicken before the egg situation...maybe forward declaration might solve it, I'll go and try...
It seems like your problem is you are declaring the functions inside of the .h files...am I right? If you are, put them in the corresponding .cpp files, and it should solve your problems.
Topic archived. No new replies allowed.