CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2008
    Location
    greater Cincinnati
    Posts
    54

    [RESOLVED] How do you declare a class within itself?

    Hello and good day.

    IDE: code::blocks
    OS: Windows 7



    I am a novice C++ programmer, and I am having trouble declaring a class withing itself or actually a vector of a class within itself.
    I am not sure on how to go about doing it or what alternatives I have. Whenever I add the " std::vector<COrderPreferences>
    orderPreferences; " to my CDomain class I get 1 very big error in stl_algobase.h - or I can comment out "std::vector<CDomain> domain;"
    , and leave the COrderPreferences vector and everything works.




    So below is a sample of my problem, could someone show me what is wrong, and give me some advice?





    Code:
    //-----------Domain.h
     
    #ifndef CDOMAIN_H
    #define CDOMAIN_H
    #include <vector>
     
     
     
     
    class CDomain
    {
        public:
     
            std::vector<COrderPreferences> orderPreferences;
            std::vector<CDomain> domain;
            void create();
    
    };
     
    #endif // CDOMAIN_H
    
     
     
    
     
    //---------CDomain.cpp
     
    #include "CDomain.h"
     
    void CDomain::create()
    {
         domain.resize(3);
    
    }
    
     
     
    
    
    //EDITED in respect to post #6 VicotorN
    
    
    
    
    
    
    
    
    
    
    //-------------COrderPreferences.h
    
    
    #ifndef CORDERPREFERENCES_H
    #define CORDERPREFERENCES_H
    
    #include <vector>
    #include <string>
    
    
    class COrderPreferences
    {
        public:
    
             struct ControlAttributes
            {
                   DWORD exStyles;
                   std::string types;
                   std::string defaultCaption;
                   DWORD styles;
                   int XStart;
                   int YStart;
                   int XWidth;
                   int YHeight;
    
    
    
                   ControlAttributes& operator=(ControlAttributes& a)
                   {
    
    //                   if  (this != &a) // protect against invalid self-assignment
    //                   {
                           exStyles       =    a.exStyles;
                           types          =    a.types;
                           defaultCaption =    a.defaultCaption;
                           styles         =    a.styles;
                           XStart         =    a.XStart;
                           YStart         =    a.YStart;
                           XWidth         =    a.XWidth;
                           YHeight        =    a.YHeight;
    
    
    //                   }
    
                       return *this;
                   }
            };
          
      
    
            ControlAttributes controlAttributes[15];
    };
    
    
    #endif // CORDERPREFERENCES_H



    Thank you very much for your time and effort. Best of wishes to everyone in this forum!
    Last edited by kmkkra; August 2nd, 2011 at 12:58 PM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: How do you declare a class within itself?

    1. What is COrderPreferences? Where and how is it declared?

    2.
    Quote Originally Posted by kmkkra
    Code:
            std::vector<COrderPreferences> domain;
            std::vector<CDomain> domain;
    You cannot declare two different class members with the same name.
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How do you declare a class within itself?

    Quote Originally Posted by kmkkra
    I am a novice C++ programmer, and I am having trouble declaring a class withing itself or actually a vector of a class within itself.
    You cannot define a class with a member object of the class type, but you should be able to define a class with a member vector of objects of the class type.

    Quote Originally Posted by kmkkra
    I am not sure on how to go about doing it or what alternatives I have. Whenever I add the " std::vector<COrderPreferences>
    orderPreferences; " to my CDomain class I get 1 very big error in stl_algobase.h.
    However, COrderPreferences is a different class type. Try this:
    Code:
    class CDomain
    {
        public:
     
            std::vector<COrderPreferences> domain;
            void create();
    
    };
    Do you still get the error? If so, you know that the problem is with COrderPreferences, not with CDomain. For example, it appears that you did not include the header that contains the definition of COrderPreferences in the given header.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Jun 2008
    Location
    greater Cincinnati
    Posts
    54

    Re: How do you declare a class within itself?

    Quote Originally Posted by VictorN View Post
    1. What is COrderPreferences? Where and how is it declared?

    2.You cannot declare two different class members with the same name.

    A:1) I edited my original post for you. It now has COderPreferences.h included.


    A:2) sorry, typo. I edited

    Code:
    std::vector<COrderPreferences> domain;
    to...

    Code:
    std::vector<COrderPreferences> orderPreferences;
    Last edited by kmkkra; August 2nd, 2011 at 11:23 AM.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How do you declare a class within itself?

    Can you explain the high-level concept with this design? Are members of the domain vector representing subdomains of the given object?

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: How do you declare a class within itself?

    Quote Originally Posted by kmkkra View Post
    A:1) I edited my original post for you. It now has COderPreferences.h included.


    A:2) sorry, typo. I edited that too.
    For me?
    I didn't need it.

    But now there are two declarations of the CDomain class: in the
    Quote Originally Posted by kmkkra View Post
    //-----------Domain.h
    and in the
    Quote Originally Posted by kmkkra View Post
    //----------COrderPreferences.h
    Besides, do you really need so many empty strings in your code snippet?
    Victor Nijegorodov

  7. #7
    Join Date
    Jun 2008
    Location
    greater Cincinnati
    Posts
    54

    Re: How do you declare a class within itself?

    Quote Originally Posted by laserlight View Post
    You cannot define a class with a member object of the class type, but you should be able to define a class with a member vector of objects of the class type.
    Thanks - that's what I thought.



    Quote Originally Posted by laserlight View Post
    However, COrderPreferences is a different class type. Try this:
    Code:
    class CDomain
    {
        public:
     
            std::vector<COrderPreferences> domain;
            void create();
     
    };
    Do you still get the error? If so, you know that the problem is with COrderPreferences, not with CDomain. For example, it appears that you did not include the header that contains the definition of COrderPreferences in the given header.

    No, I don't get any errors with what you wrote.

    And also, yes, the COrderPreferences.h is included in CDomainView.h

  8. #8
    Join Date
    Jun 2008
    Location
    greater Cincinnati
    Posts
    54

    Re: How do you declare a class within itself?

    Quote Originally Posted by Lindley View Post
    Can you explain the high-level concept with this design? Are members of the domain vector representing subdomains of the given object?

    No. The domain vector actually represents any domain object that is instantiated, including even the same CDomain object that instantiates through CDomain::create().

    CDomain::create() actually should use vector::insert() as opposed to vector::resize().

    std::vector<CDomain> domain; will ultimately be declared static, and the first domain element will be instantiated within another class: CApplication. All other elements will be instatiated within the class CDomain using CDomain::create();

    I didn't declare it static to begin with to decrease any unnecessary bits. I get the same problem, though, whether it is static or not.
    Last edited by kmkkra; August 2nd, 2011 at 12:16 PM.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How do you declare a class within itself?

    I think you need to make it static immediately. Even if there are other problems, the basic meaning of the field is totally different when it's static versus not static.

  10. #10
    Join Date
    Jun 2008
    Location
    greater Cincinnati
    Posts
    54

    Re: How do you declare a class within itself?

    Okay I think I may have got it figured out. I don't know.


    The problem seems to be with the
    " ControlAttributes& operator=(ControlAttributes& a) "
    contained in COrderPreferences.


    I have no idea why I didn't realize this until after I posted. I racked my brain trying to figure out what the problem was, but I tend to realize things after I ask for help...??? I have no idea why that happens.





    Anyway, can someone tell me what is wrong with my
    Code:
     " ControlAttributes& operator=(ControlAttributes& a) "
    ?

    Thanks

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: How do you declare a class within itself?

    Quote Originally Posted by kmkkra View Post
    Okay I think I may have got it figured out. I don't know.


    The problem seems to be with the
    " ControlAttributes& operator=(ControlAttributes& a) "
    contained in COrderPreferences.
    What error do you get?

    Quote Originally Posted by kmkkra View Post
    Anyway, can someone tell me what is wrong with my
    Code:
     " ControlAttributes& operator=(ControlAttributes& a) "
    ?
    I'd change it to
    Code:
     " ControlAttributes& operator=(const ControlAttributes& a) "
    Victor Nijegorodov

  12. #12
    Join Date
    Jun 2008
    Location
    greater Cincinnati
    Posts
    54

    Re: How do you declare a class within itself?

    Quote Originally Posted by VictorN View Post
    I'd change it to
    Code:
     " ControlAttributes& operator=(const ControlAttributes& a) "
    Ok I made the change to const and rebuilt everything and error went away. I didn't know the operator= parameter had to be const, but then again I am a novice.



    Quote Originally Posted by VictorN View Post
    What error do you get?
    The error is pretty big. I'll post it but if anyone thinks I should delete or reduce it's size to save space just let me know.


    ==================================================
    EDITED in respect to post #13 Lindley
    C:\Users\MattKL\Desktop\New folder (2)\PointClick - Copy\ShareQuantityModulator\src\CDomainView.cpp|76|instantiated from here|

    c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_algobase.h|343|error: no match for 'operator=' in '* __result = * __first'|


    ||=== Build finished: 1 errors, 0 warnings ===|
    ==================================================



    Thanks VictorN and everyone for your help. I will declare this thread resolved in a few days if I don't get any more related errors.
    Last edited by kmkkra; August 2nd, 2011 at 01:48 PM.

  13. #13
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: How do you declare a class within itself?

    It's not uncommon for template code to generate really big errors for small problems. The key is usually to find the line number in your code where the problem is occurring. In this case, this is the relevant bit:

    C:\Users\MattKL\Desktop\New folder (2)\PointClick - Copy\ShareQuantityModulator\src\CDomainView.cpp|76|instantiated from here|

    The line with the actual error message may in some cases give you further useful information:
    c:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\include\c++\bits\stl_algobase.h|343|error: no match for 'operator=' in '* __result = * __first'|

    The rest of it is mostly useless.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured