CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Dec 2009
    Posts
    11

    Constructor initialization problems

    Hello!
    I'm having some problems with constructor initialization,mainly because I have object members and vectors within classes.
    I have a class Factory in which i have 2 object members(base and dumpster),a vector of Floors and a set of Robots.
    Each Floor has a size(lin,col) and a member object Cell which represents the cells in a given floor
    I want to be able to initialize the Factory upon it's creation with the parameters on the constructor below.
    The problem is: I want to construct the Factory with a certain number of floors(say 2 floors).How do I do that in the constructor?
    I also want to initialize each floor with cells(each cell occupies a 1x2 space upon the construction of the Factory,based on the linxcol of the floor.How do I do that?My current Factory initialization is declared on main like this: Factory f(floors(lin,col,cell(x,y,residue(quant))),robots,dumpster(lposxx,lposyy),base(bposxx,bposyy));
    I think this is only initializing one floor with one cell,but I want it to initialize it as described above!
    Here are the classes:

    Code:
    class Factory
    {
    private:
    	int trackCurrentFloor=0;
    	vector<Floor> floors;
    	set<Robot> robots;
    	Dumpster dumpster;
    	Base base;
    
    public:
    	Factory(vector<Floor> floors,set<Robot> robots,Dumpster dumpster,Base base);
    ...
    }
    
    class Residue
    {
    private:
    	int quantity=0;
    ...
    }
    
    class Floor
    {
    private:
    	int lin;
    	int col;
    	Cell cell;
    }
    
    class Cell
    {
    private:
    	vector<Residue> residues;
    	int xx;
    	int yy;
    }
    
    class Dumpster
    {
    private:
    	int x;
    	int y;
    }
    
    class Base
    {
    private:
    	int x;
    	int y;
    
    }
    
    class Robot
    {
    private:
    	int x;
    	int y;
    
    
    void main()
    {
    
    ...
    
    Factory f(floors(lin,col,cell(x,y,residue(quant))),robots,dumpster(lposxx,lposyy),base(bposxx,bposyy));
    
    ...
    }
    Thanks in advance for the reply!
    Last edited by esmeco; December 27th, 2009 at 03:04 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructor initialization problems

    Quote Originally Posted by esmeco View Post
    Hello!
    I'm having some problems with constructor initialization,mainly because I have object members and vectors within classes.
    1) Use the C++ initialization list to initialize class members. Your C++ book that you're using should explain it.
    Code:
    #include <vector>
    class foo
    {
      int x;
      std::vector<int> vect;
    
      public:
         foo(); // constructor
    };
    
    foo::foo() : x(0), vect(10) { }
    2) The main function returns int, not void.
    Code:
    int main() // not void main()
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    Re: Constructor initialization problems

    So what is the problem?

    Moreover, if you passing big vectors around..Better use reference to pass around Vectors..
    Dont forget to rate my post if you find it useful.

  4. #4
    Join Date
    Dec 2009
    Posts
    11

    Re: Constructor initialization problems

    So, let's say I ask the user to input the number of floors that will be stored in a nfloor variable, how would the constructor and initialization list look like if i use that variable to determine the size of the vector?Also,how would the initialization list look like if I wanted to construct the factory floor with a given number of cells?

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructor initialization problems

    Quote Originally Posted by esmeco View Post
    So, let's say I ask the user to input the number of floors that will be stored in a nfloor variable, how would the constructor and initialization list look like if i use that variable to determine the size of the vector?
    Code:
    #include <iostream>
    #include <vector>
    
    class foo
    {
       unsigned int nFloor;
       std::vector<int> vect;
    
       public: 
            foo(unsigned int x) : vect(x), nFloor(x) { }
    };
    
    using namespace std;
    
    int main()
    {
       int n;
       cout << "Enter number of floors: ";
       cin >> n;
       foo myFoo( n );  
    }
    Also,how would the initialization list look like if I wanted to construct the factory floor with a given number of cells?
    It looks the same as if you are constructing the vector, whether you are using initialization lists or not. I truly believe you should read up on initialization lists, as this is a fundamental of C++ and creating classes.

    Also:

    1) Use const references as parameters -- do not pass vectors and other structs by value.

    2) You should have a constructor for Dumpster, Base, etc. so that when you declare one of them, the members have known values, not uninitialized variables.

    3) Do not have "using namespace std" in a header file. I know you did this because your code has only "vector" and not "std::vector" as the type.

    4) The code you posted has many mistakes such as initializing members within the class itself:
    Code:
    class Factory
    {
       int trackCurrentFloor=0;  // What's this?
    What are you trying to accomplish with that line? If it's what I think it is, this is exactly why the initialization list is used. You initialize members using the init-list, not by setting values in the class definition.
    Code:
    #include <vector>
    #include <set>
    
    class Residue
    {
       private:
    	int quantity;
       public:
           Residue(int q=0) : quantity(q) { }
    };
    
    class Cell
    {
       private:
    	std::vector<Residue> residues;
    	int xx;
    	int yy;
    
       public:
           Cell(int x=0, int y=0) : xx(x), yy(y) { }
    };
    
    
    class Floor
    {
        private:
    	int lin;
    	int col;
    	Cell cell;
    
        Floor(int lin_, int col_) : lin(lin_), col(col_) { }
    };
    
    class Dumpster
    {
        private:
    	int x;
    	int y;
    
        public:
           Dumpster(int x_, int y_) : x(x_), y(y_) { }
    };
    
    class Base
    {
        private:
    	int x;
    	int y;
    
        public:
           Base(int x_, int y_) : x(x_), y(y_) { }
    };
    
    class Robot
    {
       private:
    	int x;
    	int y;
    
       public:
          Robot(int x_, int y_) : x(x_), y(y_) { }
          bool operator < (const Robot& r) const
          {  return x < y; }
    };
    
    
    class Factory
    {
        std::vector<Floor> floors;
        std::set<Robot> robots;
        Dumpster dumpster;
        Base base;
        int trackCurrentFloor;
    
       public:
          Factory(const std::vector<Floor>& floors,
                       const std::set<Robot> robots,
                       const Dumpster& dumpster,
                       const Base& base);
    };
    // In some CPP file...
    Code:
    //...
    Factory::Factory(const std::vector<Floor>& floors_,
                               const std::set<Robot> robots_,
                               const Dumpster& dumpster_,
                               const Base& base_) : 
    
                              floors(floors_), robots(robots_), dumpster(dumpster_),
                             base(base_), trackCurrentFloor(0)
    { 
    }
    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Dec 2009
    Posts
    11

    Re: Constructor initialization problems

    I'm starting to understand it now!

    What I'm in trouble now is with the constructor calling on main.
    Given a specific command like :factory <floors> <line> <col> <robots> I want the user to input the number of floors of the factory,their size and the number of robots so that I can construct the factory.The robots are created in the base which has a random position on the floor.
    How should I call the constructor in main so that I can use those inputs as constructor arguments?
    I should send the number of floors as an argument to create a vector of floors of that size,as well as the number of robots to create a set of that size.
    I guess now the constructor wouldn't have the same parameters as below:

    Factory(const std::vector<Floor>& floors_,const std::set<Robot> robots_,const Dumpster& dumpster_, const Base& base_)

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructor initialization problems

    Quote Originally Posted by esmeco View Post
    I'm starting to understand it now!

    What I'm in trouble now is with the constructor calling on main.
    Given a specific command like :factory <floors> <line> <col> <robots> I want the user to input the number of floors of the factory,their size and the number of robots so that I can construct the factory.The robots are created in the base which has a random position on the floor.
    Look at my first post. You see an integer passed as a parameter, and the vector is initialized using that number.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Dec 2009
    Posts
    11

    Re: Constructor initialization problems

    I don't know if I understood it correctly...what you have is std::vector<int> vect , but that is a vector of integers,correct?What I have is std::vector<Floor> floors ,which is a vector of objects of type Floor and I want the input number of floors to be used as a vector size determiner.
    It will be passed as an integer parameter,but the vector is not type int,but instead of type Floor...How can I use the input number?

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructor initialization problems

    Quote Originally Posted by esmeco View Post
    I don't know if I understood it correctly...what you have is std::vector<int> vect , but that is a vector of integers,correct?What I have is std::vector<Floor> floors ,which is a vector of objects of type Floor and I want the input number of floors to be used as a vector size determiner.
    It will be passed as an integer parameter,but the vector is not type int,but instead of type Floor...How can I use the input number?
    It doesn't matter what the vector type is.

    The std::vector<T> is a template class. What T is doesn't matter -- it could be ints, doubles, chars, Widgets, Elephants, JackHammers, or Floors -- there is a public interface that vector has, and it is the same regardless of what type it is. It wouldn't make any sense if a vector of one type has a different functional interface as a vector of another type.

    Please see the reference on std::vector.

    http://www.cplusplus.com/reference/stl/vector/

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Dec 2009
    Posts
    11

    Re: Constructor initialization problems

    So, instead of an int there would be a Floor in the code to initialize a vector of Floors with a given size?Something like this?

    Code:
    #include <iostream>
    #include <vector>
    
    class foo
    {
       unsigned int nFloor;
       std::vector<Floor> vect;
    
       public: 
            foo(unsigned int x) : vect(x), nFloor(x) { }
    };
    
    using namespace std;
    
    int main()
    {
       int n;
       cout << "Enter number of floors: ";
       cin >> n;
       foo myFoo( n );  
    }
    Last edited by esmeco; December 27th, 2009 at 11:24 PM.

  11. #11
    Join Date
    Jan 2008
    Location
    California, USA
    Posts
    822

    Re: Constructor initialization problems

    Quote Originally Posted by esmeco View Post
    So, instead of an int there would be a Floor in the code to initialize a vector of Floors with a given size?Something like this?

    Code:
    #include <iostream>
    #include <vector>
    
    class foo
    {
       unsigned int nFloor;
       std::vector<Floor> vect;
    
       public: 
            foo(unsigned int x) : vect(x), nFloor(x) { }
    };
    
    using namespace std;
    
    int main()
    {
       int n;
       cout << "Enter number of floors: ";
       cin >> n;
       foo myFoo( n );  
    }
    Yes, except that you'd want to initiailize the members in the order which they were declared.
    But, given the context of this code snippet,
    retaining nFloor as a member is redundant.

  12. #12
    Join Date
    Dec 2009
    Posts
    11

    Re: Constructor initialization problems

    I'm now having some problems with cell initialization and floor initialization.
    I want to,upon creation of each floor,to create cells each with a 3x5 size.
    I'll use an array to store the each cell of the floor.
    Here's my current constructor and Floor class:

    Code:
    class Floor
    {
    private:
    	
    	int lin;
    	int col;
    	int quantcell;
    	Cell cell[??];
    	
    
    public:
    	
    	Piso(int line,int column,int qcell,const Cell &cel);
    ...
    }
    
    class Cell
    {
    private:
    	vector<Residue> residues;
    	int xx;
    	int yy;
    
    public:
    Celula(int x=0,int y=0);
    ...
    }
    
    Factory.cpp 
    
    Factory::Factory(int nfloors,int line,int column,int nrobots):floors(nfloors,Floors(line,column,(linha*col)/15,cell(0,0)),robots(nrobots),trackActualFloor(0)
    I want the cell array size to be (line*column)/15.The 15 would be the cell dimension(3x5).So the number of cells in a floor is represented by that equation.
    How would I do to initialize the cell array size?
    Does this initialization list in the factory look ok?I'm having some troubles initializing the floors,mostly because I'm working with vectors as object members!

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructor initialization problems

    Quote Originally Posted by esmeco View Post
    I'm now having some problems with cell initialization and floor initialization.
    I want to,upon creation of each floor,to create cells each with a 3x5 size.
    I'll use an array to store the each cell of the floor.
    Do you know the purpose of vector? It is a dynamic array that you can size to any dimensions that you want.

    Given that, why are you going back to straight arrays? Just use vectors. Another thing is that "normal" arrays cannot be resized -- they are fixed in size.

    You must use a container, i.e. vector.
    Code:
    class Floor
    {
    private:
    	
    	int lin;
    	int col;
    	int quantcell;
    	std::vector<Cell> cell;
    
        public:
    	Floor(int numLines = 0, int numCols = 0);
    };
    Code:
    Floor::Floor(int numLines, int numCols) :
         lin(numLines), col(numCols), cell( /* you fill this in */ )
    {
    }
    I don't know if you're getting the idea that all of these principles and techniques we're showing you are to be used by you in the program in the appropriate situations, not just in one place for one thing.

    If you know that vectors can be of any type, and that vectors act as dynamic arrays (I gave you the link to the vector class and what it does), it should be obvious that this is the thing that you wanted to use in your Floor class.

    Regards,

    Paul McKenzie

  14. #14
    Join Date
    Dec 2009
    Posts
    11

    Re: Constructor initialization problems

    The problem is that the requirements state that I should not use the STL library for that purpose,so that is why I'm thiking of arrays.

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructor initialization problems

    Quote Originally Posted by esmeco View Post
    The problem is that the requirements state that I should not use the STL library for that purpose
    What is with these C++ courses that do not want to teach C++?
    so that is why I'm thiking of arrays.
    Well, arrays are impossible to use. Again, they are fixed in size -- you cannot resize them. So forget about using them, regardless of what your requirements are.

    Either use container classes, or you have to go and code your own dynamic array class -- those are your two options.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 28th, 2009 at 01:33 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured