By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,261 Members | 1,325 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,261 IT Pros & Developers. It's quick & easy.

Trouble moving overloaded operator from inline

P: n/a
Hello,

I have the following file which compiles and runs like a charm:

//BEGIN WORKING FILE
#include <iostream>
using namespace std;
class Dog
{
public:
int run(int i) const { cout << "run\n"; return i; }
int eat(int i) const { cout << "eat\n"; return i; }
int sleep(int i) const { cout << "ZZZ\n"; return i; }
typedef int (Dog::*PMF) (int) const;
class FunctionObject
{
public:
FunctionObject(Dog* wp, PMF pmf);
int operator( )(int i) const;

private:
Dog* ptr;
PMF pmem;
};
FunctionObject operator->*(PMF pmf)
{
return FunctionObject(this, pmf);
}
};
Dog::FunctionObject::FunctionObject(Dog* wp, PMF pmf)
:ptr(wp), pmem(pmf)
{ }
int Dog::FunctionObject::operator( )(int i) const
{
return (ptr->*pmem)(i);
}
int main( )
{ }
//END WORKING FILE

However, I'd like to move the 'operator->*' definition out of the
class definition as follows:

//BEGIN BROKEN FILE
#include <iostream>
using namespace std;
class Dog
{
public:
int run(int i) const { cout << "run\n"; return i; }
int eat(int i) const { cout << "eat\n"; return i; }
int sleep(int i) const { cout << "ZZZ\n"; return i; }
typedef int (Dog::*PMF) (int) const;
class FunctionObject
{
public:
FunctionObject(Dog* wp, PMF pmf);
int operator( )(int i) const;
private:
Dog* ptr;
PMF pmem;
};
FunctionObject operator->*(PMF pmf);
};
FunctionObject Dog::operator->*(PMF pmf) //THIS IS LINE 25, see below
{
return FunctionObject(this, pmf);
}
Dog::FunctionObject::FunctionObject(Dog* wp, PMF pmf)
:ptr(wp), pmem(pmf)
{ }
int Dog::FunctionObject::operator( )(int i) const
{
return (ptr->*pmem)(i);
}
int main( )
{ }
//END BROKEN FILE

Now I get the following compiler error:
foo.cpp:25: syntax error before '::'

The line number (25) is a bit off because of the cut and paste but, in
any case, it points to the indicated line. Can anyone tell me what
I've done wrong here? Thanks in advance.

-exits
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
exitsfunnel wrote:
Hello,

Hi.
FunctionObject Dog::operator->*(PMF pmf) //THIS IS LINE 25, see below
{
return FunctionObject(this, pmf);
}
Dog::FunctionObject Dog::operator->*(PMF pmf)
... I've done wrong here? Thanks in advance.


I know how you feel right now ...

Cheers

Jul 22 '05 #2

P: n/a
Gianni Mariani wrote:
exitsfunnel wrote:
Hello,


Hi.
FunctionObject Dog::operator->*(PMF pmf) //THIS IS LINE 25, see below
{
return FunctionObject(this, pmf);
}

Dog::FunctionObject Dog::operator->*(PMF pmf)
... I've done wrong here? Thanks in advance.

I know how you feel right now ...

Cheers


Oh man, I can't believe I missed that. Thanks for the quick reply!

-exits

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.