 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Tommy Lang Guest
|
Posted: Mon Sep 29, 2003 10:15 pm Post subject: Class problem |
|
|
I have created my own class called MyClass.
I then have created three instances/variables of the class in main...
int main(){
MyClass One;
MyClass Two;
MyClass Three;
//In the constructor of the class I have given all the above variables
a value.
//I want to call a function that calculates and return the sum, like
this:
Three = One.Add(Two); //function Add returns a double
.......
But I end up getting an error saying that...... Add' : cannot convert
parameter 1 from 'class MyClass' to 'double'
How do I avoid this error??
Does anyone know what I am doing wrong? Tips??
Thanks,
T
|
|
| Back to top |
|
 |
jeffc Guest
|
Posted: Mon Sep 29, 2003 10:53 pm Post subject: Re: Class problem |
|
|
"Tommy Lang" <mums_se (AT) yahoo (DOT) se> wrote
| Quote: | I have created my own class called MyClass.
I then have created three instances/variables of the class in main...
int main(){
MyClass One;
MyClass Two;
MyClass Three;
//In the constructor of the class I have given all the above variables
a value.
|
There isn't a way to give *those* variables a "value" in the constructor.
| Quote: | //I want to call a function that calculates and return the sum, like
this:
Three = One.Add(Two); //function Add returns a double
But I end up getting an error saying that...... Add' : cannot convert
parameter 1 from 'class MyClass' to 'double'
|
You're going to have to show the entire class, including the constructor and
Add functions. I suspect you're confusing the actual class with a variable
contained insided the class.
|
|
| Back to top |
|
 |
Default User Guest
|
Posted: Mon Sep 29, 2003 11:06 pm Post subject: Re: Class problem |
|
|
Tommy Lang wrote:
| Quote: |
I have created my own class called MyClass.
I then have created three instances/variables of the class in main...
int main(){
MyClass One;
MyClass Two;
MyClass Three;
//In the constructor of the class I have given all the above variables
a value.
//I want to call a function that calculates and return the sum, like
this:
Three = One.Add(Two); //function Add returns a double
......
But I end up getting an error saying that...... Add' : cannot convert
parameter 1 from 'class MyClass' to 'double'
|
In the future, show us complete, minimal code. But the problem is
obvious, the variable Three is not of type double, it is of MyClass.
Just exactly what your compiler told you. To fix it, you either need an
overloaded = operator (way overkill) or just assign the result to the
class member. You didn't show us the class definition, so we can't tell
what that is.
Your C++ book should explain this.
Brian Rodenborn
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|