[Code::Blocks] Error: 'Polygon' does not name a type

So I'm writing some simple graphic routines for C++ using the OpenGL methods. I wrote one class, Polygon, that has functions for drawing geometric primitives like circles, rectangles, triangles, etc., and a function for n-gons. I included Polygon.h in my main file and want to see if my methods actually work, but I keep getting an error that tells me that Polygon does not name a type, yet I know it does. Here's what I have:

Polygon.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
30
31
32
33
34
35
36
37
38
39
40
#ifndef POLYGON_H
#define POLYGON_H

#include "Color.h"
#include "Rect.h"
#include "Point.h"
#include <GL/gl.h>
#include <windows.h>

class Polygon
{
    public:
        Polygon();
        virtual ~Polygon();
        void drawLine( int, int, int, int );
        void drawLine( float, float, float, float );
        void drawLine( Point, Point );
        void drawTriangle( int, int, int, int, int, int );
        void drawTriangle( float, float, float, float, float, float );
        void drawTriangle( Point, Point, Point );
        void drawRect( int, int, int, int );
        void drawRect( float, float, float, float );
        void drawRect( Point, Point );
        void drawRect( Rect );
        void drawPolygon( int, int*, int* );
        void drawCircle( float, float, float );
        void drawPolygon( float, float*, float* );
        void drawCircle( int, int, int );
        void drawCircle( Point, int );
        void setColor( int, int, int );
        void drawCircle( Point, float );
        void setColor( float, float, float );
        void setColor( Color );
        void clearColor();
    protected:
        float red, green, blue;
    private:
};

#endif // POLYGON_H 


Polygon.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "Polygon.h"

Polygon::Polygon() {}

Polygon::~Polygon() {}

void Polygon::drawLine( int x1, int y1, int x2, int y2 )
{
    glBegin( GL_LINE );
        glVertex2i( x1, y1 );
        glVertex2i( x2, y2 );
    glEnd();
}

void Polygon::drawLine( float x1, float y1, float x2, float y2 )
{
    glBegin( GL_LINE );
        glVertex2f( x1, y1 );
        glVertex2f( x2, y2 );
    glEnd();
}

void Polygon::drawLine( Point p1, Point p2 )
{
    glBegin( GL_LINE );
        glVertex2f( p1.getX(), p1.getY() );
        glVertex2f( p2.getX(), p2.getY() );
    glEnd();
}

void Polygon::drawTriangle( int x1, int y1, int x2, int y2, int x3, int y3 )
{
    glBegin( GL_TRIANGLES );
        glVertex2i( x1, y1 );
        glVertex2i( x2, y2 );
        glVertex2i( x3, y3 );
    glEnd();
}

void Polygon::drawTriangle( float x1, float y1, float x2, float y2, float x3, float y3 )
{
    glBegin( GL_TRIANGLES );
        glVertex2f( x1, y1 );
        glVertex2f( x2, y2 );
        glVertex2f( x3, y3 );
    glEnd();
}

void Polygon::drawTriangle( Point p1, Point p2, Point p3 )
{
    glBegin( GL_TRIANGLES );
        glVertex2f( p1.getX(), p1.getY() );
        glVertex2f( p2.getX(), p2.getY() );
        glVertex2f( p3.getX(), p3.getY() );
    glEnd();
}

void Polygon::drawRect( int x, int y, int w, int h )
{
    glBegin( GL_POLYGON );
        glVertex2i( x, y );
        glVertex2i( x+w, y );
        glVertex2i( x+w, h+w );
        glVertex2i( w, y+h );
    glEnd();
}

void Polygon::drawRect( float x, float y, float w, float h )
{
    glBegin( GL_POLYGON );
        glVertex2f( x, y );
        glVertex2f( x+w, y );
        glVertex2f( x+w, h+w );
        glVertex2f( w, y+h );
    glEnd();
}

void Polygon::drawRect( Point loc, Point size )
{
    glBegin( GL_POLYGON );
        glVertex2f( loc.getX(), loc.getY() );
        glVertex2f( loc.getX()+size.getX(), loc.getY() );
        glVertex2f( loc.getX()+size.getY(), loc.getY()+size.getY() );
        glVertex2f( loc.getX(), loc.getY()+size.getY() );
    glEnd();
}

void Polygon::drawPolygon( int sides, int *xVerts, int *yVerts )
{
    glBegin( GL_POLYGON );
        for( int i = 0; i < sides; i++ )
            glVertex2i( xVerts[i], yVerts[i] );
    glEnd();
}

void Polygon::drawCircle( int x, int y, int radius )
{
    /*glBegin( GL_POLYGON );
    for( int i = 0; i <)*/
}

void Polygon::setColor( float r, float g, float b )
{
    red = r;
    green = g;
    blue = b;
    glColor3f( red, green, blue );
}

void Polygon::setColor( Color c )
{
    red = c.getRed();
    blue = c.getBlue();
    green = c.getGreen();
}


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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <windows.h>
#include <gl/gl.h>

#include "Polygon.h"
#include "Color.h"
#include "Point.h"
#include "Rect.h"

LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
void DisableOpenGL(HWND, HDC, HGLRC);

Polygon p;

void display()
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    p.drawRect( 0, 0, 1, 1 );
    glPopMatrix();
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wcex;
    HWND hwnd;
    HDC hDC;
    HGLRC hRC;
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0.0f;
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_OWNDC;
    wcex.lpfnWndProc = WindowProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = "GLSample";
    wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;
    if (!RegisterClassEx(&wcex))
        return 0;
    hwnd = CreateWindowEx(0, "GLSample", "OpenGL Sample", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
                          CW_USEDEFAULT, 256, 256, NULL, NULL, hInstance, NULL);
    ShowWindow(hwnd, nCmdShow);
    EnableOpenGL(hwnd, &hDC, &hRC);
    while (!bQuit)
    {
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                bQuit = TRUE;
            else
                TranslateMessage(&msg);
                DispatchMessage(&msg);
        }
        else
        {
            glMatrixMode( GL_PROJECTION );
            glLoadIdentity();
            glOrtho( 0.0f, 500.0f, 0.0f, 500.0f, -0.5f, 0.5f );
            glMatrixMode( GL_MODELVIEW );
            glLoadIdentity();
            glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
            glClear(GL_COLOR_BUFFER_BIT);
            glPushMatrix();
            glRotatef(theta, 0.0f, 0.0f, 1.0f);
            glBegin(GL_TRIANGLES);
                glColor3f(1.0f, 0.0f, 0.0f);   glVertex2f(0.0f,   1.0f);
                glColor3f(0.0f, 1.0f, 0.0f);   glVertex2f(0.87f,  -0.5f);
                glColor3f(0.0f, 0.0f, 1.0f);   glVertex2f(-0.87f, -0.5f);
            glEnd();
            glPopMatrix();
            SwapBuffers(hDC);
            theta += 1.0f;
            Sleep (1);
        }
    }
    DisableOpenGL(hwnd, hDC, hRC);
    DestroyWindow(hwnd);
    return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_CLOSE:
            PostQuitMessage(0);
        break;

        case WM_DESTROY:
            return 0;

        case WM_KEYDOWN:
        {
            switch (wParam)
            {
                case VK_ESCAPE:
                    PostQuitMessage(0);
                break;
            }
        }
        break;

        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }

    return 0;
}

void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC)
{
    PIXELFORMATDESCRIPTOR pfd;

    int iFormat;

    /* get the device context (DC) */
    *hDC = GetDC(hwnd);

    /* set the pixel format for the DC */
    ZeroMemory(&pfd, sizeof(pfd));

    pfd.nSize = sizeof(pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW |
                  PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;

    iFormat = ChoosePixelFormat(*hDC, &pfd);

    SetPixelFormat(*hDC, iFormat, &pfd);

    /* create and enable the render context (RC) */
    *hRC = wglCreateContext(*hDC);

    wglMakeCurrent(*hDC, *hRC);
}

void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC)
{
    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(hRC);
    ReleaseDC(hwnd, hDC);
}


Any help is appreciated.
Could it be that you compile your project as C-project?
Move line 13 from main.cpp inside WinMain. Why, I let you figure out yourself.
I made that variable global so that I could use it in display(), but I could really just pass it to that function and get the same result.

I put the line declaring my Polygon after float theta;, but now I get these errors:
error: expected ';' before 'p'
warning: statement is a reference, not call, to function 'Polygon'
warning: statement has no effect

All these refer to line 34, which says "Polygon p;"

I also modified the display() function to be display( Polygon poly ), but I got these errors:
error: variable or field 'display' declared void
error: expected ')' before 'poly'

Those both refer to line 17, which says "void display( Polygon poly )"

I'm starting to think that I somehow have my project set as a C project, not C++, mostly because I noticed that I'm using the GCC compiler, not G++... I think that matters, idk. How could I change my project to be a C++ project?
Reset code::blocks settings and make a new project. It will ask you if want a C or C++ project.
Topic archived. No new replies allowed.