Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Weird VC++ behaviour

Weird VC++ behaviour

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestionlearning
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DaFrawg
    wrote on last edited by
    #1

    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 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? //Please don't mind my bad English

    M D M 3 Replies Last reply
    0
    • D DaFrawg

      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 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? //Please don't mind my bad English

      M Offline
      M Offline
      Michael P Butler
      wrote on last edited by
      #2

      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

      D 2 Replies Last reply
      0
      • D DaFrawg

        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 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? //Please don't mind my bad English

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        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?

        D 1 Reply Last reply
        0
        • D DaFrawg

          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 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? //Please don't mind my bad English

          M Offline
          M Offline
          Mike Dimmick
          wrote on last edited by
          #4

          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 of operator->, operator[] or operator() 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 by this inside the function body. It should return a reference to the object that was modified.

          D 1 Reply Last reply
          0
          • D David Crow

            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?

            D Offline
            D Offline
            DaFrawg
            wrote on last edited by
            #5

            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: ';').

            D 1 Reply Last reply
            0
            • D DaFrawg

              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: ';').

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              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, friends 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?

              1 Reply Last reply
              0
              • M Michael P Butler

                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

                D Offline
                D Offline
                DaFrawg
                wrote on last edited by
                #7

                Then why doesn't TRACE1("Hello %s", name) fail? ---QUOTE--- "ERROR: Keyboard not present - Press F1 to continue" - Most BOIS chips

                M 1 Reply Last reply
                0
                • D DaFrawg

                  Then why doesn't TRACE1("Hello %s", name) fail? ---QUOTE--- "ERROR: Keyboard not present - Press F1 to continue" - Most BOIS chips

                  M Offline
                  M Offline
                  Michael P Butler
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • M Mike Dimmick

                    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 of operator->, operator[] or operator() 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 by this inside the function body. It should return a reference to the object that was modified.

                    D Offline
                    D Offline
                    DaFrawg
                    wrote on last edited by
                    #9
                    1. the operator function IS a member of the class 2) my (new) code IS using the this pointer and returns it And still VC++ keeps saying "Operator '=' must be an member.
                    1 Reply Last reply
                    0
                    • M Michael P Butler

                      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

                      D Offline
                      D Offline
                      DaFrawg
                      wrote on last edited by
                      #10

                      Thanks, it works really good.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups