I’m writing a C++ program using Code::Blocks. I want to make a doubly linked list.
My plan is to make an node class called geoPoint with pointers north and south to other nodes. I’ve written a test function to create and link two nodes, then traverse them with a third node. Here’s what I have so far:
#include <iostream>
#include <string>
using namespace std;
class geoPoint
{
public:
geoPoint *north, *south;
private:
string description;
public:
void showDesc()
{
cout << description << endl;
};
void setDesc(string sourceText)
{
description=sourceText;
};
void setNorth(geoPoint sourcePoint)
{
north= &sourcePoint;
}
void setSouth(geoPoint sourcePoint)
{
south= &sourcePoint;
}
};
int main()
{
geoPoint testPoint,testPoint2,currentPoint;
string sourceText("testPoint");
string sourceText2("testPoint2");
testPoint.setDesc(sourceText);
testPoint2.setDesc(sourceText2);
testPoint.setNorth(testPoint2);
testPoint2.setSouth(testPoint);
currentPoint=testPoint;
currentPoint.showDesc();
currentPoint= ¤tPoint.north;
currentPoint.showDesc();
cin.get();
return 0;
};
main() crashes when it gets to the line currentPoint= ¤tPoint.north;. The error message is:
error: no match for 'operator=' in 'currentPoint = & currentPoint.geoPoint::north'
I thought that a=&b is the right way to assign the dereferenced contents of pointer b to variable a. What am I doing wrong?
currentPointis of typegeoPoint.¤tPoint.northis of typegeoPoint**.&is the address-of operator: you’re taking the address of ageoPoint*, which stores the address of ageoPoint.If you want
currentPointto hold a copy of thegeoPointto whichcurrentPoint.northrefers, use the dereference operator*, as in*currentPoint.north. However, if you merely want to refer to the object without copying it, changecurrentPointto ageoPoint*and write this instead: