C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Class problem

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Tommy Lang
Guest





PostPosted: Mon Sep 29, 2003 10:15 pm    Post subject: Class problem Reply with 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'

How do I avoid this error??
Does anyone know what I am doing wrong? Tips??

Thanks,
T
Back to top
jeffc
Guest





PostPosted: Mon Sep 29, 2003 10:53 pm    Post subject: Re: Class problem Reply with quote




"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





PostPosted: Mon Sep 29, 2003 11:06 pm    Post subject: Re: Class problem Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.