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!
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.
}
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>
usingnamespace std;
int main()
{
Polygon myPolygon;
...
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?
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.
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?
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
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...
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!