Weird VC++ behaviour
-
While writing some code for a class, wich also contains oveloads for the assignment operator ( = ), VC++ keeps giving C2801 errors. According to MSDN, C2801 means that assignment, class member access, subscripting or function call operators must be a non-static class member. This is my code:
class Foo { private: CString m_String; public: Foo(); Foo(CString); ~Foo(); friend Foo& operator= (const class Bar); } Foo::Foo (CString string) { m_String = string; } Foo& operator= (const class Bar BarInstance) { return Foo (BarInstance.m_String); } class Bar { private: CString m_String; public: Bar(); ~Bar(); }
But that's not the whole story; the codechar* yourName = "DaFrawg"; MessageBox("Hello there, %s", yourName);
will generate a messagebox with the prompt "Hello there, %s" and the caption "DaFrawg". What I want is, of course, to have the message "Hello there, DaFrawg" displayed. Can somebody help me, please? //Please don't mind my bad English -
While writing some code for a class, wich also contains oveloads for the assignment operator ( = ), VC++ keeps giving C2801 errors. According to MSDN, C2801 means that assignment, class member access, subscripting or function call operators must be a non-static class member. This is my code:
class Foo { private: CString m_String; public: Foo(); Foo(CString); ~Foo(); friend Foo& operator= (const class Bar); } Foo::Foo (CString string) { m_String = string; } Foo& operator= (const class Bar BarInstance) { return Foo (BarInstance.m_String); } class Bar { private: CString m_String; public: Bar(); ~Bar(); }
But that's not the whole story; the codechar* yourName = "DaFrawg"; MessageBox("Hello there, %s", yourName);
will generate a messagebox with the prompt "Hello there, %s" and the caption "DaFrawg". What I want is, of course, to have the message "Hello there, DaFrawg" displayed. Can somebody help me, please? //Please don't mind my bad EnglishDaFrawg wrote: But that's not the whole story; the code char* yourName = "DaFrawg"; MessageBox("Hello there, %s", yourName); will generate a messagebox with the prompt "Hello there, %s" and the caption "DaFrawg". What I want is, of course, to have the message "Hello there, DaFrawg" displayed. Can somebody help me, please?
CString strMessage; strMessage.Format("Hello there, %s", yourName); MessageBox(strMessage);
Michael 'Logic, my dear Zoe, merely enables one to be wrong with authority.' - The Doctor: The Wheel in Space -
While writing some code for a class, wich also contains oveloads for the assignment operator ( = ), VC++ keeps giving C2801 errors. According to MSDN, C2801 means that assignment, class member access, subscripting or function call operators must be a non-static class member. This is my code:
class Foo { private: CString m_String; public: Foo(); Foo(CString); ~Foo(); friend Foo& operator= (const class Bar); } Foo::Foo (CString string) { m_String = string; } Foo& operator= (const class Bar BarInstance) { return Foo (BarInstance.m_String); } class Bar { private: CString m_String; public: Bar(); ~Bar(); }
But that's not the whole story; the codechar* yourName = "DaFrawg"; MessageBox("Hello there, %s", yourName);
will generate a messagebox with the prompt "Hello there, %s" and the caption "DaFrawg". What I want is, of course, to have the message "Hello there, DaFrawg" displayed. Can somebody help me, please? //Please don't mind my bad EnglishTry this:
class Bar
{
public:
Bar();
~Bar();CString m\_String;
};
class Foo
{
private:
CString m_String;public:
Foo();
Foo(CString);
~Foo();const Foo& operator=( const Bar& bar );
};
Foo::Foo (CString string)
{
m_String = string;
}const Foo& Foo::operator=(const Bar& BarInstance)
{
m_String = BarInstance.m_String;return (\*this);
}
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
-
While writing some code for a class, wich also contains oveloads for the assignment operator ( = ), VC++ keeps giving C2801 errors. According to MSDN, C2801 means that assignment, class member access, subscripting or function call operators must be a non-static class member. This is my code:
class Foo { private: CString m_String; public: Foo(); Foo(CString); ~Foo(); friend Foo& operator= (const class Bar); } Foo::Foo (CString string) { m_String = string; } Foo& operator= (const class Bar BarInstance) { return Foo (BarInstance.m_String); } class Bar { private: CString m_String; public: Bar(); ~Bar(); }
But that's not the whole story; the codechar* yourName = "DaFrawg"; MessageBox("Hello there, %s", yourName);
will generate a messagebox with the prompt "Hello there, %s" and the caption "DaFrawg". What I want is, of course, to have the message "Hello there, DaFrawg" displayed. Can somebody help me, please? //Please don't mind my bad EnglishIt's not weird at all, it's standard:
operator=
must be implemented as a member function, according to the definition of the language. The error message also tells you that any overloads ofoperator->
,operator[]
oroperator()
must also be implemented as a member function. I believe it also applies to compound assignments, such as+=
,/=
, etc.operator=
must be a function that modifies the object to which it's applied, i.e. the left-hand side of the assignment statement, which is pointed to bythis
inside the function body. It should return a reference to the object that was modified. -
Try this:
class Bar
{
public:
Bar();
~Bar();CString m\_String;
};
class Foo
{
private:
CString m_String;public:
Foo();
Foo(CString);
~Foo();const Foo& operator=( const Bar& bar );
};
Foo::Foo (CString string)
{
m_String = string;
}const Foo& Foo::operator=(const Bar& BarInstance)
{
m_String = BarInstance.m_String;return (\*this);
}
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
It still doesn't work. I now have the real code I use:
class Foo { public: Foo(); virtual ~Foo(); private: CString m_String; public: friend const Foo& operator= (const class Bar& BarInst); //2x C2801 }; class Bar { public: Bar(); virtual ~Bar(); private: CString m_String; }; const Foo& operator=(const class Bar& BarInst) //C2801 { m_String = Bar.m_String; //C2673, C2227 and C2248. Ignore C2248 return (this*); //C2673 and C2059 }
The compiler is getting berzerk. Not only a C2801, but also two C2673 (Global functions do not have 'this' pointers), C2227 (left of m_String must point to class/struct/union), C2248 (but that's not a real problem) and C2059 (Syntax error: ';'). -
It still doesn't work. I now have the real code I use:
class Foo { public: Foo(); virtual ~Foo(); private: CString m_String; public: friend const Foo& operator= (const class Bar& BarInst); //2x C2801 }; class Bar { public: Bar(); virtual ~Bar(); private: CString m_String; }; const Foo& operator=(const class Bar& BarInst) //C2801 { m_String = Bar.m_String; //C2673, C2227 and C2248. Ignore C2248 return (this*); //C2673 and C2059 }
The compiler is getting berzerk. Not only a C2801, but also two C2673 (Global functions do not have 'this' pointers), C2227 (left of m_String must point to class/struct/union), C2248 (but that's not a real problem) and C2059 (Syntax error: ';').Please refer to M. Dimmick's reply.
Friend
functions are not considered class members; they are normal external functions that are given special access privileges. Also,friend
s are not in the class’s scope, and they are not called using the member-selection operators (. and –>). The code snippet that I provided you is correct. If it is not the correct solution to your problem, however, then you must reconsider the design.
Five birds are sitting on a fence. Three of them decide to fly off. How many are left?
-
DaFrawg wrote: But that's not the whole story; the code char* yourName = "DaFrawg"; MessageBox("Hello there, %s", yourName); will generate a messagebox with the prompt "Hello there, %s" and the caption "DaFrawg". What I want is, of course, to have the message "Hello there, DaFrawg" displayed. Can somebody help me, please?
CString strMessage; strMessage.Format("Hello there, %s", yourName); MessageBox(strMessage);
Michael 'Logic, my dear Zoe, merely enables one to be wrong with authority.' - The Doctor: The Wheel in Space -
Then why doesn't TRACE1("Hello %s", name) fail? ---QUOTE--- "ERROR: Keyboard not present - Press F1 to continue" - Most BOIS chips
DaFrawg wrote: Then why doesn't TRACE1("Hello %s", name) fail? Because the TRACE1 function supports variable arguments. MessageBox does not. Michael 'Logic, my dear Zoe, merely enables one to be wrong with authority.' - The Doctor: The Wheel in Space
-
It's not weird at all, it's standard:
operator=
must be implemented as a member function, according to the definition of the language. The error message also tells you that any overloads ofoperator->
,operator[]
oroperator()
must also be implemented as a member function. I believe it also applies to compound assignments, such as+=
,/=
, etc.operator=
must be a function that modifies the object to which it's applied, i.e. the left-hand side of the assignment statement, which is pointed to bythis
inside the function body. It should return a reference to the object that was modified. -
DaFrawg wrote: But that's not the whole story; the code char* yourName = "DaFrawg"; MessageBox("Hello there, %s", yourName); will generate a messagebox with the prompt "Hello there, %s" and the caption "DaFrawg". What I want is, of course, to have the message "Hello there, DaFrawg" displayed. Can somebody help me, please?
CString strMessage; strMessage.Format("Hello there, %s", yourName); MessageBox(strMessage);
Michael 'Logic, my dear Zoe, merely enables one to be wrong with authority.' - The Doctor: The Wheel in Space