Solved

Class Operator Problem

Posted on 2002-07-11
12
189 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
  • 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.net build a IF/Then Walkthrough Guide 1 240
Issues with C++ Class 19 101
How do i run a c++ file? 15 47
How do I get Window Title of all opened process? 4 15
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

821 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