Pointer problem

This is a discussion on Pointer problem within the C++ Programming forums, part of the General Programming Boards category; I'm getting this compiler error(below) for 3 lines(in bold) that are similar where im trying to assign a pointer to ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    25

    Pointer problem

    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.

    Quote 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 << " ";
    	}
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You are trying to assign a node to a node pointer. For example, you are trying to do the equivalent of:
    Code:
    int* pi;
    int i = 5;
    pi = i;

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,758
    Post the full error message. Knowing which file the error is from and on which line it was detected helps in zooming in on the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    25
    Quote Originally Posted by laserlight View Post
    Post the full error message. Knowing which file the error is from and on which line it was detected helps in zooming in on the problem.
    here it is:
    c:\documents and settings\blck\my documents\mazegen\func.cpp(27) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct node' (or there is no acceptable conversion)
    c:\documents and settings\blck\my documents\mazegen\func.cpp(41) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct node' (or there is no acceptable conversion)
    c:\documents and settings\blck\my documents\mazegen\func.cpp(46) : error C2065: 'i' : undeclared identifier
    c:\documents and settings\blck\my documents\mazegen\func.cpp(46) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'struct node' (or there is no acceptable conversion)
    mazegen.cpp
    Error executing cl.exe.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    25
    Quote Originally Posted by bithub View Post
    You are trying to assign a node to a node pointer. For example, you are trying to do the equivalent of:
    Code:
    int* pi;
    int i = 5;
    pi = i;
    Thnx for the advil

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 08:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 07:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 10:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 12:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21