Click here to Skip to main content
14,924,426 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear folk,
Please help me and let me ask a stupid question.
I declared variable x as DeriveType (DeriveType is a class).
I want to assign an int y to x, e.g
int y = x
But when I compile, the compiler shows the error
error: cannot convert 'const DerivType' to 'int'
Please tell me how to solve this problem.
Thank you very much.
Yours sincerely,

This is the Class DerivType

class DerivType;

typedef DerivType (*ddf_FctPtr)(const DerivType&);

class DerivType { private: interval f, df, ddf;

public: DerivType ( ); 
DerivType ( const interval&, const interval&, const interval& ); 
DerivType ( const DerivType& );

DerivType& operator= ( const DerivType& );

friend DerivType DerivConst ( const real& );
friend DerivType DerivConst ( const interval& );
friend DerivType DerivVar   ( const real& );
friend DerivType DerivVar   ( const interval& );

friend inline const interval fValue   ( const DerivType& );  
friend inline const interval dfValue  ( const DerivType& );  
friend inline const interval ddfValue ( const DerivType& );  

friend DerivType operator+ ( const DerivType& );
friend DerivType operator- ( const DerivType& );


And I have a function likes this.

DerivType f ( const DerivType& x )
        DerivType result;
        DerivType xx;
        double sum;
        xx = x;
        //Convert x from DerivType to double
        void *pVoidx = &xx;                                     [1]
        double *pDoublex = static_cast<double*>(pVoidx);        [2]
        MLPutReal(lp, *pDoublex);

        MLGetReal(lp, &sum);

        //Convert from double to DerivType for the return value of function
        printf( "x = %f, result = %f\n", *pDoublex, sum);
        void *pSum=&sum;                                            [3]
        DerivType *pDerivTypeSum = static_cast<DerivType*>(pSum);   [4]
    return *pDerivTypeSum;

This function work properly. But my teacher said that it is not the good code, because even the code works in this case, it is still implementation-dependent
and architecture-dependent. (He gives me the link:[^])

Can you help me to improve this code, namely [1], [2], [3], [4].
My idea is how to convert from DerivType to double/int and conversely.
Thank for your reading and help. I am very appriciate about your hints.
Your sincerely

Thank you very much
Yours sincerely,
Updated 29-May-15 11:48am
CPallini 28-May-15 15:47pm
Form the code posted there is no apparent way to transform an instance of DerivType into an int. Technically you could implement a 'user-defined conversion':
but then you should know what you are doing, and this doesn't seem to be the case.
[no name] 29-May-15 18:14pm
You need to go back and ask your teacher.
nothingx11x 29-May-15 18:46pm
I am very appriciate if you can help me. If I can ask my teacher, it does not make sense I send a question here.
[no name] 29-May-15 19:13pm
Help with what? It doesn't make any sense that you post a question here that you can and should be asking your teacher. They are the one that is best able to help you with your homework assignment.
It makes even less sense to update/edit your question that you have marked as "solved". So what, exactly, is it that you expect help with?
nothingx11x 29-May-15 19:28pm
Now, my question status is not solve. I just want to ask you and everyone how to improve [1], [2], [3], [4]. What is the alternative way to implement the idea which changes from DerivType to double and conversely. Thank you

1 solution

There is nothing to explain. Assignment requires assignment-compatible object on the right. Your DeriveType is not an integer type.

With inheritance, the variable to be assigned to, and the object to be on the right of assignment operators can be of different types. This is the case when variable to be assigned to is of the base type, and the object to be assigned is of more derived type. This is possible because inheritance only add members. The variable after assignment will have the compile-time type of base class, but runtime type of the derived class. This is fine, because all compile-time members will always present in the actual runtime object. The opposite is not the case: it would give you access to non-existing members, with unpredictable results. In violation of OOP principles, it's always possible to down-cast the type with the dynamic cast, which checks up actual runtime type of the object and its assignment compatibility. You will be able to learn it later.

But integer or other primitive types have nothing to do with inheritance. The assignment rules between then are related to the "width" of the value sets. Say, the integer value on right could be beyond the range of the variable on left. But this is totally different story. You cannot assign between primitive types and classes/structures. After all, what would be your logic behind that, what would it possibly mean?

Sergey Alexandrovich Kryukov 28-May-15 11:14am
I already explain everything. Please refer to my explanation if you want to ask some follow-up questions.
"To fix code" would only make sense if I knew what do you want to achieve. It does not seem reasonable enough to discuss.

MLPutInteger definition is not show. Also, put the code only in the body of the question where you can format it properly. It's not readable in comments.

nothingx11x 28-May-15 11:23am
I am sorry to make disturb you.
The definition of MLPutInteger is MLPutInteger[pointer, int]. That means its second argument has to be an integer. In this case, I want to assign x into this argument, but in this case, x is DerivType. How can I convert x into integer?
Sergey Alexandrovich Kryukov 28-May-15 11:29am
No problem.

This is not the definition of MLPutInteger. Look, we can discuss your code only if you move it to the question. Use "Improve question".

Sergey Alexandrovich Kryukov 28-May-15 14:48pm
All right. Thank you for showing you code sample.

I looked at your code. I describe exactly what the errors are: in both cases, assignment between instance of some class and int. It makes no sense.
There is nothing to fix in this code. You have to write new code, this time understanding what you are doing.
Your question is answered.

nothingx11x 28-May-15 14:58pm
Thank for your response. But can you tell me how the new code is, I am sorry,I do not have any idea for new code. How can I fix it and say "convert" a DerivType to int variable. Thank you
Sergey Alexandrovich Kryukov 28-May-15 15:15pm
Code is not "told", it is written. Sorry, your request makes no sense. First of all, I don't know what you want to achieve. To get help, you have to ask a question.
And I don't see that you accepted my present answer (all I see is someones down-vote of 1). I answered in full.
nothingx11x 28-May-15 17:22pm
Do you mean I have to change the code of class DerivType. But in this case, can you tell me how can I change it. I am high appriciate your suggestions.
I just want to "convert" from DerivType to int.
Please help me.
Thank you
Stefan_Lang 2-Jun-15 8:11am
Although Sergey made a heroic attempt to help you out, you do not seem to understand that what you are trying to do does not make sense any more than trying to fit a car into a refrigerator! A refrigerator is not meant o hold a car, and an int is not meant to hold a variable of type DerivType.

Maybe what you meant to do is access one of the member variables of x, rather than x itself, or process a function that calculates an int value based on x?
CPallini 28-May-15 15:42pm
My 5.
Sergey Alexandrovich Kryukov 28-May-15 15:43pm
Thank you, Carlo.
Stefan_Lang 2-Jun-15 8:16am
Upvoted, even though you've spent way more effort than the question deserved. ;-)
Thank you, Stefan.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900