Don't know how to define my class correctly

  • Thread starter cainiaodelixiang
  • Start date
C

cainiaodelixiang

Hi:
I want to define an object as the node of a tree. Unforunately i
find myself fall into the syntax maze. Could anybody please help me
out?
Here is my wrong code. I don't know how to compile it correctly.
Thank you for your help.

#include <set>
using namespace std;

struct NodeKeyCompare {
bool operator () (const Node *n1, const Node *n2)
{
return n1->m_SearchKey < n2->m_SearchKey;
}
};

typedef set<Node *, NodeKeyCompare> NodeSet;

class Node {
private:
int m_i4Depth;
NodeSet m_Children;

public:
string m_SearchKey;

};


int main()
{
return 0;
}
 
V

Victor Bazarov

I want to define an object as the node of a tree. Unforunately i
find myself fall into the syntax maze. Could anybody please help me
out?
Here is my wrong code. I don't know how to compile it correctly.
Thank you for your help.

#include <set>
using namespace std;

struct NodeKeyCompare {
bool operator () (const Node *n1, const Node *n2)

'Node' is undefined at this point. You have to forward-declare it.
{
return n1->m_SearchKey < n2->m_SearchKey;

Since the contents of 'Node' class will not be known (if you just
forward-declare it), you need to change this to a declaration and
define this function out-of-class, after 'Node' is defined.
}
};

typedef set<Node *, NodeKeyCompare> NodeSet;

class Node {
private:
int m_i4Depth;
NodeSet m_Children;

public:
string m_SearchKey;

If you want to use 'string' said:
};


int main()
{
return 0;
}

V
 
D

Daniel T.

Hi:
I want to define an object as the node of a tree. Unforunately i
find myself fall into the syntax maze. Could anybody please help me
out?
Here is my wrong code. I don't know how to compile it correctly.
Thank you for your help.


#include <set>

class Node;

struct NodeKeyCompare {
bool operator()( const Node*, const Node* ) const;
};

class Node {
int m_i4Depth;
NodeSet m_Children;

public:
string m_SearchKey;
};

bool NodeKeyCompare::eek:perator()( const Node* n1, const Node* n2 )
{
return n1->m_SearchKey < n2->m_SearchKey;
}

int main()
{
return 0;
}
 
O

Old Wolf

Hi: I want to define an object as the node of a tree. Unforunately i
find myself fall into the syntax maze. Could anybody please help me
out?

Here's one way. But have you considered making NodeSet be
a set of Nodes, rather than a set of pointers? Using pointers
makes it harder to manage memory (although both approaches
have their benefits).

If you stick with pointers, call it something like NodePtrSet
(since it is a set of node pointers, not nodes).

Also, it is risky having m_SearchKey publicly settable, as
that could make your tree inconsistent if someone changes it.

Either have a public "get" method only, or make the
compare functor a friend of Node.

#include <set>
#include <string>
using std::set;
using std::string;

class Node;

struct NodeKeyCompare {
bool operator () (const Node *n1, const Node *n2);
};

typedef set<Node *, NodeKeyCompare> NodeSet;

class Node {
int m_i4Depth;
NodeSet m_Children;
public:
string m_SearchKey;
};

bool NodeKeyCompare::eek:perator () (const Node *n1, const Node *n2)
{
return n1->m_SearchKey < n2->m_SearchKey;
}
 
V

Victor Bazarov

Daniel said:
[..]
#include <set>

class Node;

struct NodeKeyCompare {
bool operator()( const Node*, const Node* ) const;
};

class Node {
int m_i4Depth;
NodeSet m_Children;

public:
string m_SearchKey;

'string' undefined.
};

bool NodeKeyCompare::eek:perator()( const Node* n1, const Node* n2 )
{
return n1->m_SearchKey < n2->m_SearchKey;
}

int main()
{
return 0;
}

V
 
C

cainiaodelixiang

Thank you for all replies. I apologize for my mistake of missing
#include <string> when posting the message.
Actually my problem is that when i declare the NodePtrSet, i need
NodeKeyCompare declared first. But when i declare the NodeKeyCompare i
need Node declared, and when decaring the Node, NodeKeyCompare is
needed. Just wrote "class Node;" doesn't solve the proble, because the
struct NodeKeyCompare didn't know the member m_SearchKey at the point.
Where is the way out?

#include <string>
#include <set>
using namespace std;

class Node;
struct NodeKeyCompare {
bool operator () (const Node *n1, const Node *n2)
{
return n1->m_SearchKey < n2->m_SearchKey;
}
};

typedef set<Node *, NodeKeyCompare> NodePtrSet;

class Node {
private:
int m_i4Depth;
NodePtrSet m_Children;

public:
string m_SearchKey;
};


int main()
{
return 0;
}

My fellow just give me a suggestion. I think it is a walk-around way,
is there any direct ways to solve the problem?
I hope my poor english has described the problem clearly, thank you for
your effort to read my message and try solving the problem. This is my
first time to write my problems in a group. Best regards to all wise
and patient people here.
 
C

cainiaodelixiang

My fellow's suggestion:
#include <set>
#include <string>
#include <iostream>
using namespace std;

class Node
{
public:
Node(string str) : m_SearchKey(str ) { }

virtual bool CompareKey(Node* node) = 0;
string m_SearchKey;
};

struct NodeKeyCompare : public binary_function<Node* ,Node* ,bool> {
bool operator () (Node *n1,Node *n2) const
{
return n1->CompareKey(n2);
}
};

class ComplexNode : public Node {
public:
int m_i4Depth;
set<Node *, NodeKeyCompare> m_Children;

public:
ComplexNode(string str) : Node(str ) {
}

virtual bool CompareKey(Node* node)
{
return m_SearchKey < node->m_SearchKey;
}

};

int main()
{
ComplexNode node1("11"), node2("22");
ComplexNode parent("pp");
parent.m_Children.insert(&node1);
parent.m_Children.insert(&node2);


return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top