variable-sized object `bin' may not be initialized

working on school project that uses one class object constructor to call another to make an array of objects and store data.. tried a forloop that displays correctly from inside the loop but not outside..
here's the header..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef PROG4CLASSESANDFUNCTS_H
#define PROG4CLASSESANDFUNCTS_H


#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <math.h>
#include <windows.h>
#include <string>
#include <cstdlib>



using namespace std;
//********************************************************************
//CLASSES
//********************************************************************
//************************************************************
//
//************************************************************
//********************************************************************
//Class InvBin
//********************************************************************
class InvBin
{
private:
     string description;  // item name
     int qty;                     // Quantity of items
                                      // in this bin
public:
     InvBin (string d , int q );//2-parameter constructor
               //with default values

// It will also have the following public member functions. They
//will be used by the BinManager class, not the client program.

void setDescription( string d );
string getDescription();
void setQty ( int q );
int getQty();
};

//********************************************************************
//Class BinManager
//********************************************************************
class BinManager
{
private:
   InvBin bin[30];                 //array of InvBin objects
   int numBins;                   // number of bins
                               //currently in use

public:
    BinManager();                    // default constructor


    BinManager(int size, string d[], int q[]);
  
// the class will also have the following public member functions:

     string getDescription(int index);  //returns name of one item
     int getQuantity(int index) ;       // returns qty of one item
     string DisplayAllBins() ;   //returns string having one line 
                                  //for each item
     bool addParts(int binIndex, int q); //these returns true if the
     bool removeParts(int binIndex, int q); //action was done and false
                                      // if it could not be done-
                                     //see validation information
};



#endif 


and here's my BinManager constructor..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//********************************************************************
//Class BinManager
//********************************************************************

    BinManager::BinManager()                    // default constructor
    { numBins = 0;}


    BinManager::BinManager(int size, string d[ ], int q[ ])
    {
       // 3-Parameter constructor:: Recieves number of  
      //bins in use and parallelarrays of item names and 
      //quantities. Uses this info. to store values in the 
      //elements of the bin array. Remember, these elements 
      //are InvBin objects.
      int index;
      cout<<"constructor"<<endl;
        for( index=0 ; index < size ; index++ )
      {
            InvBin bin[index](d[index], q[index]);
           cout<<fixed<<left<<index +1<<"   ";
            cout<<setw(20)<<bin[index].getDescription();
            cout<<"    "<<bin[index].getQty()<<endl;
            
           
      }
Sleep(2000);
   }


and here's the BinManager member function for displaying all bins..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string BinManager::DisplayAllBins()
{
       int i;
       cout<< "Current Inventory: "<<endl;
       cout<<"still not working"<<endl;
       for (i=0; i<9;i++)
       {
           cout<<fixed<<left<<i<<"     "<<setw(20);
           cout<<bin[i].getDescription();
            cout<<"    "<<bin[i].getQty()<<endl;
       }
       Sleep(4000);
return 0 ;
};


as you can see by the comments..
1
2
3
4
5
// 3-Parameter constructor:: Recieves number of  
      //bins in use and parallelarrays of item names and 
      //quantities. Uses this info. to store values in the 
      //elements of the bin array. Remember, these elements 
      //are InvBin objects. 


this last part was taken straight from the book, as was the header file.. so I CAN'T use a vector, nor can I use pointers, since we haven't covered that yet..(yes I'm aware of them but haven't practiced with them yet).. i know that because it's a forloop that local data isn't stored once it ends, so how do i get it to save? i get the feeling i'm doing something stupid that i should SEE is wrong but i don't see it... HELP!
Continuation of http://cplusplus.com/forum/general/64214/

That's too bad that you can't use vectors. Still the problem is the same as yesterday. In BinManager's constructor you have:
InvBin bin[index](d[index], q[index]);

This doesn't call the constructor for the class-member bin. It creates a local version of bin which is destroyed at the end of each for loop iteration.

Since Bin is created in the declaration of BinManager, I don't think you can apply another constructor to it (a default constructor is called which doesn't do anything). Use a function instead:

To declare:
1
2
3
4
5
6
7
8
9
10
11
12
13
class InvBin
{
private:
     string description;  
     int qty;                   
public:
     void init(string d , int q ); // void init() replaces the constructor

     void setDescription( string d );
     string getDescription();
     void setQty ( int q );
     int getQty();
};


Then to call:
1
2
3
4
5
6
7
8
9
10
11
12
13
BinManager::BinManager(int size, string d[ ], int q[ ])
{
      int index;
      cout<<"constructor"<<endl;
      for( index=0 ; index < size ; index++ )
      {
            bin[index].init(d[index], q[index]); // We call the init function instead of a constructor.
            cout<<fixed<<left<<index +1<<"   ";
            cout<<setw(20)<<bin[index].getDescription();
            cout<<"    "<<bin[index].getQty()<<endl;
      }
    Sleep(2000);
}
Last edited on
Topic archived. No new replies allowed.