• Advertisement
Sign in to follow this  

Cannot Access Class Functions and Variables

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using classes and multiple files. I've defined each class in a header file, a class function file for each class, and I have a main.cpp file. I define the class objects in the main.cpp file. Now the problem is, because I'm not defining the class object until main, one of my classes can't access the class function and (public) variable of another. How do I accomplish this? Thanks in advance, Steve. Classes csBoard csInput Objects obBoard obInput Problem lines in csInput iMovepiece = obBoard.iBoard[iSqr]; obBoard.update(iMovepiece, iMovefrom, iMoveto); Clearly class object obBoard hasn't been defined yet (just the class definition) so gives a compiler error. It doesn't work with the class name csBoard either. Main File csBoard obBoard; csInput obInput;

Share this post


Link to post
Share on other sites
Advertisement
obBoard undeclared identifier. That's in the csInput class - I can see why it's complaining - a class object hasn't yet been created. But I don't know how to resolve the problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by Steve Elliott
obBoard undeclared identifier.


You are going to have to post actual code. Try main.cpp, and the headers for the included classes. Use [source][/source] to get a ice syntax highlighted box.

Share this post


Link to post
Share on other sites

#ifndef BOARD_H
#define BOARD_H

class csBoard {
public:
int iBoard[64];
int *pBoard;
csBoard();
~csBoard();
void update(int iMovepiece, int iMovefrom, int iMoveto);
void draw();
};

#endif

#ifndef INPUT_H
#define INPUT_H

class csInput {
int iMove, iMovepiece, iMovefrom, iMoveto;
int iSqr, iConvertcol, iConvertrow;
char cCol, cRow;
bool bError, bPlaygame;

public:
csInput();
~csInput();
bool getmove();
void draw();
};

#endif

//----------------------------------------------------------------------------------

bool csInput::getmove()
{
// problem lines

iMovepiece = obBoard.iBoard[iSqr];

if (bPlaygame)
obBoard.update(iMovepiece, iMovefrom, iMoveto);

return(bPlaygame);
}

//----------------------------------------------------------------------------------

bool bPlaygame = true;

//----------------------------------------------------------------------------------

#include <iostream>
using namespace std;

#include "board.h"
#include "input.h"

//----------------------------------------------------------------------------------

int main()
{
csBoard obBoard;
csInput obInput;

do {
obBoard.draw();
obInput.draw();
bPlaygame = obInput.getmove();
}
while(bPlaygame);

return 0;
}




[Edited by - Steve Elliott on November 3, 2007 8:13:04 AM]

Share this post


Link to post
Share on other sites
You have no obBoard declared in the csInput class, that why you get an undeclared identifier error. And in the main() you didn't declare the bPlaygame var.

Share this post


Link to post
Share on other sites
Hi, the method GetMove uses obBoard which is declared in the main function. You should give a csBoard object as parameter to GetMove. That way in the main function, you may have

bPlaygame = obInput.GetMove(obObject);

edit: and don't forget to declare bPlaygame, as xissburg pointed

Share this post


Link to post
Share on other sites
Guys bPlaygame is declared, otherwise I would have listed another error message (I didn't copy and paste all the code). And I know obBoard isn't declared (I said that in my first post). Just don't understand the syntax required to solve the problem. :-)

KOzymandias that won't work because it's a compile error - not a runtime error.

Share this post


Link to post
Share on other sites
issburg, yes I edited the post to prevent further confusion. :-)

Ok using this code now compiles - but it doesn't work correctly. At least the thing is compiling now.


#ifndef INPUT_H
#define INPUT_H

class csInput {
int iMove, iMovepiece, iMovefrom, iMoveto;
int iSqr, iConvertcol, iConvertrow;
char cCol, cRow;
bool bError, bPlaygame;

public:
csInput();
~csInput();
bool getmove(csBoard obBoard);
void draw();
};

#endif


bPlaygame = obInput.getmove(obBoard);



[Edited by - Steve Elliott on November 3, 2007 9:56:42 AM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement