|
Hi guys,
Michael Dunn was kind enough to answer a question I had last night, but now I seem to have hit another nearby snag.
I'm trying to implement a copy constructor as follows:
<br />
Node somenode = new Node();<br />
Node masterArray[40];<br />
.<br />
. <br />
.<br />
<br />
for (int i = 0; i < 40; i++)<br />
{<br />
.
masterArray[i] = new Node(someNode);<br />
and this is my copy constructor (all values are ints, char arrays, etc)
<br />
Node::Node(const Node& n)<br />
{<br />
name = n.name;<br />
lChild = n.lChild;<br />
rChild = n.rChild;<br />
placed = n.placed;<br />
arraySpot = n.arraySpot;<br />
dRPtr = n.dRPtr;<br />
}<br />
When I try to compile, I get errors on all the lines similar to:
<br />
masterArray[i] = new Node(someNode);<br />
that read:
.\BST.cpp(52) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'Node *' (or there is no acceptable conversion)
c:\path 3 files\bst\bst\Node.h(14): could be 'Node &Node::operator =(const Node &)'
while trying to match the argument list '(Node, Node *)'
(verbose, isn't it? )
I'll keep digging around for an answer (yes, I did a cursory check of google first), but in the mean time, does anyone have any ideas?
Thanks, as always,
-Sean
|
|
|
|
|
You do not have an assigment operator defined for your Node class. You are getting the error because the default operator= simply does a bitwise copy of the bits from the right to the left, but it does not know how to copy from a Node* pointer to a Node object. Your Node class has to define it's own assignment operator.- Node& Node::operator =(const Node& n)
- {
- Name = n.Name;
- ...
- return *this;
- }
Node& Node::operator =(const Node& n)
{
Name = n.Name;
...
return *this;
}
[Edit]
It is always a good idea to declare both a copy c'tor and an assignment operator at the same time when you feel the need for either one.
[/Edit]
You may be right
I may be crazy
-- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
I see...
I stuck this code in the Node.h file:
Node& Node::operator =(const Node& n);
and this in the Node.cpp file:
Node& Node::operator =(const Node& n)
{
name = n.name;
lChild = n.lChild;
rChild = n.rChild;
placed = n.placed;
arraySpot = n.arraySpot;
dRPtr = n.dRPtr;
return *this;
}
But I seem to get the same thing. When I do masterArray[0] = new Node(someNode);
I get the same error. If I omit the "new" I get shallow copies.
Could someone please clarify which step I'm missing here? I guess I'm still a little confused.
Thanks for your patience.
|
|
|
|
|
Have you tried stepping through your code in the debugger? Does operator= get called? You may have to declare another operator= that takes a Node* pointer
Node & Node::operator =(const Node *n)
{
...
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
The operator= is not called, even when I add the function you mentioned above. My copy constructor is called:
Node::Node(const Node& n)
{
name = n.name;
lChild = n.lChild;
rChild = n.rChild;
placed = n.placed;
arraySpot = n.arraySpot;
dRPtr = n.dRPtr;
}
but I still seem to be getting shallow copies. If I put that "new" keyword back, I don't get compile time errors, but I get a run time error which reads "there is no source code available for this location", or something to that effect. Thanks again for the help. You seem to be pretty busy helping people on these boards today. I really appreciate all your hard work, and I'm sure they do too .
-Sean
|
|
|
|
|
Are you coming from a C# background perhaps? You're mixing up objects and pointers to objects. When you write Node masterArray[40] you get 40 objects, there's no need to call new Node later and put the result in the array.
Also - I didn't mention this in my earlier post - Node should have an operator= as well. When a class has either a copy constructor or an operator= , it usually needs the other as well.
|
|
|
|
|
Yeah, I took a few C++ classes, then about three semesters of C# (very astute). So then (referring to my above posts), how do I get that deep assignment that I need from where I am?
masterArray[i] = Node(someNode);
doesn't do the trick. Like I said, I get a shallow assignment after that line calls
Node::Node(const Node& n)
{
name = n.name;
lChild = n.lChild;
rChild = n.rChild;
placed = n.placed;
arraySpot = n.arraySpot;
dRPtr = n.dRPtr;
}
in node.h.
oddly, I get precisely the same thing happening if I give it
masterArray[i] = someNode;
still shallow assignment!
however, this time the overloaded operator is called:
Node& Node::operator =(const Node& n)
{
name = n.name;
lChild = n.lChild;
rChild = n.rChild;
placed = n.placed;
arraySpot = n.arraySpot;
dRPtr = n.dRPtr;
return *this;
}
why won't that give me my deep assignment?
Thanks again guys
-Sean
|
|
|
|
|
How are you determining "shallow" vs "deep" assignments?
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
Shallow changes when the object it is derived from changes, whereas a deep assignment is independent of its parent object.
|
|
|
|
|
All I want to do is change the font color in a button on themed XP/Vista...
MSDN claims that to change "text colors for all controls that support custom draw except for the list view and tree view, simply set the desired text and background colors in the device context supplied in the custom draw notification structure with the SetTextColor and SetBkColor functions"
I do this in the CDDS_PREPAINT phase and return CDRF_DODEFAULT. This has no effect. Anyone got any clues?
There was an MSDN sample but it has vanished from the Microsoft site.
Thx++
|
|
|
|
|
Can you show the code in your NM_CUSTOMDRAW handler?
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
Hi PJ
<br />
LPNMCUSTOMDRAW pCustomDraw = (LPNMCUSTOMDRAW) pNotifyStruct;<br />
if (pCustomDraw->dwDrawStage == CDDS_PREPAINT)<br />
{<br />
COLORREF crf = GetSysColor(COLOR_BTNTEXT);<br />
SetTextColor(pCustomDraw->hdc,crf);<br />
*result = CDRF_NOTIFYPOSTPAINT;<br />
}<br />
Thx
|
|
|
|
|
How i can get the source code for video streaming in handheld devices such as PDA.
alamzeb
|
|
|
|
|
hi
this is program i made it okay
but i want convert all if statment in last programm to the for loop but the user must determine the input or stage to any number he wants okay
there's conditions every a give you b,b give you ab
if the user enter 4 level answer must be
stage 0:a
stage 1:b
stage 2:ab
stage 3:bab
etc...... this is code :
#include
#include
int size;
void main(){
char symbol[10 ];
int level;
int i;
//******************************************************************//
cout<<"ENTER SIZE OF YOUR CHARACTER :";
cin>>size;
cout<<"*ENTER CHARACTER/ALAPHBET : ";
for( i=0;i
{
cin>>symbol[i];
}
//******************************************************************//
cout<<"*ENTER HOW MANY LEVEL YOU WANT : ";
cin>>level;
cout<<endl;
if(level>8)
cout<<"PLEASE ENTER NUMBER BETWEEN 1 TO 8"<
|
|
|
|
|
Have lil patience dude....some one would help you out soon.
All the best
|
|
|
|
|
heh, you can always tell when midterms roll around in the introductory C++ classes. The urgency seems to increase by a magnitude of ten .
those were dark days indeed.
|
|
|
|
|
Use two strings, one to hold the previous level and the other to build the output for the new level.
Traverse the previous string one charcter at a time and build the new output string as you go.
Repeat as meny times as needed.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
Please review your post. There is no indentation and worse still, the for loop has been truncated. I can probably guess what's missing but you'll get more help if you take some time making your post easier for people to read.
Steve
|
|
|
|
|
Someone was nice enough to write a solution for you in one of your previous posts.
"Great job, team. Head back to base for debriefing and cocktails."
(Spottswoode "Team America")
|
|
|
|
|
Hi i am using borland for my contact manager application.
Now i have to read all the values from the comma seperated value file. But i am not sure how to achieve this.
The csv file is something like this
Konopelski,Tessa,255,ElphinstoneStreet,Hidworthville,958,Adolfo.Y.Anderson@Amazeworld.edu,388-0896,,,c:\DuffPath\DuffPhoto.jpg,0
Wisozk,Veda,13,Carmel Close,Upmouth,66744,Kristy.A.Boyer@DoughnutOffLine.net,442-1682,,,,0.....etc
Representing First name, last name, house number, street name, town, post code, email, phone n all.
Any or all help would be greatly appreciated.
Thanks a lot
|
|
|
|
|
strtok
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
|
Lookup the strtok() function in MSDN. strtok() is used to split a string based on certain tokens, such as a comma.
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|
|
But i am using Borland....
Is this function fine for borland or only MSVC++ and .NET
|
|
|
|
|
AFAIK it is a standard CRT function so it should work with Borland. Try it and find out
You may be right I may be crazy -- Billy Joel --
Within you lies the power for good, use it!!!
|
|
|
|