Solved

C++ How to solve cannot convert in assignment ?

Posted on 2011-03-22
13
851 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
Comment Utility
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
Comment Utility
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 32

Accepted Solution

by:
sarabande earned 500 total points
Comment Utility
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
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
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
Comment Utility
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
Comment Utility
correction:
""You should cast from delta_structure* to data_vec4*""

read as "You should NOT blindly cast from delta_structure* to data_vec4*"
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Expert Comment

by:sarabande
Comment Utility
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
Comment Utility
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 32

Expert Comment

by:sarabande
Comment Utility
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
Comment Utility
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
Comment Utility
>>>> 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 32

Expert Comment

by:sarabande
Comment Utility
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Viewers will get an overview of how to make and use Drum Racks in Ableton Live. Load new Drum Rack into empty MIDI track: Fill rack with audio samples: Re-arrange sample slots as necessary: Adjust parameters of each slot to tailor each sound a…
Viewers will learn how to create and use Simpler instruments in Ableton Live. Load new Simpler into an empty MIDI track: Select a sample and drop it into sample window in Simpler: If sample is not pitched at C3, adjust tuning with Transpose para…

772 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now