Cant initialize a class object in main.cpp

Mar 18, 2012 at 8:08pm
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!
Mar 18, 2012 at 8:11pm
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 Mar 18, 2012 at 8:12pm
Mar 18, 2012 at 8:46pm
You're definitely declaring the Polygon class somewhere before
Polygon myPolygon; ?
Mar 18, 2012 at 9:40pm

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 Mar 18, 2012 at 9:41pm
Mar 18, 2012 at 9:54pm
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
Mar 18, 2012 at 10:06pm
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.
Mar 18, 2012 at 10:06pm
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.
Mar 18, 2012 at 10:38pm
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 Mar 18, 2012 at 10:47pm
Mar 18, 2012 at 10:41pm
Let's see the definition of Polygon.
Mar 18, 2012 at 10:53pm
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.
Mar 18, 2012 at 10:53pm
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
Mar 18, 2012 at 10:59pm
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?
Mar 19, 2012 at 3:47am
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.
Mar 20, 2012 at 12:10am
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
Mar 20, 2012 at 12:38am
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.
Mar 21, 2012 at 9:51am
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...
Mar 21, 2012 at 10:09am
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.