|
-
August 2nd, 2011, 10:40 AM
#1
[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.
-
August 2nd, 2011, 10:47 AM
#2
Re: How do you declare a class within itself?
1. What is COrderPreferences? Where and how is it declared?
2.
 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
-
August 2nd, 2011, 10:51 AM
#3
Re: How do you declare a class within itself?
 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.
 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.
-
August 2nd, 2011, 11:04 AM
#4
Re: How do you declare a class within itself?
 Originally Posted by VictorN
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.
-
August 2nd, 2011, 11:12 AM
#5
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?
-
August 2nd, 2011, 11:15 AM
#6
Re: How do you declare a class within itself?
 Originally Posted by kmkkra
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
 Originally Posted by kmkkra
//-----------Domain.h
and in the
 Originally Posted by kmkkra
//----------COrderPreferences.h
Besides, do you really need so many empty strings in your code snippet?
Victor Nijegorodov
-
August 2nd, 2011, 11:20 AM
#7
Re: How do you declare a class within itself?
 Originally Posted by laserlight
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.
 Originally Posted by laserlight
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
-
August 2nd, 2011, 11:56 AM
#8
Re: How do you declare a class within itself?
 Originally Posted by Lindley
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.
-
August 2nd, 2011, 12:05 PM
#9
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.
-
August 2nd, 2011, 12:47 PM
#10
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
-
August 2nd, 2011, 01:01 PM
#11
Re: How do you declare a class within itself?
 Originally Posted by kmkkra
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?
 Originally Posted by kmkkra
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
-
August 2nd, 2011, 01:33 PM
#12
Re: How do you declare a class within itself?
 Originally Posted by VictorN
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.
 Originally Posted by VictorN
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.
-
August 2nd, 2011, 01:43 PM
#13
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|