RUSES
Would you like to react to this message? Create an account in a few clicks or log in to continue.
May 2024
MonTueWedThuFriSatSun
  12345
6789101112
13141516171819
20212223242526
2728293031  

Calendar Calendar


Classes in [C++]

3 posters

Go down

Classes in [C++] Empty Classes in [C++]

Post  lee Fri Feb 24, 2012 8:54 am

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? >.<
lee
lee

Posts : 22
Join date : 2012-02-23

Back to top Go down

Classes in [C++] Empty Re: Classes in [C++]

Post  John Fri Feb 24, 2012 9:39 am

int main man Smile
John
John
Admin

Posts : 36
Join date : 2012-02-23
Age : 32

https://ruses.forumotion.co.uk

Back to top Go down

Classes in [C++] Empty Re: Classes in [C++]

Post  Alex Fri Feb 24, 2012 9:46 am

In the class you've defined colour as a function that returns an integer

Instead of

int colour();
it should be
int colour;
Alex
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Classes in [C++] Empty Re: Classes in [C++]

Post  Alex Fri Feb 24, 2012 9:47 am

pnumber, ver and hor are also functions when they should be variables

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
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Classes in [C++] Empty Re: Classes in [C++]

Post  lee Fri Feb 24, 2012 9:54 am

Thank you so much it works now Very Happy This has been bugging me for ages!
lee
lee

Posts : 22
Join date : 2012-02-23

Back to top Go down

Classes in [C++] Empty Re: Classes in [C++]

Post  Alex Fri Feb 24, 2012 9:59 am

Np Smile

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
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Classes in [C++] Empty Re: Classes in [C++]

Post  lee Fri Feb 24, 2012 11:42 am

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.
lee
lee

Posts : 22
Join date : 2012-02-23

Back to top Go down

Classes in [C++] Empty Re: Classes in [C++]

Post  Alex Fri Feb 24, 2012 11:53 am

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
Alex

Posts : 67
Join date : 2012-02-23
Location : Come at me bro

Back to top Go down

Classes in [C++] Empty Re: Classes in [C++]

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum