# Why would this not compile... (c++)

This topic is 1282 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Screwing around today with c++ classes/objects trying to plant syntax in my brain through repetition.

I created my own little point class and it compiles and tests justs fine.

#ifndef POINT_H
#define POINT_H

#include<iostream>
#include<string>
#include<vector>
//Point.h

class Point
{
public:
Point();
Point(int xIn, int yIn);
void setPoint(int xIn, int yIn);

void moveUp();
void moveDown();
void moveRight();
void moveLeft();

void print();

int getX();
int getY();

private:
int x;
int y;
};

#endif

//Point.cpp

#include<iostream>
#include<string>
#include<vector>
#include"Point.h"

using namespace std;

Point::Point()
{
setPoint(-1,-1);
}

Point::Point(int xIn, int yIn)
{
setPoint(xIn,yIn);
}

void Point::setPoint(int xIn, int yIn)
{
this->x = xIn;
this->y = yIn;
}

void Point::moveUp(){this->x++;}
void Point::moveDown(){this->x--;}
void Point::moveRight(){this->y++;}
void Point::moveLeft(){this->y--;}

void Point::print()
{
cout << "X: " << this->x << endl;
cout << "Y: " << this->y << endl;
}

int Point::getX(){return x;}
int Point::getY(){return y;}



I then created a Stats class, which is basically the exact same thing. An object with int data members... But this one gives me a warning, and a few errors. Here is the code and the errors/warning.

//Stats.cpp
#include<iostream>
#include"Stats.h"

using namespace std;

Stats::Stats()
{
setStats(2,10,1,0);
}

Stats::Stats(int attackIn, int healthIn, int agilityIn, int manaIn)
{
setStats(attackIn, healthIn, agilityIn, manaIn);
}

int Stats::getAttack(){return this->attack;}
int Stats::getHealth(){return this->health;}
int Stats::getAgility(){return this->agility;}
int Stats::getMana(){return this->mana;}

void Stats::setAttack(int attackIn){this->attack = attackIn;}
void Stats::setHealth(int healthIn){this->health = healthIn;}
void Stats::setAgility(int agilityIn){this->agility = agilityIn;}
void Stats::setMana(int manaIn){this->mana = manaIn;}

void Stats::setStats(int attackIn, int healthIn, int agilityIn, int manaIn)
{
this->attack = attackIn;
this->health = healthIn;
this->agility = agilityIn;
this->mana = manaIn;
}

void Stats::print()
{
cout << "Attack: " << this->attack << endl;
cout << "Health: " << this->health << endl;
cout << "Agility (mpt): " << this->agility << endl;
cout << "Mana: " << this->mana << endl;
}

//Stats.h
#ifndef STATS_H
#define STATS_H

#include<iostream>
#include<string>
#include<vector>

class Stats
{
public:
Stats();
Stats(int attackIn, int healthIn, int agilityIn, int manaIn);
void setAttack(int attackIn);
void setHealth(int healthIn);
void setAgility(int agilityIn);
void setMana(int manaIn);
void setStats(int attackIn, int healthIn, int agilityIn, int manaIn);

void print();

int getAttack();
int getHealth();
int getAgility();
int getMana();

private:
int attack;
int health;
int agility;//Moves per turn
int mana;
};

#endif

//testing file
GNU nano 2.2.6                                   File: tester.cpp

#include<iostream>
#include<string>
#include<vector>
#include"Stats.h"

using namespace std;

int main
{
Stats def;
Stats nonDef(1,1,1,1);

return 0;
}



What is the difference between these two? Also, I should note that Stats.cpp compiles just fine when compiling without the tester. I'm sure I can get it to compile, but my question is what is the difference between these objects compiling wise? Is my simple tester to blame? Simplicity seems to be my Achilles heel... There is always something simple I overlook.

Here is the error:

clobs@LinuxTop:~/Documents/reGnob/sfml/practice/VectorMap/Classes$g++ -c tester.cpp tester.cpp:6:5: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default] tester.cpp:8:8: error: expected primary-expression before ‘def’ tester.cpp:8:8: error: expected ‘}’ before ‘def’ tester.cpp:8:8: error: expected ‘,’ or ‘;’ before ‘def’ tester.cpp:11:2: error: expected unqualified-id before ‘return’ tester.cpp:12:1: error: expected declaration before ‘}’ token clobs@LinuxTop:~/Documents/reGnob/sfml/practice/VectorMap/Classes$ nano Stats.h
clobs@LinuxTop:~/Documents/reGnob/sfml/practice/VectorMap/Classes$g++ -c Stats.cpp clobs@LinuxTop:~/Documents/reGnob/sfml/practice/VectorMap/Classes$



Any tips?

Edited by Clobslee

##### Share on other sites

Found the problem. Forget I asked!

##### Share on other sites

Found the problem. Forget I asked!

This thread would be much more useful if you cared to explain what the problem was.

##### Share on other sites

Tried that code in Visual Studio C++ 2010 Express and didn't get any of those errors, but I got an error because "int main {" is not valid, it shuld be "int main() {" or the version with arguments. After adding those () the code compiled.

##### Share on other sites

Found the problem. In the ORIGINAL version of the post, the main function was missing parentheses, which caused it to be interpreted as an extended initializer list instead of a function. Tried it out with GCC 4.8 on openSUSE, and I get the same warning and errors he got.

The code that's currently in the post compiles fine. (I checked the post history to see what was changed.)

Please don't post looking for how to fix code, then post "I got it" and change the original post to have the fixed code so that people get really confused if they decide to try to see what's wrong with your code.

I agree and I apologize. I had a few too many cups of coffee and was a walking brain fart that afternoon. Thanks all who tried to help. I love how quickly someone will come running to help me with a problem on this site... The last thing I want to do is discourage that!

EDIT:: Also I changed the code back to when my issue was occurring.

Edited by Clobslee

• 9
• 23
• 10
• 19