Hi!
this is my problem:
I have a class where I have define a struct.at most I can have 32 of this struct:
class E{
int cont;
struct X {
int l;
int v;
int d;
}x[32]
E();
ecc...
};
the first question is:
must I do a copy constructor for X too?
Inside the main() I have create a list<E>r.
How can I access to the struct for modify it with iterator?
I am very sorry for my english but I hope somebody can understand my problem.
thanks in advance
As your class doesn't contain pointers, the default copy constructor and copy operator are fine.
As for getting to each element in a std list:
1 2 3 4 5 6 7 8 9 10 11 12
typedef std::list<E> Es;
Es elist;
// ... add to the list
for (Es::iterator p = elist.begin(); p != elist.end(); ++p)
{
E& e = *p;
for (size_t i = 0; i < 32; ++i)
{
std::cout << e[i].v << std::endl;
}
}
Typically, you'd think of E having an aggregate relationship with X and representing that with a collection class rather than fixing an array into E.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class E
{
public: // If X appears in the interface functions, X must be public.
struct X
{
int l;
int v;
int d;
};
typedef std::vector<X> X_list;
private:
X_list x;
};
I have done in this way....but still some problem.
I post part of my script in order to anderstand the error(s)
In the header file
using namespace std;
class E {
friend ostream &operator<<(ostream & outp, const P & p)
{ cout << "contatore per le copie :" << p.cont << endl;
return outp;
}
public:
int cont;
struct X {
public:
int d;
int l;
int v;
};
typedef list<X> struct_list;
E();
E (const E & );
E& operator=(const E & );
int operator==(const E::X &rhs) const;
Some error are:
mioprogramma.cc:176: error: expected `;' before ‘x’
mioprogramma.cc:177: error: ‘x’ was not declared in this scope
mioprogramma.cc:186: error: expected unqualified-id before ‘.’ token
mioprogramma.cc:190: error: expected primary-expression before ‘.’ token
mioprogramma.cc:190: error: expected primary-expression before ‘.’ token
mioprogramma.cc:195: error: ‘class P’ has no member named ‘v’
mioprogramma.cc:196: error: ‘class P’ has no member named ‘l’
mioprogramma.cc:197: error: ‘class P’ has no member named ‘d’
mioprogramma.cc:1366: error: expected primary-expression before ‘.’ token
mioprogramma.cc:1394: error: expected primary-expression before ‘.’ token
mioprogramma.cc:1422: error: expected primary-expression before ‘.’ token
................
..................
...............