I'm getting this compiler error(below) for 3 lines(in bold) that are similar where im trying to assign a pointer to a pointer and I cant figure out why. Shouldn't a pointer thats in a struct be able to point to a different pointer in an array of pointers of the same type? I know im not doing it right :/ but I cant seem to figure it out.
Originally Posted by
MSVC++
error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct node' (or there is no acceptable conversion)
main.cpp
Code:
#include <iostream>
#include "func.h"
using namespace std;
int main(int argc, char * argv[])
{
// int n = atoi(argv[1]);
int n = 10;
forest Forest(n);
Forest.printArray();
Forest.wunion(0,10);
Forest.wunion(7,4);
Forest.wunion(1,9);
Forest.wunion(6,5);
Forest.printArray();
/*
Forest.wunion(0,2);
Forest.wunion(7,10);
Forest.wunion(9,6);
Forest.printArray();
Forest.wunion(5,4);
Forest.printArray();
Forest.wunion(3,8);
Forest.wunion(3,5);
Forest.printArray();
int x = Forest.find(8);
int y = Forest.find(1);
cout << "x = " << x << endl << "y = " << y << endl;
Forest.printArray();
*/
return 0;
}
func.h
Code:
struct node
{
node(const node &original);
node();
int data;
node *parent;
};
class forest
{
public:
forest(int);
int find(int);
int pcfind(int);
void wunion(int, int);
void printArray();
private:
node * nodeArray;
int size;
};
func.cpp
Code:
#include "func.h"
#include <iostream>
using namespace std;
node::node(){data = -1; parent = NULL;}
node::node(const node & original)
{
data = original.data;
parent = original.parent;
}
forest::forest(int n)
{nodeArray = new node[n]; size = n;}
int forest::find(int x)
{
int p;
while( (p = this->nodeArray[x].data) > 0 ) x = p;
return x;
}
int forest::pcfind(int e)
{
int x = find(e);
this->nodeArray[e].parent = this->nodeArray[x];
return x;
}
void forest::wunion(int j, int k)
{
int x = this->pcfind(j);//root index of 1st tree
int y = this->pcfind(k);//root index of 2nd tree
int tree1nodes = this->nodeArray[x].data * (-1);
int tree2nodes = this->nodeArray[y].data * (-1);
if( (tree1nodes == tree2nodes) || (tree1nodes > tree2nodes) )
{
this->nodeArray[k].parent = this->nodeArray[x]; //2nd index node parent = root of 1st tree
this->nodeArray[x].data--;//increment child counter for 1st tree
}
else if(tree1nodes < tree2nodes)
{
this->nodeArray[j].parent = this->nodeArray[k];//1st index node parent = root of 2nd tree
this->nodeArray[y].data--;//increment child counter for 2nd tree
}
}
void forest::printArray()
{
for (int i = 0; i < this->size; i++)
{
cout<< this->nodeArray[i].data << " ";
}
}