Classes in [C++]
3 posters
RUSES :: Programming :: Help
Page 1 of 1
Classes in [C++]
So I am having serious beef with classes in C++. I am trying to make a really simple text based chess game using classes but I cannot get classes to work for the life of me. It's not even complicated stuff and the notes aren't being very helpful. Anyway heres my problem
The header is
#include <iostream>
using namespace std;
class castle
{
public:
castle();
~castle(); //used when piece is destroyed
int pnumber();
int colour();
int ver(); //position on board
int hor(); //position on board
};
and the main
#include "chess.h"
void main()
{
castle cas1;
cas1.colour = 1;
}
I just cannot get it to compile - tried many different waits of creating the class but I want to make it and give it all of the parameters included in the header. Where am I going wrong guys? >.<
The header is
#include <iostream>
using namespace std;
class castle
{
public:
castle();
~castle(); //used when piece is destroyed
int pnumber();
int colour();
int ver(); //position on board
int hor(); //position on board
};
and the main
#include "chess.h"
void main()
{
castle cas1;
cas1.colour = 1;
}
I just cannot get it to compile - tried many different waits of creating the class but I want to make it and give it all of the parameters included in the header. Where am I going wrong guys? >.<
lee- Posts : 22
Join date : 2012-02-23
Re: Classes in [C++]
In the class you've defined colour as a function that returns an integer
Instead of
int colour();
it should be
int colour;
Instead of
int colour();
it should be
int colour;
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Classes in [C++]
pnumber, ver and hor are also functions when they should be variables
Change your class to
Change your class to
- Code:
class castle
{
public:
castle();
~castle(); //used when piece is destroyed
int pnumber;
int colour;
int ver; //position on board
int hor; //position on board
};
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Classes in [C++]
Thank you so much it works now This has been bugging me for ages!
lee- Posts : 22
Join date : 2012-02-23
Re: Classes in [C++]
Np
Also to note, it's standard to make all your variables private members and use functions to access them ie:
But I think this is stupid and just wastes time
Also to note, it's standard to make all your variables private members and use functions to access them ie:
- Code:
class castle
{
private:
int pnumber;
int colour;
int ver;
int hor;
public:
castle();
~castle(); //used when piece is destroyed
void setpnumber(int p) { pnumber = p; }
int getpnumber() const { return pnumber; }
//etc for all variables
//...
};
But I think this is stupid and just wastes time
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
Re: Classes in [C++]
So next issue is trying to print a pointer.
In my class I have a pointer effectively giving each object a number so I can loop through all the objects quickly. Here is the code (but I've taken all the useless stuff out to make it easier to understand
class team
{
public:
void creation();
int *pnumber;
};
------------------------------
http://team.h
team::pnumber = 1
void creation(){
*pnumber++;
}
My question is how do I then print each objects individual *pnumber. I tried making a pointer to the pointer but am still having no luck.
In my class I have a pointer effectively giving each object a number so I can loop through all the objects quickly. Here is the code (but I've taken all the useless stuff out to make it easier to understand
class team
{
public:
void creation();
int *pnumber;
};
------------------------------
http://team.h
team::pnumber = 1
void creation(){
*pnumber++;
}
My question is how do I then print each objects individual *pnumber. I tried making a pointer to the pointer but am still having no luck.
lee- Posts : 22
Join date : 2012-02-23
Re: Classes in [C++]
If by object you mean each chess piece, you could remove the need for pointers by doing:
- Code:
enum chessType { castle,king,queen }; //etc
class chessPiece
{
public:
chessPiece();
~chessPiece(); //used when piece is destroyed
chessType Type;
int pnumber;
int colour;
int ver;
int hor;
};
int main()
{
//make all pieces and push them into a vector
vector<chessPiece> cp;
chessPiece temp;
temp.Type = castle;
//etc
cp.push_back(temp);
//rest of pieces
for (int i=0;i<cp.size();i++)
{
//cp[i]
//loops through all pieces
}
}
Alex- Posts : 67
Join date : 2012-02-23
Location : Come at me bro
RUSES :: Programming :: Help
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum