473,325 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Don't know how to define my class correctly

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

Aug 1 '06 #1
6 1410
ca**************@gmail.com wrote:
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', you need to #include <string>
>
};
int main()
{
return 0;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #2
In article <11**********************@s13g2000cwa.googlegroups .com>,
ca**************@gmail.com wrote:
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::operator()( const Node* n1, const Node* n2 )
{
return n1->m_SearchKey < n2->m_SearchKey;
}

int main()
{
return 0;
}
Aug 1 '06 #3
ca**************@gmail.com wrote:
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::operator () (const Node *n1, const Node *n2)
{
return n1->m_SearchKey < n2->m_SearchKey;
}

Aug 1 '06 #4
Daniel T. wrote:
[..]
#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::operator()( const Node* n1, const Node* n2 )
{
return n1->m_SearchKey < n2->m_SearchKey;
}

int main()
{
return 0;
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 1 '06 #5
Thank you for all replies. I apologize for my mistake of missing
#include <stringwhen 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.

Aug 1 '06 #6
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 *, NodeKeyComparem_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;
}

Aug 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: DP | last post by:
(2nd post, I think my first may have been to the wrong group - sorry) Hello Perl-ers - I´m hoping I can get some help here, because I'm very lost. Don't know Perl, I'm not a programmer. And...
20
by: Sam | last post by:
Hi I'm learning to code with C++ and wrote some very simple code. I think it's consistent with every rule but always got compiling errors that I don't understand. The code include 5 files as...
16
by: Jace Benson | last post by:
Ok I have read alot of things on zend.com, php.net and other sites went to the wikibooks to try to understand how to use a class. I have this project I want to do that I am sure would work great...
1
by: Geoff Hilyard | last post by:
Using VS.net 2003 / Managed C++ I have created a base object (public __gc CBaseObject) that everything in my project inherits from (Similar to how everything really inherits system::Object). I...
6
by: Erik Johnson | last post by:
Maybe I just don't know the right special function, but what I am wanting to do is write something akin to a __getattr__ function so that when you try to call an object method that doesn't exist,...
1
by: illegal.prime | last post by:
I would like to have an array of objects (whose class I define) and then just invoke either: MyClass clonedArray = (MyClass) myArray.Clone(); OR Array.Copy(myArray, clonedArray, myArray.Length);...
14
by: Keith Clark | last post by:
I want to define a text style class which is active whenever I refer to it. The following does not work in CSS: bluetext { COLOR: blue; FONT-SIZE:16px; } in HTML: ..... <TABLE><TR><TD>...
2
hsriat
by: hsriat | last post by:
I am not sure how many of you would have faced this problem, but I faced this many times. I am not good at memorizing things, so I give lot of code comment in my code. And never wanted that code to...
3
by: Immortal Nephi | last post by:
The rule of inheritance states that you define from top to bottom. Sometimes, you want to define base class and set reference from dervied class to base class, but you violate the rule. Here is an...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.