Operator versus Function Object

  • Thread starter cainiaodelixiang
  • Start date
C

cainiaodelixiang

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

class Node;
typedef set<Node, less<Node> > NodeSet;

// This code works.
// But when i use typedef set <Node, NodeCmp> instead, it will cause a
compile time error.
//error C2065: 'NodeCmp' : undeclared identifier.
// I don't know how can i use function objects here? For simply put the
NodeCmp before the
// typedef obviously causes error too.
// Notice that if i need a set which contains not Node object , but
just Node pointers,such as
// set<Node *>, using operators in the definition is impossible, for it
is not allowed to reload
// operators is impossible for object pointers.
// How can i define such a set?
//Best regards.

class Node {
private:
int m_i4Depth;
NodeSet m_Children;

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

bool operator < (const Node &n1, const Node &n2)
{
cout << "^^" << endl;
return n1.m_SearchKey < n2.m_SearchKey;
}

struct NodeCmp: public binary_function<const Node &, const Node &,
bool> {
bool operator () (const Node &n1, const Node &n2) const
{
return n1.m_SearchKey < n2.m_SearchKey;
}
};
 
V

Victor Bazarov

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

class Node;
typedef set<Node, less<Node> > NodeSet;

// This code works.
// But when i use typedef set <Node, NodeCmp> instead, it will cause a
compile time error.
//error C2065: 'NodeCmp' : undeclared identifier.

Haven't we just talked about it? To use a class as a template argument
in a typedef, you need to forward-declare it. You have forward-declared
'Node'. Why don't you forward-declare 'NodeCmp' as well?
// I don't know how can i use function objects here? For simply put
the NodeCmp before the
// typedef obviously causes error too.
// Notice that if i need a set which contains not Node object , but
just Node pointers,such as
// set<Node *>, using operators in the definition is impossible, for
it is not allowed to reload
// operators is impossible for object pointers.
// How can i define such a set?
//Best regards.

class Node {
private:
int m_i4Depth;
NodeSet m_Children;

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

bool operator < (const Node &n1, const Node &n2)
{
cout << "^^" << endl;
return n1.m_SearchKey < n2.m_SearchKey;
}

struct NodeCmp: public binary_function<const Node &, const Node &,
bool> {
bool operator () (const Node &n1, const Node &n2) const
{
return n1.m_SearchKey < n2.m_SearchKey;
}
};

V
 
C

cainiaodelixiang

Victor said:
Haven't we just talked about it? To use a class as a template argument
in a typedef, you need to forward-declare it. You have forward-declared
'Node'. Why don't you forward-declare 'NodeCmp' as well?

Maybe it is not as easy as you think.
Forward-declare NodeCmp is not allowed here,as though the class Node is
forward-declared, the struct NodeCmp knows class Node, but it still
does not know the member in the class Node.
 
V

Victor Bazarov

Maybe it is not as easy as you think.
Forward-declare NodeCmp is not allowed here,as though the class Node
is forward-declared, the struct NodeCmp knows class Node, but it still
does not know the member in the class Node.

Look, we've been through this. You were given a solution before, and
you just need to apply the same logic here.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <string>
#include <set>
#include <iostream>
using namespace std;

class Node;

// define NodeCmp, don't define its members, only declare them.
struct NodeCmp: public binary_function<const Node &, const Node &, bool> {
bool operator () (const Node &n1, const Node &n2) const;
};

typedef set<Node, NodeCmp> NodeSet;

class Node {
private:
int m_i4Depth;
NodeSet m_Children;

public:
Node(string str):m_SearchKey(str) {}
// ^^^^^^^^^^
// and you should be passing by a reference to const here.

string m_SearchKey;
};

// now that all parts are there, define the NodeCmp member:
bool NodeCmp::eek:perator () (const Node &n1, const Node &n2) const
{
return n1.m_SearchKey < n2.m_SearchKey;
}

int main()
{
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

V
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top