What am I not understanding?
Code:class Player { public: Player(); void talk(); }; void Player::talk() { cout<<"Yo"; } int main() { Player::Player(); Player().talk(); return 0; }
What am I not understanding?
Code:class Player { public: Player(); void talk(); }; void Player::talk() { cout<<"Yo"; } int main() { Player::Player(); Player().talk(); return 0; }
AIM: MarderIII
You need a Player instance in main.
Player a;
a.Talk();
hth
-nv
She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."
When in doubt, read the FAQ.
Then ask a smart question.
There is another problem.
A class type is used just like a basic type. If you wanted to declare an int named number, you would do this:
int number;
Similarly, when you want to declare a variable of type Player, you do this:
Player a_player;
That calls the default constructor. You declared the default constructor:
Player();
and since you declared it, you have to define it. Since you don't want it to do anything, you would define it like this:
Player()
{
}
Typically, with a short function, you would define it inside the class like this:
The compiler provides the default constructor for you, so you don't have to declare and define it if you don't want to.Code:class Player { public: Player(){} void talk(); };
Im not sure what you mean, I tried to add that but it said that talk was not a member of player and I don't know...but the below code works I guessOriginally posted by nvoigt
You need a Player instance in main.
Player a;
a.Talk();
Code:#include <iomanip> #include <iostream> #include <limits> #include <fstream> #include <stdlib> #include <conio> #include <time> #include <ctype> class Player { public: Player::Player(int x,int y,int z) { one = x; two = y; three = z; } void talk(); private: int one, two, three; }; void Player::talk() { cout<<"Yo"<<one<<two<<three; } int main() { int x=1,y=2,z=3; Player::Player(x,y,z); Player(x,y,z).talk(); return 0; }
Last edited by Noobie; 05-16-2003 at 05:45 AM.
AIM: MarderIII
Try again:
Code:int main() { int x=1,y=2,z=3; Player p(x,y,z); p.talk(); return 0; }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Wuts the difference between
andCode:Player p(x,y,z); p.talk();
Code:Player::Player(x,y,z); Player(x,y,z).talk();
AIM: MarderIII
using a class is similar to using a struct in many ways. You define the type/class, and then you have to declare an instance of it...which is what P is in the previous example. Player is just the name of your class...P is the variable.
Kinda like "int g;" Int is the type, g is the variable. Here, Player is the type, P is the variable.
Hope that helps.
Away.
This is the good way of doing things. You create a new Player object called p. You then call a member function of p.Code:Player p(x,y,z); p.talk();
This is bad. The first line creates a new Player object but doesn't assign it to a variable name (essentially, you just wasted stack space creating an object you can never use). The first line is completely useless except if your goal is to waste space.Code:Player::Player(x,y,z); Player(x,y,z).talk();
The second line creates a second new Player object, with the same parameters as the other one (it is NOT the same object), calls one method on that object, and then the object, just like the first one, is "lost on the stack" -- you can't ever touch that object again.
I think you have a problem between the distinction of a class and an object.Wuts the difference between.....
When you declare Player as a class you are only saying to the compiler what data(data-members) it contains and what it can do(member-functions). You do NOT allocate memory for a Player(donīt really know if this is true if the class contains static data-members). On the other hand when you declare an object you are creating an instance of a class, and memory is allocated.
Create a player named p(define)
Tell the compiler what a player is(declare)Code:Player p(x,y,z);
Do you get the distinction???Code:class Player { public: Player::Player(int x,int y,int z) { one = x; two = y; three = z; } void talk(); private: int one, two, three; };
01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
Good things donīt come easy in life!!!