How to Resolve CType* to CType*& Conversion Error in C++?

  • Context: C/C++ 
  • Thread starter Thread starter neurocomp2003
  • Start date Start date
  • Tags Tags
    Local Reference
Click For Summary
SUMMARY

The discussion addresses the "Cannot convert CType* to CType*&" error encountered in C++ when attempting to assign a dereferenced pointer to a reference to a pointer. The user aims to simplify code by using a reference to a pointer for accessing node data without repeatedly dereferencing. Solutions provided include directly assigning a pointer from the node's data member or using a friend function to access private members. The recommended approach is to use CType* tp; tp = a->data; for cleaner access to the data.

PREREQUISITES
  • Understanding of C++ pointer and reference semantics
  • Familiarity with operator overloading in C++
  • Knowledge of class member access and visibility (public/private)
  • Basic understanding of function parameters and memory management in C++
NEXT STEPS
  • Learn about C++ operator overloading, specifically unary operators
  • Research C++ pointers and references to enhance memory management skills
  • Explore friend functions in C++ for accessing private class members
  • Study best practices for managing object references in C++ to avoid common pitfalls
USEFUL FOR

C++ developers, software engineers, and students seeking to resolve pointer and reference issues in their code, particularly those working with class structures and operator overloading.

neurocomp2003
Messages
1,359
Reaction score
4
i have this following code with local var as a "ref. to ptr"

class node {
CType* data;
CTYpe* operator*() { return data; }
}
void F(node *a, node *b)
{
CType*& tp; tp=**a;
}

I get the error "Cannot convert CType* to CType*&
which i sort of understnad but i do not know how to resolve this.
The reason I'm looking to do this is so that i don't have (**a).Data() throughout my code but rather tp.Data(); and i need it to be a reference otherwise it would not change the variable outside of hte Fx.

I guess i can use a ptr to ptr(CType**) but i'd much rather just use a C++ reference. and if i do tp=***a i still get a error.

thanks in advance
 
Technology news on Phys.org
The example is kind of hard to follow, what is it youre trying to do? could you spell out the goal of the coding?

Using the Address of operator, is that for a function call? Do you want to manipulate objects with a pass by reference? It looks like a 3D array in there?
 
Brad, he is overloading the unary operator *. The purpose of this is so when he asks for the pointer of a node object, he gets the pointer of data instead.neurocomp2003, If you do this:

Ctype* tp; tp=**a;

then you would do this:

(*tp).data()

The other option is to do something like this:

CType*tp; tp=a->data;

Then you would do this:

tp->data();

This is of course assuming you make data public. Either that, or you make the function F a friend of node.
 
Last edited:

Similar threads

  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
2
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K