0

I have 3 classes.

class piesa_a{

protected:

    int id;
    char *tip;
    int pret;

public:
[custructor with/without param, display function - works well each one of it]




class piesa_b:public piesa_a
{

private:
    float lungime;
    bool bw;
public:
[custructor with/without param, display function - works well each one of it]

class piesa_c:public piesa_a
{

    private:
        int nr;
        piesa_b *buf;
    public:
        piesa_c():piesa_a(){nr=0; buf = new piesa_b[nr];}
        piesa_c(int n, piesa_b *bu,int aid, char *tipi, int pretzz):piesa_a(aid,tipi,pretzz) 
        {
            buf = new piesa_b[nr];
            for(int i=0;i<nr;i++)
                buf[i]= bu[i];
        }
    void afisare()
    {

        cout<<nr;

    }

In main i have this:

piesa_c C(2, H,14,"TIPC",20);


C.afisare();

But this doesn't work. I don't know if the "buf" was declared properly because the problem seems to be in last class. Why?

Later Edit: The entire code is here: http://pastebin.com/nx2FGSfe.

Now, i have this in main

int main(int argc, char** argv) {

    piesa_b *H;
            H = new piesa_b[2];

    piesa_a A(4,"TIPA",120);
    piesa_b B(100,1,3,"TIPA",120);
    H[0]=B;
    H[1]=B;
    piesa_c C(2, H,14,"TIPC",20);

    piesa_a** v = new piesa_a*[3];

    v[0] = &A;
    v[1] = &B;
    v[2] = &C;

    for(int i=0;i<3;i++)
        v[i].afisare();

    return 0;
}

The display function return this error

main.cpp:143:14: error: request for member ‘afisare’ in ‘*(v + ((unsigned int)(((unsigned int)i) * 4u)))’, which is of non-class type ‘piesa_a*’
kraYz
  • 587
  • 1
  • 4
  • 11

2 Answers2

3

nr is not initialized in the piesa_c() constructor, meaning it will have an undefined value.

Instead of using a dynamically allocated array used a std::vector<piesa_b> instead. It will handle dynamic memory allocation and do the right thing when instances of piesa_c is copied. Using std::vector also means the nr member variable can omitted as that information can be obtained from vector::size() and the std::vector can be populated in the initializer list instead of in the constructor body:

std::vector<piesa_b> buf;

piesa_c(int      n,
        piesa_b *bu,
        int      aid,
        char*    tipi,
        int pretzz) : piesa_a(aid,tipi,pretzz),
                      buf(bu, bu + nr) {}

And to invoke a member function on each element in buf:

// C++11 lambda, otherwise use
// std::vector<piesa_b>::const_iterator.
//
std::for_each(buf.begin(), buf.end(), [](piesa_b& pb) { pb.afisare(); });

If afisare() does not modify then make it const:

void afisare() const
{
}

Additonally, use std::string instead of char*. If you insist on having dynamically allocated members in the classes you need to obey the rule of three.

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
2

I am not sure what "not work" means in this context, but when you call this constructor:

piesa_c C(2, H,14,"TIPC",20);

the data member nr is not set. It can have any value that fits into an int, so when you use it to initialize an array you will get variable and weird results.

Note that you could save yourself a lot of trouble by using std::vector and std::string instead of dynamically allocated arrays and char*.

hmjd
  • 120,187
  • 20
  • 207
  • 252
juanchopanza
  • 223,364
  • 34
  • 402
  • 480