Cannot Access Class Functions and Variables
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;
What errors do you get?
Anyway, this article may be helpfull, if you haven't read it already: Organizing code files in C and C++.
Anyway, this article may be helpfull, if you haven't read it already: Organizing code files in C and C++.
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.
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.
#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]
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.
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
bPlaygame = obInput.GetMove(obObject);
edit: and don't forget to declare bPlaygame, as xissburg pointed
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.
KOzymandias that won't work because it's a compile error - not a runtime error.
Oh sorry cuz the bPlaygame, now I see its declared as global(haven't you edited that post now?!hehh).
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.
[Edited by - Steve Elliott on November 3, 2007 9:56:42 AM]
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(); };#endifbPlaygame = obInput.getmove(obBoard);
[Edited by - Steve Elliott on November 3, 2007 9:56:42 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement