Hi,
I'm new to C++ programming. I've used Qbasic in the past, as well as Borland Delphi, but I decided that I'd probably learn something as commenly known as C++
after reading through the tutorials here, I tried my hand on some of the possibilities, but I got stuck trying to figure functions out. Below is my code, I don't see why it shouldn't work...
I'm probably missing something really obvious, but it doesn't compile telling me that "PlayerDir" is undeclared, or with the other approach, Player1.Code:#include <iostream> using namespace std; class PlayerObj // Standard way of defining the class { public: PlayerObj(); // Constructor ~PlayerObj(); // Destructor void setspeed ( int p ); int readspeed(); void Setdir ( int d ); int Readdir(); char Textface(); protected: int Playerspeed; int PlayerDir; }; PlayerObj::PlayerObj() { Playerspeed = 0; PlayerDir = 3; } PlayerObj::~PlayerObj() { //Destructors do not accept arguments } void PlayerObj::setspeed ( int p ) { Playerspeed = p; } int PlayerObj::readspeed() { return Playerspeed; } void PlayerObj::Setdir (int d ) { PlayerDir = d; } int PlayerObj::Readdir() { return PlayerDir; } char PlayerObj::Textface() { // Here's the problem... I've attempted to do it as depicted below this line, // as well as the method depicted below that. Neither worked... if ( PlayerDir > 8 ) { Playerdir = 1 ; } if ( Player1.Readdir() == 1 ) return "Up"; if ( Player1.Readdir() == 2 ) return "Up + Right"; if ( Player1.Readdir() == 3 ) return "Right"; if ( Player1.Readdir() == 4 ) return "Right + Down"; if ( Player1.Readdir() == 5 ) return "Down" if ( Player1.Readdir() == 6 ) return "Down + Left"; if ( Player1.Readdir() == 7 ) return "Left"; if ( Player1.Readdir() == 8 ) return "Left + Up;" } int main() { PlayerObj Player1; Player1.setspeed ( 100 ); cout<< Player1.readspeed(); cout<<"\nPlayer1 is facing: "<< Player1.Textface() <<"\n"; cin.get(); }
Any help would be appreciated!