|
Michael Randolph wrote: I thought once you instantiate the object it sets the variables that I have for parameters in the function? Where am I forgetting something? This should work as far as I can see.
I think you are seriously mistaken here: the compiler is no magic. You declare a constructor and pass two variables in that constructor. If you don't do anything with those values, the compiler won't magically assign them to your class members. That would be crazy...
You have to assign them yourself as the other poster already told you.
Michael Randolph wrote: I thought once you instantiate the object it sets the variables that I have for parameters in the function?
Once again, the compiler is no magic, it can't guess what you want to do with the variables. What if you pass less variables than you have class members (or more) ? What if they are in different order ? In most of the cases, a class is much more than two member variables (sometimes around 100 members) and in general you pass only a few of them in the constructor. So, how will the compiler know tho which member variables he has to assign those ? That would be crazy...
|
|
|
|
|
Calling a constructor is no different from calling a normal function in this sense. The arguments supplied needs to be handled explicitly.
|
|
|
|
|
Change the constructor to assign the constructor parameters to the members, either using initialiser syntax:
Point::Point(float f_x, float f_y, float f_z) : x(f_x), y(f_y), z(f_z)
{
cout << "We're in the constructor with arguments " << (int)this << endl;
}<pre>
or explicit assignments
<pre>Point::Point(float f_x, float f_y, float f_z)
{
x = f_x;
y = f_y;
z = f_z;
cout << "We're in the constructor with arguments " << (int)this << endl;
}
As Cedric said - the compiler ain't magic - it doesn't look for similarities between parameter names and data member names and decide to assign parameters to data members automatically!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
ahh I see. That makes so much more sense! Thanks for your responses. My next question deals with inheritance. It appears that the = operator declaration is not being inherited from my class Point. I am getting this compiling error:
g++ -o inheritance inheritance.cpp
inheritance.cpp: In function ‘int main()’:
inheritance.cpp:165: error: no match for ‘operator=’ in ‘vect3 = Vector::operator+(Vector&)(((Vector&)(& vect2)))’
inheritance.cpp:135: note: candidates are: Vector& Vector::operator=(Vector&)
here is my source:
#include <iostream>
using namespace std;
class Point
{
public:
Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);
~Point();
void getXYZ(float &X, float &Y, float &Z);
float getX();
float getY();
float getZ();
void setXYZ(float X, float Y, float Z);
void setX(float X);
void setY(float Y);
void setZ(float Z);
Point operator = (Point &p);
private:
float x, y, z;
protected:
};
Point::Point(float f_x, float f_y, float f_z)
{
cout << "We're in the constructor with arguments " << (int)this << endl;
x = f_x;
y = f_y;
z = f_z;
}
Point::~Point()
{
cout << "Destructing now!!! " << (int)this << endl;
}
float Point::getX()
{
return x;
}
float Point::getY()
{
return y;
}
float Point::getZ()
{
return z;
}
void Point::setX(float X)
{
x = X;
}
void Point::setY(float Y)
{
y = Y;
}
void Point::setZ(float Z)
{
z = Z;
}
void Point::setXYZ(float X, float Y, float Z)
{
x = X;
y = Y;
z = Z;
}
void Point::getXYZ(float &X, float &Y, float &Z)
{
X = getX();
Y = getY();
Z = getZ();
}
ostream &operator <<(ostream &stream, Point &p)
{
stream << p.getX() << " " << p.getY() << " " << p.getZ();
return stream;
}
istream &operator >>(istream &stream, Point &p)
{
float x, y, z;
stream >> x >> y >> z;
p.setXYZ(x, y, z);
return stream;
}
Point Point::operator =(Point &p)
{
setX(p.getX());
setY(p.getY());
setZ(p.getZ());
return *this;
}
class Vector : public Point
{
public:
Vector(float X=0, float Y=0, float Z=0);
Vector operator +(Vector &p);
};
Vector::Vector(float X, float Y, float Z) : Point(X, Y, Z)
{
}
Vector Vector::operator +(Vector &p)
{
Vector outV;
outV.setX(getX() + p.getX());
outV.setY(getY() + p.getY());
outV.setZ(getZ() + p.getZ());
return outV;
}
int main()
{
Vector vect1(1, 2, 3);
Vector vect2(5, 4, 2);
Vector vect3;
vect3 = vect1 + vect2;
cout << vect3 << endl;
}
|
|
|
|
|
OK - a couple of issues there. Firstly, your operator= isn't inherited because the signature of operator= in Point doesn't conform to what the generated operator= in Vector wants to see - it wants this:
Point& operator = (Point const &p);
i.e. take a const reference and return a non-const reference. Having said that, returning Point by value rather than reference is also a conformant signature, as C++ function signatures depend on parameter types and CV qualification (for methods), not return value - but why return Point by value anyway?
Secondly, make your getters const methods - then they can be used on const references, as (now) taken by your operator= :
void getXYZ(float &X, float &Y, float &Z) const;
float getX() const;
float getY() const;
float getZ() const;
If you make those two changes, then your Vector's generated operator= will compile successfully.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thank you so much for your help. Looks like I have a bit of studying to do. I am not totally sure how this works but it does. Here is the fixed source in case someone needs to check it out.
#include <iostream>
using namespace std;
class Point
{
public:
Point(float f_x = 1.0, float f_y = 1.0, float f_z = 1.0);
~Point();
void setXYZ(float X, float Y, float Z);
void setX(float X);
void setY(float Y);
void setZ(float Z);
void getXYZ(float &X, float &Y, float &Z);
float getX() const;
float getY() const;
float getZ() const;
Point& operator =(Point const &p);
private:
float x, y, z;
protected:
};
Point::Point(float f_x, float f_y, float f_z)
{
cout << "We're in the constructor with arguments " << (int)this << endl;
x = f_x;
y = f_y;
z = f_z;
}
Point::~Point()
{
cout << "Destructing now!!! " << (int)this << endl;
}
float Point::getX() const
{
return x;
}
float Point::getY() const
{
return y;
}
float Point::getZ() const
{
return z;
}
void Point::setX(float X)
{
x = X;
}
void Point::setY(float Y)
{
y = Y;
}
void Point::setZ(float Z)
{
z = Z;
}
void Point::setXYZ(float X, float Y, float Z)
{
x = X;
y = Y;
z = Z;
}
void Point::getXYZ(float &X, float &Y, float &Z)
{
X = getX();
Y = getY();
Z = getZ();
}
Point& Point::operator =(Point const &p)
{
setX(p.getX());
setY(p.getY());
setZ(p.getZ());
return *this;
}
ostream &operator <<(ostream &stream, Point &p)
{
stream << p.getX() << " " << p.getY() << " " << p.getZ();
return stream;
}
istream &operator >>(istream &stream, Point &p)
{
float x, y, z;
stream >> x >> y >> z;
p.setXYZ(x, y, z);
return stream;
}
class Vector : public Point
{
public:
Vector(float X=0, float Y=0, float Z=0);
Vector operator +(Vector &p);
};
Vector::Vector(float X, float Y, float Z) : Point(X, Y, Z)
{
}
Vector Vector::operator +(Vector &p)
{
Vector outV;
outV.setX(getX() + p.getX());
outV.setY(getY() + p.getY());
outV.setZ(getZ() + p.getZ());
return outV;
}
int main()
{
Vector vect1(1, 2, 3);
Vector vect2(5, 4, 2);
Vector vect3;
vect3 = vect1 + vect2;
cout << vect3 << endl;
}
|
|
|
|
|
I am using Microsoft web browser using Active X Control to display Power Point file. But when it is displaying in the web browser a vertical scroll bar is displaying. How can I remove this Scroll bar?
|
|
|
|
|
If the application is in MFC then you can call the method which hide scrollbar of your Activex control through IDispatch interface. I do n't remember exactly which property you have to set. In case it is pure c++, then you have to write your own helper class.
modified on Thursday, March 26, 2009 10:16 AM
|
|
|
|
|
Hi to everyone!
I am writing because i need help with making this "game". I am looking for anyone who is familiar with mfc more than i am to write me a code(or send me project) for following program:
- game randomly show characters and numbers from keyboard(ONLY ALPHABETIC and NUMBERS from 1-9) and player pushes appropriate buttons on keyboard
- characters must be shown on panel in specific timing
-there must be option to set up time for game with slider
- with progress bar time must be shown
- start and stop buttons for game
- on panel must be shown: HOW MANY BUTTONS PLAYER HIT and HOW MANY MISSED, how many buttons player didn't manage to hit in time and CHARACTER which must be pressed at the moment
I would be very grateful to anyone who would make this program alive;) This "game" or program is very important for me and must be made fast.
Thanks,
Best regards
|
|
|
|
|
Aljaz111 wrote: This "game" or program is very important for me and must be made fast.
Then go on rentacoder and pay somebody to do it.
Seriously, you really think somebody will write your program for you without being paid ?
|
|
|
|
|
I didn't say i wouldn't pay. I am just looking for one who is prepared to help me;)
|
|
|
|
|
Please don't blame him. Maybe you aren't aware about, but, neverthless, this is no more a forum for developers, it is instead the bazar of the code prêt-à-porter.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Still anyone can help me and give few tips on how to show characters with time and how to make slider for time for showing characters on cdialog panel?
|
|
|
|
|
It looks like you're improving on your requests. Now, anyway you should tell us what have you done so far and what are the hurdles on your way.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
CPallini wrote: and what are the hurdles on your way.
I've identified the biggest hurdle. Biggest hurdle == Aljaz111
|
|
|
|
|
You're such a bad guy.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
evry body is bad heer
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
I don't know how to start.. i am thinking about making an array with all alphabetic chars(arrayAlpha[25]) and another array for numbers(arrayNum[10]). But i don't know how to randomly choose one of them..i know function for example rnd()%1+1..but how to randomly select variable with char or number from array. Next how to show them depending on time? This is all for now.
Thanks
|
|
|
|
|
Aljaz111 wrote: I don't know how to start
Indeed, this is common for folk that wake up one morning after a night out of binge drinking and decide they are going to be software developers. Then they come to internet forums like this one and ask people questions like yours. Then people that frequent these types of forums and have an education in computer information science inform them that you start with REQUIREMENTS.
Aljaz111 wrote: i am thinking about making an array with all alphabetic chars(arrayAlpha[25]) and another array for numbers(arrayNum[10]). But i don't know how to randomly choose one of them..i know function for example rnd()%1+1
So you have a plan and have made some assumptions. The assumptions are without form and the Plan is completely without substance. The darkness is upon those that read your post. Verily we say unto you:
"It is a crock of sh*t, and it stinketh".
"It is a pail of dung and none may abide by the odor thereof."
|
|
|
|
|
led mike wrote: So you have a plan and have made some assumptions. The assumptions are without form and the Plan is completely without substance. The darkness is upon those that read your post. Verily we say unto you:
"It is a crock of sh*t, and it stinketh".
"It is a pail of dung and none may abide by the odor thereof."
I think I already read that somewhere . Why don't you put it in your signature, this way you don't need to rewrite it each time
|
|
|
|
|
|
Well, I'm used to consider 26 letters in the alphabet.
You may use one array to store both letters and digits (it would better than using two arrays) and then randomly select the index or you may do something like (consider it a welcome-in the-forum-code-snippet):
char getRandomSymbol()
{
int i = (int) (rand() / (RAND_MAX + 1.0) * 36;
if (i < 10) return '0' +i;
return 'A'+(i-10);
}
Aljaz111 wrote: Next how to show them depending on time?
As you said, this would be the next step.
BTW at the moment your project looks a bit too ambitious.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
OK...now i have to use cedit..as i found! pallini..can u give me something that we could talk on private..
|
|
|
|
|
How to show now this charaters in editbox.. i created edit box with edit control added variable..but what next? Where to put call for function getRandomSymbol?
|
|
|
|
|
You call the getRandowSymbol function whenever you need a new random chosen letter.
For instance, suppose you want show one random letter in the edit box, then (supposing the member variable associated to the edit is a CString and it is called m_sEdit ) you may do
m_sEdit = getRandomSymbol();
eventually calling UpdataData(FALSE) method of the dialog.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|