Functions in classes
Mar 24, 2013 at 1:20am UTC
I am hesitant to post this topic because it is the second time today that I have posted with a similar question, but I really can't figure out what is wrong. I have some simple code that moves a player in the direction you give them by changing a x,y,and z axis value. The problem that I am having is that I want to give a function access to another function, but they are in different classes and I don't know the correct syntax to do this. Here is the code that I have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <iostream>
#include <limits>
class Move
{
public :
int x;
int y;
int z;
void move_player(int x,int y, int z);
};
class Menue
{
public :
Move movement;
void menue_main(void move_player(int x, int y, int z));
};
int main()
{
Menue main;
std::cout<<main.Menue::menue_main(move_player(main.movement.x,main.movement.y, main.movement.z))<<std::endl;
return 0;
}
void Move::move_player(int x,int y,int z)
{
char direction;
std::cout<<"Which direction will you move?" <<std::endl;
std::cin>>direction;
std::cout<<std::endl;
if (direction = 'n' )
{
x++;
std::cout<<"You move north" <<std::endl;
}
if (direction = 's' )
{
x--;
std::cout<<"You move south" <<std::endl;
}
if (direction = 'e' )
{
y++;
std::cout<<"You move east" <<std::endl;
}
if (direction = 'w' )
{
y--;
std::cout<<"You move west" <<std::endl;
}
if (direction = 'u' )
{
z++;
std::cout<<"You move up" <<std::endl;
}
if (direction = 'd' )
{
z--;
std::cout<<"You move down" <<std::endl;
}
}
void Menue::menue_main(movement.move_player(movement.x,Menue::movement.y,movement.z))
{
char menue_input;
std::cout<<"What would you like to do?" <<std::endl;
std::cout<<"m:move" <<std::endl;
std::cin>>menue_input;
std::cout<<std::endl;
if (menue_input = 'm' )
{
movement.move_player(movement.x,movement.y,movement.z);
}
else
{
std::cout<<"I do not understand '" <<menue_input<<"'" <<std::endl;
}
}
If someone could please explain to me what I am doing wrong that would be great. Thanks!
Mar 24, 2013 at 7:55am UTC
see the changes.. also, while testing for conditions use "==" and not "=". read the difference between them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include <iostream>
#include <limits>
class Move
{
public :
int x;
int y;
int z;
void move_player(int x,int y, int z);
};
class Menue
{
public :
Move movement;
void menue_main();
};
int main()
{
Menue main;
main.menue_main();
return 0;
}
void Move::move_player(int x,int y,int z)
{
char direction;
std::cout<<"Which direction will you move?" <<std::endl;
std::cin>>direction;
std::cout<<std::endl;
if (direction == 'n' )
{
x++;
std::cout<<"You move north" <<std::endl;
}
if (direction == 's' )
{
x--;
std::cout<<"You move south" <<std::endl;
}
if (direction == 'e' )
{
y++;
std::cout<<"You move east" <<std::endl;
}
if (direction == 'w' )
{
y--;
std::cout<<"You move west" <<std::endl;
}
if (direction == 'u' )
{
z++;
std::cout<<"You move up" <<std::endl;
}
if (direction == 'd' )
{
z--;
std::cout<<"You move down" <<std::endl;
}
}
void Menue::menue_main()
{
char menue_input;
std::cout<<"What would you like to do?" <<std::endl;
std::cout<<"m:move" <<std::endl;
std::cin>>menue_input;
std::cout<<std::endl;
if (menue_input = 'm' )
{
int x, y, z;
std::cout << "where you want to move it, enter x, y, z;" << std::endl;
std::cout << "x: " ;
std::cin >> x;
std::cout << "y: " ;
std::cin >> y;
std::cout << "z: " ;
std::cin >> z;
movement.move_player(x, y, z);
}
else
{
std::cout<<"I do not understand '" <<menue_input<<"'" <<std::endl;
}
}
Mar 24, 2013 at 12:28pm UTC
Thank you so much! I can finally continue with this project.
Topic archived. No new replies allowed.