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

  • Thread starter Thread starter neurocomp2003
  • Start date Start date
  • Tags Tags
    Local Reference
AI Thread Summary
The discussion revolves around a coding issue related to pointer and reference handling in C++. The user encounters an error when attempting to assign a dereferenced pointer to a reference type, specifically "Cannot convert CType* to CType*&." The goal is to simplify code by using a reference to avoid repetitive dereferencing, allowing for cleaner access to data through a reference variable. Suggestions include using a pointer to a pointer (CType**) as an alternative, but the user prefers a direct reference approach. Clarifications are sought regarding the intent behind the code, with some participants noting the use of operator overloading for pointer access and discussing potential solutions, such as making the data member public or declaring the function F as a friend of the node class to facilitate access. The conversation highlights the complexities of managing references and pointers in C++ while aiming for cleaner code.
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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
19
Views
5K
Replies
22
Views
3K
Replies
3
Views
5K
Replies
8
Views
2K
Replies
34
Views
4K
Replies
7
Views
3K
Back
Top