Class problem

T

Tommy Lang

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
 
J

jeffc

Tommy Lang said:
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.
//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.
 
D

Default User

Tommy said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top