Solved

Class Operator Problem

Posted on 2002-07-11
12
188 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert char array to number in c 5 85
How to convert MFC::CString to UTF8 wchar_t* 10 209
VS2015 Redefinition errors 4 51
DCT of  2D array using fftw in c++ 9 41
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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

773 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