Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C++ How to solve cannot convert in assignment ?

Posted on 2011-03-22
13
Medium Priority
?
939 Views
Last Modified: 2012-05-11
Hi Experts,

I have the following situation:

in .h file I have these declarations:

public:
struct data_vec1 {
            float x, y, z;
    };

struct data_vec3{
           float u, v;
  };


  struct delta_structure{
            data_vec1   g1;
            data_vec3   g2;

            void set_val(float x, float y, float z, float u, float v)
            {
                    g1.x = x;
                    g1.y = y;
                    g1.z = z;
                    g2.u = u;
                    g2.v = v;
            }
  };

  struct data_vec4{
            data_vec1   g1;
            data_vec3   g2;
  };

private:
data_vec4      *t_v;

In the .cpp file I try this:

t_v = new delta_structure[50];

When compiling, I get this error:
cannot convert 'delta_structure**' to 'data_vec4*' in assignment.

Can anyone tell me what am I doing wrong and how to fix this error ?

Thanks,
Peter
0
Comment
Question by:PeterVarain
  • 4
  • 4
  • 4
13 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 35193090
delta_structure is not the same as data_vec4 - they're incompatible types.

Did you mean :

        t_v = new data_vec4[50];
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 35194385
infinity is right. you can assign a pointer to point to an object of its type or sub-type if you use inheritance.

However I was trying to see what you are actually intending to do :


delta_structure and data_vec4 contain data_vec1 and data_vec2 objects. Thereafter a data_vec4 pointer is made to point to a delta structure object.

This seems to imply that you intended delta_structure and data_vec4 to be interchangeable. perhaps you intended data_vec4 to be a subtype of delta_structure??

If you didn't intend sub-typing or sub-classing then the whole idea of having a data_vec4 struct doesn't make sense. Either way you would be better off using classes to implement inheritance and polymorphism.
0
 
LVL 35

Accepted Solution

by:
sarabande earned 2000 total points
ID: 35197358
you could do

   t_v = (data_vec4*)new delta_structure[50];

or

  t_v = reinterpret_cast<data_vec4*>(new delta_structure[50]);

what would work cause both structures have identical data members. but as told before it makes no sense using two structures which are not compatible for the compiler.

instead you could drop the declaration of struct data_vec4 and simply make a typedef:

    typedef delta_structure data_vec4;

with that data_vec4 is an alias name of delta_structure and the code compiles.

Sara

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 53

Expert Comment

by:Infinity08
ID: 35201499
If you accepted that post because you're gonna use the cast, then I strongly recommend against doing that. It'll get rid of the error, yes, but it's a very bad design, and will most likely cause you serious problems later on.

The suggested typedef is a much better idea.


I do wonder though if none of the other posts helped you ?
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 35202906
I agree with Infinity. You should cast from delta_structure* to data_vec4*. A cast should be done only if there is compatibility between types. In this case even though it might seem that delta_structure and data_vec4 are similar, the question is pedagogic in nature and the similarity between structures cannot be taken for granted.

The same argument holds good for the reinterpret_cast as well as the typedef.

0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 35202918
correction:
""You should cast from delta_structure* to data_vec4*""

read as "You should NOT blindly cast from delta_structure* to data_vec4*"
0
 
LVL 35

Expert Comment

by:sarabande
ID: 35213489
i don't think the objections are justified. it is only a guess that PeterVarain will use a cast though all expert comments have explicitly recommended against. in my answer i gave a much better alternative to a cast by using a typedef. you should also see, that casting of structures which have a common part is a quite usual thing in C and even in C++ when normal inheritage cannot be used.

Sara


0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35213520
An objection was imo not needed indeed. However, I would have appreciated some feedback from PeterVarain to put my mind at ease. Given the time that has passed, I think it's safe to say we can forget about that though.

The question doesn't need to be re-opened for me. As long as the recommendations against casting are visible (which they are), I'm happy.



>> you should also see, that casting of structures which have a common part is a quite usual thing in C

It's not because it's used that it's good practice ;) imo, it's sloppy and a sign of bad design.
0
 
LVL 35

Expert Comment

by:sarabande
ID: 35213978
i know a few samples of such coding which are neither sloppy nor bad design (at least in my opinion) for example if you need to keep a common interface with c or have structures (for example bitmaps) which have a variable size but need to be contiguous. in MFC the message handlers for list control all pass a structure of NMHDR which actually is NMLISTVIEW. the following is generated code by Visual Studio:

void TestmfcDlg::OnLvnItemchangedList1(NMHDR *pNMHDR, LRESULT *pResult)
{
    LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
    // TODO: Add your control notification handler code here
    *pResult = 0;
}

Open in new window


the design was due to the usage of function pointers in generated code and because the control was implemented with a C interface only.

Sara
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35213998
I wouldn't use MFC as the reference for good design, but let's just agree to disagree heh :)
0
 
LVL 12

Expert Comment

by:trinitrotoluene
ID: 35222028
>>>> in my answer i gave a much better alternative to a cast by using a typedef.

I don't agree that it is a better alternative when we didn't even get a response to what the author wanted in the first place.
It was apparent to a certain extent that the author was attempting to achieve inheritance or sub-typing using structs and he may have been doing this unintentionally.
Before this could be confirmed an answer using typedefs and casting was introduced and the author accepting it goes to prove he did not understand the implications of what he was doing and neither was an attempt made by the expert to offer the rationale behind the proposed solution.

>>>you should also see, that casting of structures which have a common part is a quite usual thing in C and even in C++ when normal inheritage cannot be used.

it may be usual but that doesn't mean it is a good practice. And what do you mean by common part. What happens to the cast when there are uncommon parts as well?


I stand by my objection. Ofcourse the author has got what he expected so there's no point reopening or re-assigning.

0
 
LVL 35

Expert Comment

by:sarabande
ID: 35222297
trinotrotuolene, you do request more from the questioner than he is obliged to.

why do you need a response of the questioner in order to valuate my comment?

you may disagree that a typedef is better than a cast to a structure with equal members but you should find reasons for that - eh - rather unexpected opinion.

in my opinion it is two things to say 'something is good practice' or 'something often can be seen' (and therefore it might be worth to know about). i only asserted the second in my first comment to the objection and even regarding the mfc i did not say it was good design but i only said 'in my opinion' it can't be blamed as slobby or bad design. isn't there a wide field between good and bad? can anyone say here he/she never made a cast which could have been avoided?

Sara

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
I have written articles previously comparing SARDU and YUMI.  I also included a couple of lines about Easy2boot (easy2boot.com).  I have now been using, and enjoying easy2boot as my sole multiboot utility for some years and realize that it deserves …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question