473,396 Members | 1,849 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,396 software developers and data experts.

using unary_function

Hello,

I have the following code, but does not compile:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<class Arg, class Result>
  5. struct unary_function
  6. {
  7.        typedef Arg argument_type;
  8.        typedef Result result_type;
  9. };
  10.  
  11. class Greater: public unary_function<int, bool>
  12. {
  13.       public:
  14.              result_type operator () (argument_type k)
  15.              {
  16.               if (k>10)
  17.               return true;
  18.               else
  19.               return false;
  20.              }
  21. };
  22.  
I get the following errors:

expected template-name before '<' token
expected `{' before '<' token
expected unqualified-id before '<' token

All of these errors are related to the line:
class Greater: public unary_function<int, bool>

I would appreciate any insights. Thank you.
Jul 12 '07 #1
7 2985
weaknessforcats
9,208 Expert Mod 8TB
What compiler are you using?

This compiled without error using Visual Studio.NET 2005.
Jul 12 '07 #2
DEV C++. It seems like Visual Studio.NET 2005 is much more flexible.
Jul 12 '07 #3
OK, so when I replace:

class Greater: public unary_function<int, bool>

with

class Greater: public std::unary_function<int, bool>

I get rid of the error. However, I don't know why this works because I have had using namespace std at the first place.
Jul 12 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
It's because you also have a struct named unary_function. Now the compiler can't tell whether to use yours or the one in std. So it bails on you.

This is the reason for namespaces and why your unary_function should be in your namespace. Had it been so, this error would never have cropped up.
Jul 12 '07 #5
how can I put my unary_function into namespace?
Jul 12 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
[code=cpp]
namespace curious2007
{
template<class Arg, class Result>
struct unary_function
{
typedef Arg argument_type;
typedef Result result_type;
};

class Greater: public unary_function<int, bool>
{
public:
result_type operator () (argument_type k)
{
if (k>10)
return true;
else
return false;
}
};


} // end of namespace

Now it's called: curious2007::unary_function.
Jul 12 '07 #7
SFaX
1
unary_function is declared in <functional>... try to include it explicitly
Jul 26 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: kyo | last post by:
class A { public: int a; }; struct testfun1 : public unary_function<const A&, bool> { bool operator()(const A &) const {
12
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ so maybe there's something I miss. I write a simple board game. It has a board class. This class has a method that returns the count of pieces a player has on the...
3
by: Allerdyce.John | last post by:
I would like to know what is the advantage of inherit from unary_function template? For example, I have this in my code: What is the advantage of doing this: class Same :...
6
by: Dilip | last post by:
I have a vector of class object pointers that have 2 internal states (USED or EMPTY). USED is just a way of indicating the element in that slot has valid state. EMPTY means the element is...
8
by: michael.lesniak | last post by:
Hello, I'm learning C++ for a couple of days and play a bit with the algorithms provided in the STL. One thing I don't understand is the fact that classes inherited of functors have to be...
6
by: Belebele | last post by:
Suppose I want to use some object's member function as the action passed to a for_each call: class A { ... public: void foo(int ); }; A a;
3
by: PolkaHead | last post by:
I was wondering if there's a way to traverse a two-dimensional vector (vector of vectors) with a nested for_each call. The code included traverses the "outer" vector with a for_each, than it...
1
by: LinLMa | last post by:
Hello everyone, I am wondering what is the practical usage of unary_function? Could anyone list some practical benefits? My question is, a class like class Foo : public...
11
by: Dijkstra | last post by:
Hi folks! First, this is the code I'm using to expose the problem: ------------------------------------------------------------------ #include <functional> #include <string> #include...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.