struct in class c++

Jun 10, 2009 at 9:55am
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
Last edited on Jun 10, 2009 at 9:56am
Jun 10, 2009 at 11:42am
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;
};

Jun 10, 2009 at 3:02pm
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;

private:
struct_list x;

};

E::E(){
cont = 0;}
E::E (const E & copy){
cont = copy.cont;
}
E& E::operator=(const E&rr){

if (this == &rr) return *this;
cont = rr.cont;
}

in the main

typedef list<E> Es;
Es struct;
struct x;
E copia;
int n=1;
int nn=1;
for(n=1;n<=10;n++){

copia.cont=0;
for (nn=1; nn<=32; nn++){

struct x;
x.d=0;
x.l=0;
x.v=0;
struct.push_back(x);
}
Es.push_back(copia);
}

list<E>::iterator a;
for (a=Es.begin(); a!=Es.end(); ++a) {
cout << *a << endl;
for (Es::iterator pp= struct.begin(); pp!= struct.end(); ++pp){
E& p = *pp;
for(size_t h=1; h<=32; ++h){
cout << p.v << endl;
cout << p.l << endl;
cout << p.d << endl;
}
}
}

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
................
..................
...............


what is wrong?
thanks!






Jun 10, 2009 at 3:38pm
It would really help if you formatted your code using the code tag.

You don't need to write a copy constructor or copy operator as your class doesn't contain pointers. In fact, your version doesn't copy the x.

You've missed the close brace off the class declaration.

This is a syntax error I expect. But I can't work out what you're trying to do as it's difficult to read without formatting.
1
2
3
typedef list<E> Es;
Es struct;
struct x;


And this is definitly incorrect as Es is a type not a variable.
1
2
list<E>::iterator a;
for (a=Es.begin(); a!=Es.end(); ++a) {
Topic archived. No new replies allowed.