Cant initialize a class object in main.cpp

Hello

In my program/project, I have created a class which I call Polygon, but when i try to make an instance of the class i get an error.

1
2
3
4
5
6
7
...
int main()
{
...
Polygon myPolygon;
...
}


returns:
..Main.cpp|31|error: expected ';' before 'myPolygon'

I can initialize objects "after declaring" Polygon, as:

1
2
3
4
class Polygon
{
...
} myPolygon;


I cant find a solution to the problem as it makes absolutely no sense to me.
I have the project on two different computers, one on my Ubuntu and the other one on my Windows setup, and it's the one on my Windows that is not working!

my project is a SFML project, if that has anything to do with the issue..
All help would be very appreciated!
closed account (zb0S216C)
Your object declaration seems valid, with no immediate flaws. The problem could reside within the code that proceeds it. Can you post the part of main( ) before your declaration? For example:

1
2
3
4
5
6
7
int main( )
{
    //
    // The code here...
    //
    Polygon myPolygon.
}


Wazzak
Last edited on
You're definitely declaring the Polygon class somewhere before
Polygon myPolygon; ?

Did you include in your main appilcation file( main.cpp, or whatever it's name is) the header file where you declare your class Polygon ?
Last edited on
Actually myPolygon is the first object I initialize, so the code is basically

1
2
3
4
5
6
int main()
{
Polygon myPolygon;
int gravity = 0.01;
...
}


iam going to try to reinstall codeblocks aswell and se what that will do
You need to make sure that:

1) The compiler knows what a Polygon is when it is compiling main. If your Polygon class is declared in "polygon.hpp" and defined in "polygon.cpp" and your main.cpp looks like this:

1
2
3
4
5
6
7
#include <iostream>
using namespace std;

int main()
{
  Polygon myPolygon;
  ...

then you need to #include "polygon.hpp" somewhere before main().

2) Make sure that the important bits of your class are public, like the constructors.

1
2
3
4
5
6
class Polygon
{
  vector <Point> points;
  bool is_connected;
  Polygon(): points(), is_connected( true ) { }
};

That can never be used, because everything in the class is automatically private.

3) Make sure your class's closing curly-brace } is followed by a semicolon ;

If you are unsure about using multiple files, read up here:
http://www.cplusplus.com/forum/general/13162/#msg63354

Hope this helps.
If it doesn't like the word Polygon at that point, it means you have neither defined nor declared the class by then. You must define or declare it before you use it.
But it's really strange because this line doesnt give any errors

1
2
3
4
5
int main()
{
Polygon;
...
}


Why does that work but not

1
2
3
4
5
int main()
{
Polygon myPolygon;
...
}


edit:

I made everything in the class public just for the ease of access while developing, also, the Polygon class is both declared and defined in Polygon.h, is there some advantage in using a separate file for declaring and another for defining?
Last edited on
Let's see the definition of Polygon.
is there some advantage in using a separate file for declaring and another for defining?


Every file that uses Polygon needs it declared, but you can only have it defined once in the entire program. If you have it declared in a header, and defined separately, you can #include that header in every file you need it.

If you put the declaration and the definition in the same file, you'll be unable to build the program if you #include it more than once.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Polygon {
    public:
    sf::Vector2f velocity;
    sf::Shape polygon;
    float mass;
    static int n;
    int id;
    float maxVertexDistance;
    void applyForce(sf::Vector2f);
    void applyForce(float,float);
    void addPoint(float,float,sf::Color,sf::Color);
    void move(float,float);
    void move(Polygon&);
    void move();
    void draw(sf::RenderWindow&);
    Polygon();
} myPolygon;


at the moment I have the object myPolygon right after declaring Polygon, bcuz it's the only way i can get it to work atm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Polygon {
    public:
  //sf::Vector2f velocity;
  //  sf::Shape polygon;
    float mass;
    static int n;
    int id;
    float maxVertexDistance;
  //    void applyForce(sf::Vector2f);
    void applyForce(float,float);
  //    void addPoint(float,float,sf::Color,sf::Color);
    void move(float,float);
    void move(Polygon&);
    void move();
  //   void draw(sf::RenderWindow&);
    Polygon();
};

int main()
{
  Polygon a;
}


This compiles fine, once I commented out those functions using the sf:: namespace, but they're just function declarations - there's a link error, obviously, since there's a declaration of the constructor but I don't have the code, but whatever the issue is does not appear to be a compilation error. Does this not compile on your machine?
He is making a newbie mistake that all newbies make. (Mine took me a week to figure out that I left a } out somewhere.)

Until he posts all his code to us, we cannot read his mind well enough to help.
Well my project files contain lots of code that I dont use so I'll post the essential

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

//this is Polygon.h
#ifndef _POLYGON
#define _POLYGON

class Polygon {
    public:
    sf::Vector2f velocity;
    sf::Shape polygon;
    float mass;
    static int n;
    int id;
    float maxVertexDistance;
    void applyForce(sf::Vector2f);
    void applyForce(float,float);
    void addPoint(float,float,sf::Color,sf::Color);
    void move(float,float);
    void move(Polygon&);
    void move();
    void draw(sf::RenderWindow&);
    Polygon();
} myPolygon[2];

//lots of function definitions below
//...
#endif 


and then in my main.cpp file

1
2
3
4
5
6
7
8
9
//This is the main.cpp file

#include "Polygon.h"

int main()
{
   Polygon secondPolygon;
   return 0;
}



this is all my code atm, i commented all code in the main file except the Polygon secondPolygon; statement, but i still get the
error: expected ";" before "secondPolygon"
message, this really drives me crazy....
thanks anyways
1
2
3
4
5
//this is Polygon.h
#ifndef _POLYGON
#define _POLYGON
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp> 
It compiles fine here.
I do also get two warnings on the same line, although these are not errors.

...\Main.cpp|20|error: expected ';' before 'secondPolygon'|
...\Main.cpp|20|warning: statement is a reference, not call, to function 'Polygon'|
...\Main.cpp|20|warning: statement has no effect|

could these two warning indicate something else?
whats really strange aswell is that i have another class which i call Ball and is declared and defined similarly, but with this class I have no problem creating instances...
Ok I believe I have found what the problem is

I cannot use the name Polygon, i thinks this is because of some SFML feature, so when i renamed the class to Polygons (adding an s) everything worked out fine... I still find it strange since i could declare and define Polygon.
Thanks anyways!
Topic archived. No new replies allowed.