Public Group

This topic is 4218 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

error C2808: unary 'operator ->' has too many formal parameters ...is an error I received after attempting to overload said operator. While it was easy enough to change which operator I was overloading, I'm still confused as to why this one, out of all possible operators, is a black sheep. If for nothing more than sheer curiosity, can anyone explain that to me?

##### Share on other sites
Can you post the code? The error description makes it pretty easy to guess the cause of the problem, but we'll be able to give you more specific feedback if we can see what you're actually trying to do.

##### Share on other sites
class Base_Stat {	int Base_Score, Base_Mod, Temp_Score, Temp_Mod, Misc_Mod;	string Stat_Name;	Base_Stat ();	Base_Stat (string);	Base_Stat operator -> (Base_Stat);	int Get_Mod(int);	void Set_Base (int);	void Change_Base (int);	void Change_Temp (int);	void Print_Stats ();	friend class Character;};

...is the class in which I declare the overload...

Base_Stat Base_Stat::operator -> (Base_Stat param) {	param.Stat_Name = Stat_Name;	return param;}

...and there is it's definition. After compiling with those, I got the previous error, but after switching the -> with a <=, the program compiled and ran nicely. I'm now having a bit of trouble using this overloaded operator, but it doesn't seem to be something I can't handle (although any advice would be duly appreciated).

As for what I'm attempting to do, I've made a class that has several other classes as members (Base_Stat being the one you've seen). When declaring the class, a default constructor is used to add Base_Stat to the large class, but I've had some problems initializing a non-default constructor while declaring another class. So I'm attempting to side-step the problem and simply give myself a way to modify the default-created Base_Stat with a properly constructed one, by way of overloading an operator. This error simply maybe me raise an eyebrow.

##### Share on other sites
13.5.6 - "operator-> shall be a non-static member function taking no parameters."

##### Share on other sites
Just how do you plan on passing a parameter to operator-> ?

##### Share on other sites
Quote:
 Original post by Washu13.5.6 - "operator-> shall be a non-static member function taking no parameters."

...which to me says "no overloading." I'll keep that in mind, thank you for the response!

##### Share on other sites
It doesn't mean you can't overload it.

You should understand how operator-> works, though. When you call operator-> on an object, it keeps getting applied to the return value of operator-> until it is applied to a pointer type, then it's used to access one of the members of that object.

Like this:

struct A{  void foo() const  {    std::cout << "A::Foo() called\n";  }};struct B{  A x;  A const* operator->() const  {    std::cout << "B::operator->() called\n";    return &x;  }};struct C{  B x;  B const& operator->() const  {    std::cout << "C::operator->() called\n";    return x;  }};struct D{  C x;  C const& operator->() const  {    std::cout << "D::operator->() called\n";    return x;  }};int main(){  D d;  d->foo();}

Will print:

D::operator->() calledC::operator->() calledB::operator->() calledA::Foo() called

##### Share on other sites
I understand how it works in normal circumstances, but I thought overloading simply made the operator have completely different functionality, which you define yourself, meaning that it wouldn't matter how the -> works as a pointer function; I told it to do something else entirely when I overloaded it as a class operator.

Is this incorrect then?

##### Share on other sites
Quote:
 Original post by hereticprophecyI understand how it works in normal circumstances, but I thought overloading simply made the operator have completely different functionality, which you define yourself, meaning that it wouldn't matter how the -> works as a pointer function; I told it to do something else entirely when I overloaded it as a class operator.Is this incorrect then?

You can't go wild and invent new valid syntax. For instance, how did you plan on passing a parameter to your overloaded operator->? obj->(arg) ??

operator-> is a unary function, you can't make it binary, the syntax doesn't support it.

##### Share on other sites
If I knew the difference between 'unary' and 'binary' operators, I wouldn't be posting this. Unary = 'one-directional'?

I'm toying with syntax, as I'm teaching myself the language through various sources, and when I saw in the cplusplus.com tutorial that the + operator could be modified to have a vastly different function than it normally has, I assumed the others listed there could do the same. Passing parameters with those overloaded functions was easy, and I'd made it work in several test programs; I simply tried something new and was made curious by the result.

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 16
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632155
• Total Posts
3004473

×