Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Class Operator Problem

Posted on 2002-07-11
12
Medium Priority
?
192 Views
Last Modified: 2010-04-01
What i am trying to do is get a class to act as a function when needed
class Foo
{
 char *operator() (char *str) { return "Test"; }
 int v;
}

Foo x; char *TestStr;
TestStr=x("String");

and i also want to still be able to access the variables/ functions inside the class

int myInt = x.v;

i keep getting the error that "term does not evaluate to a function"

any help on this would be great, thanks in advance
0
Comment
Question by:Khisanth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 3
12 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 7146872
In your example,  both the operator function and member data v, are private.

Try making them public:
0
 
LVL 30

Expert Comment

by:Axter
ID: 7146875
class Foo
{
public:
char *operator() (char *str) { return "Test"; }
int v;
};

You were also missing a simicolon at the end of your class statement.
That might also be the problem.
0
 
LVL 30

Expert Comment

by:Axter
ID: 7146878
char *operator() (char *str) { return "Test"; }

This operator is returning a temporary string that goes out of scope out side of the function.  This will cause problems.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:Axter
ID: 7146882
Try the following:

class Foo
{
public://Add missing public keyword
char *operator() (char *str)
{
 static char Data[] = "Test";
 return Data;
}
int v;
}; //Add missing simicolon
0
 
LVL 30

Expert Comment

by:Axter
ID: 7146887
I forgot to mention your opertator should accept a const char*.
Example:

class Foo
{
public://Add missing public keyword
char *operator() (const char *str) //Add const char*
{
static char Data[] = "Test";
return Data;
}
int v;
}; //Add missing simicolon
0
 
LVL 30

Expert Comment

by:Axter
ID: 7146890
Last but not least, Add an extra () to the following line:

TestStr=x()("String");
0
 

Author Comment

by:Khisanth
ID: 7146913
Well i just typed that real fast thats why there are typos and missing the public, but its right in the code

ive tried what you posed and still get this error when trying to do testStr=x()("String");
"term does not evaluate to a function"
0
 
LVL 30

Expert Comment

by:Axter
ID: 7146980
>>Well i just typed that real fast thats why there are typos

Instead of typing it in here, could you please copy and paste the code to a comment.

There could be other problems in the code, but we won't know until we see the actual code you're using.
0
 

Author Comment

by:Khisanth
ID: 7147289
Ok it looks like your way works as long as the class isnt a pointer

class FOO
{
public:
     char *operator()(char *lpl) { return lpl; }
     int x;
};

FOO *footest; char *fst;

footest=new FOO;
fst=footest("Test123");
footest->x=0;
MessageBox(hWnd, fst, "Test", MB_OK);
delete footest;

works if i just do FOO footest; and dont make it a pointer but the way it is it generates the error "error C2064: term does not evaluate to a function"
0
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 7147343
When it's a pointer, You can get it to work using any one of the following syntax:

fst=(*footest)("Test123");
fst=footest->operator()("Test123");
0
 
LVL 30

Expert Comment

by:Axter
ID: 7147346
It should also be able to work like this:
fst=footest->()("Test123");

But I couldn't get that to compile on VC++ 6.0.

The previous two methods I posted will compile.
0
 

Author Comment

by:Khisanth
ID: 7147365
Worked Perfect, Thanx :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question