473,406 Members | 2,707 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,406 software developers and data experts.

Operator versus Function Object

#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, NodeCmpinstead, 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;
}
};

Aug 1 '06 #1
3 1420
ca**************@gmail.com wrote:
#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, NodeCmpinstead, 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
--
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

Victor Bazarov wrote:
ca**************@gmail.com wrote:
#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, NodeCmpinstead, 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?
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.

Aug 2 '06 #3
ca**************@gmail.com wrote:
Victor Bazarov wrote:
>ca**************@gmail.com wrote:
>>#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, NodeCmpinstead, 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?

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::operator () (const Node &n1, const Node &n2) const
{
return n1.m_SearchKey < n2.m_SearchKey;
}

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

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 2 '06 #4

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

Similar topics

8
by: Kelly Mandrake | last post by:
Iv'e been reading tutorials and articles describeing operator overloading as both member functions and friend functions. I don't understand however the purpose of the friend on an operator when...
8
by: Floogle | last post by:
how do i create a virtual == operator. I've tried the following but it's incorrect... class Interface { ... public: virtual bool operator==(const Interface& rhs)const=0;
3
by: Neil Zanella | last post by:
Hello, I would like to ask the following question concerning the C# as operator. I would like to know whether the difference between using a C-style cast such as double x = 0; float y =...
6
by: hopangtsun | last post by:
Hello all, I've got a problem on operator overloading, I don't know what's wrong with my code. Can anyone kindly tell me what's the problem is? Thanks //this is the error message I've got...
11
by: Philipp Brune | last post by:
Hello Newsgroup, in c# there exists the '??' operator. It is used like this : string s1 = null; string s2 = s1 ?? "(undefined)" And does the following (simplified) :
7
by: Deron Meranda | last post by:
This is an optimization problem, one that occurs deep inside a loop that gets executed thousands of times. I'm looking for something in Python which would act like an identity operator, or...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
11
by: jakester | last post by:
I am using Visual C++ 2007 to build the code below. I keep getting linkage error. Could someone please tell me what I am doing wrong? The code works until I start using namespace for my objects. ...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.