Solved

Class Operator Problem

Posted on 2002-07-11
12
186 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now