#include <iostream>
#include <cstdlib>

using namespace std;

struct A {
      A *B;
       };
       
       struct C{
              C*D;
              };
int main()
{
 
 
 A * Z = new A;
 C * Y = new C;
 
 Z = Y;

 
 cin.get();
 return 0;  
}

it says cannot convert Z to Y in assignment.

Dani AI

Generated

The compiler is refusing the assignment because A* and C* are different, unrelated pointer types. C++ only allows pointer assignment when the target type is identical or there is a valid conversion (for example an inheritance relationship or an explicit cast). declared Z as an A* and Y as a C*; without a relationship between A and C the assignment will not compile. was on the right track about providing a conversion, but here are safer, idiomatic alternatives.

One common fix is to make one type inherit from the other so a pointer-to-derived converts to pointer-to-base automatically. Example:

struct A {
    virtual ~A() {}
    // A's members
};

struct C : A {
    // C's members
};

A* z = new A;
C* y = new C;
z = y; // OK: C* -> A*
delete z; // safe because A::~A is virtual

If the goal is a runtime-checked cast in a polymorphic hierarchy (base -> derived), use dynamic_cast. For forcing the compiler to accept an assignment when there is no type relationship, reinterpret_cast will compile but can easily cause undefined behavior and should be avoided:

A* z = reinterpret_cast<A*>(y); // compiles but is unsafe unless the layouts are compatible

For designs where types must remain unrelated, consider templates (type-safe, no cross-type assignment) or redesigning around a common abstract base/interface. Always make the base destructor virtual if you delete derived objects through base pointers. Prefer smart pointers (std::unique_ptr, std::shared_ptr) over raw new/delete to manage ownership and avoid leaks.

Further reading: reinterpret_cast (cppreference) and dynamic_cast (cppreference).

Recommended Answers

All 7 Replies

that is because you do not have a function that can convert struct A to struct C. you must supply one to the compiler otherwise it doesn't know what to do

So I have to overload the assignment operator?
I was never really good with that.

yep you can do that or you can write a conversion function that takes in a C and will return an A. so if an A is 3.26 times a C you could have

A * convertC(C*);

A * convertC(C * c)
{
            A * a = new A;
            a.B = c.C * 3.26;
            return a;
}

this example assumes that the members are some form float but from what i can tell your member variables have no type.

I don't see how that function allows me to have

a pointer from another struct point to pointer from another struct

oops sorry typo &a.A = &c.C * 3.26; i forgot to dereference the pointers before i use the values. also what is the data type for the member variables in the struct.

It's a pointer.
And i'm not sure where the 3.26 came from

a pointer to what? another A? the 3.26 was just an example that if both members were float and there was a conversion difference of 3.26 that you could multiply and get an equivalent answer. think of converting Celsius and Fahrenheit and they were structs in your program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.