Jump to content
  • Advertisement
Sign in to follow this  
kingpinzs

how do I make one class point to another class?

This topic is 2706 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have created 2 class's and I am trying to get one to point to another one but I am getting this error.



2664: 'main_tree::TreeNode<Item>::set_Data_Link' : cannot convert parameter 1 from 'node<Item>' to 'main_tree::TreeNode<Item> *'
1> with
1> [
1> Item=std::string
1> ]
1> and
1> [
1> Item=user
1> ]
1> and
1> [
1> Item=std::string
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> see reference to function template instantiation 'void data_link<std::string,user>(main_tree::TreeNode<Item> *&,node<user> *)' being compiled
1> with
1> [
1> Item=std::string
1> ]
1>

Share this post


Link to post
Share on other sites
Advertisement


template <class Item>

class node
{
public:
typedef Item value_type;

node(const Item& init_data = Item(),node* init_link=NULL)
{data_field = init_data; next = init_link;}

void set_data(const Item& new_data){data_field = new_data;}
void set_next(node* new_link) {next = new_link;}

const Item& data() const{return data_field;}
const node* link() const{return next;}
node* link() {return next;}
Item& data() {return data_field;}
private:
Item data_field;
node *next;
};




template <class Item>

class TreeNode
{
public:
typedef Item value_type;

TreeNode(const Item& init_data = Item(),
TreeNode* init_Left_link=NULL,
TreeNode* init_Right_link=NULL,
TreeNode* init_Data_link=NULL)
{
data_field = init_data;
Data = init_Data_link;
Left = init_Left_link;
Right = init_Right_link;
}

void set_data(const Item& new_data){data_field = new_data;}
void set_Data_Link(TreeNode* new_link) {Data = new_link;}
void set_Left_Link(TreeNode* new_left) {Left = new_left;}
void set_Right_Link(TreeNode* new_right) {Right = new_right;}

const TreeNode* Data_link() const{return Data;}
const TreeNode* Left_link() const{return Left;}
const TreeNode* Right_link() const{return Right;}
const Item& data() const{return data_field;}

bool is_leaf() const{return (Left == NULL)&&(Right == NULL);}

TreeNode*& Data_link() {return Data;}
TreeNode*& Left_link() {return Left;}
TreeNode*& Right_link() {return Right;}
Item& data() {return data_field;}

private:
Item data_field;
TreeNode *Left;
TreeNode *Right;
TreeNode *Data;
};




template <class Item, class Item2>
void data_link(TreeNode<Item> *&root,node<Item2>* head_ptr)
{


data_link(root->Left_link(),head_ptr);

while(head_ptr)
{
if(root->data() == head_ptr->data().name)
{
root->set_Data_Link();
}
}
data_link(root->Right_link(),head_ptr);

}


I am trying to link the link list to the tree

Share this post


Link to post
Share on other sites
Post the real code. In your data_link method you're calling root->set_Data_Link which takes no argument while in the code snippet above the set_Data_Link does have one argument.

The error message indicates that you're passing the wrong node type to set_Data_Link (node<Item> instead of TreeNode<Item>).

Share this post


Link to post
Share on other sites

Post the real code. In your data_link method you're calling root->set_Data_Link which takes no argument while in the code snippet above the set_Data_Link does have one argument.

The error message indicates that you're passing the wrong node type to set_Data_Link (node<Item> instead of TreeNode<Item>).


I removed the argument this is what it should say [color="#1C2837"][font="CourierNew, monospace"][color="#000000"]root[color="#666600"]->[color="#000000"]set_Data_Link[color="#666600"]([color="#000000"]head_ptr[/font][color="#666600"][font="CourierNew, monospace"]);[/font]
[font="CourierNew, monospace"][color="#666600"]but [/font][color="#1C2837"][font="CourierNew, monospace"][color="#000088"]void[color="#000000"] set_Data_Link[color="#666600"]([color="#660066"]TreeNode[color="#666600"]*[color="#000000"] new_link[color="#666600"]) [color="#666600"]{[color="#660066"]Data [color="#666600"]=[color="#000000"] new_link[color="#666600"];} [/font]
[color="#1C2837"][font="CourierNew, monospace"][color="#666600"]takes a treeNode as an argument so I need to make the TreeNode a template so it can take any type but note sure how.[/font]
[color="#666600"][font="CourierNew, monospace"] [/font]
[color="#666600"][font="CourierNew, monospace"]and what do you mean by real code? This is my real code.[/font]

Share this post


Link to post
Share on other sites
What is calling this bit?

[color="#000088"]void data_link[color="#666600"]([color="#660066"]TreeNode[color="#666600"]<[color="#660066"]Item[color="#666600"]> [color="#666600"]*&root[color="#666600"],node[color="#666600"]<[color="#660066"]Item2[color="#666600"]>* head_ptr[color="#666600"])

Share this post


Link to post
Share on other sites
the main after the loading of both the tree and the link list.

I figured out how to get it to work I had to add another item to the template

[font=CourierNew, monospace][size=2][color=#000000] [color=#000088]void[color=#000000] set_Data_Link[color=#666600]([color="#660066"]Item2[color=#666600]*[color=#000000] new_link[color=#666600])[color=#000000] [color=#666600]{[color=#660066]Data[color=#000000] [color=#666600]=[color=#000000] new_link[color=#666600];}[/font]
[font=CourierNew, monospace][size=2][color=#666600]
[/font]
[font=CourierNew, monospace][size=2][color=#666600]instead of [/font]
[font=CourierNew, monospace][size=2][color=#666600]
[/font]
[font=CourierNew, monospace][size=2][color=#666600][color=#1C2837][font=arial, verdana, tahoma, sans-serif][color=#000000] [color=#000088]void[color=#000000] set_Data_Link[color=#666600]([color=#660066]TreeNode[color=#666600]*[color=#000000] new_link[color=#666600])[color=#000000] [color=#666600]{[color=#660066]Data[color=#000000] [color=#666600]=[color=#000000] new_link[color=#666600];}[/font][/font]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!