vector of vectors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ejack
    New Member
    • Mar 2008
    • 12

    vector of vectors

    Hello:

    This is my first time here so I hope I am doing this correctly. I am trying to push a vector object into a vector (I think). Here is my code (header file first) but it's only part of the code. Hope this is enough to make sense. I am getting the error:

    Code:
    IrrigationEstimator.cpp
    c:\documents and settings\developer\desktop\c++ ii\jacksonp5\jacksonp5\irrigationestimator.cpp(53) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'const IrrigationComponent &'
            with
    [CODE=cpp]#include "IrrigationComp onent.h"
    #include <vector>
    #include <string>
    using namespace std;
    // This is IrrigationEstim ator.h

    class IrrigationEstim ator
    {
    private:
    vector<Irrigati onComponent> vComp;
    vector < vector<Irrigati onComponent> > vItems;
    string companyName, busName;
    public:
    IrrigationEstim ator();
    bool ReadData();
    string WriteInvoice();
    };

    // This is my read function and where I am getting the error. See below (line 53).
    bool IrrigationEstim ator::ReadData( )
    {
    string filename;
    ifstream input;
    input.open("Job .txt");
    if(!input)
    {
    return false;
    }

    string component;
    string type;
    float gph;
    float cost;
    int required;

    getline(input, companyName);
    while(!input.eo f())
    {
    int i = 0;
    getline(input, component);
    getline(input, type);
    input >> gph;
    input >> cost;
    input >> required;
    input.ignore();

    vector<Irrigati onComponent> component;
    vector<Irrigati onComponent> type;
    vector<Irrigati onComponent> gph;
    vector<Irrigati onComponent> cost;
    vector<Irrigati onComponent> required;

    vItems.push_bac k(component);
    (this being line 53) vComp.push_back (vItems);

    vItems.push_bac k(type);
    vItems.push_bac k(gph);
    vItems.push_bac k(cost);
    vItems.push_bac k(required);

    i++;

    }
    input.close();
    return true;
    }[/CODE]

    Thank you for any and all help.
    Last edited by Ganon11; Mar 12 '08, 04:18 PM. Reason: Please use the [CODE] tags provided.
  • Studlyami
    Recognized Expert Contributor
    • Sep 2007
    • 464

    #2
    First of all please use the CODE tags around your code it makes it a lot easier to read.

    Take a look at your definition of your vectors.
    Code:
    vector<IrrigationComponent> vComp;
    vector < vector<IrrigationComponent> > vItems;
    when you do vComp.pushback( vItems) you are trying to push a vector<vector<I rrigationCompon ent>> when the compiler is expecting IrrigationCompo nent. I THINK you can fix this by
    Code:
     vector< vector < vector <IrrigationComponent> > > vComp;
    but that is ugly. Can we see your IrrigationCompo nent class? You created a vector of cost, type ect. which seems like you could wrap each of those vectors in a class and just have one vector which contains an object which has a vector of costs, type ect. I hope that makes sense.

    Comment

    • ejack
      New Member
      • Mar 2008
      • 12

      #3
      Sorry about the code tags. Hope I did it right this time. Here is the IrrigationCompo nent class. Thank you so much for any and all help.

      Code:
      #include <string>
      using namespace std;
      
      
      class IrrigationComponent
      {
      private:
      	string type, name;
      	int required;
      	float gallons, gph, cost, totalCost;
      	void CalcCost();
      	void CalcGPH();
      
      public:
      	IrrigationComponent();
      
      	void SetName(string n);
      	void SetType(string t);
      	void SetRequired(int r);
      	void SetGallons(float g);
      	void SetCost(float c);
      
      	float GetUnitCost();
      	float GetTotalCost();
      	float GetTotalGPH();
      	int GetNumber();
      	string GetName();
      	string GetType();
      };

      Comment

      • Studlyami
        Recognized Expert Contributor
        • Sep 2007
        • 464

        #4
        Okay in the IrrigationCompo nent class you have variable who has the same name as your vectors in your estimator class. Why do you need a vector for each one of these if you already have a variable for it in the IrrigationCompo nent?
        I don't understand the full program, but it seems to me you have unneeded vectors.

        I see your program like this: please correct it where I'm wrong. You want a program to calculate an Irrigation project. The IrrigationEstim ator class will contain the entire proposal which includes IrrigationCompo nents which make up the job along with a few other variables variables and functions (print invoice ect.). Each IrrigationCompo nent is a small task that is needed to be done to complete the project (and makes up the Estimate).

        I don't see why you need a vector of type, vector of name, vector of gph ect. in your IrrigationEstim ator class. Each component already has a type, name gph, cost ect. Please expand a little bit on your logic.

        Comment

        • ejack
          New Member
          • Mar 2008
          • 12

          #5
          That's exactly what I need the program to do. Here is the original Irrigation Estimator class. I just don't understand how to push items into the vComp vector:

          Code:
          class IrrigationEstimator
          {
          private:
          	vector<IrrigationComponent> vComp;
          	string companyName, busName;
          public:
          	IrrigationEstimator();
          	bool ReadData();
          	string WriteInvoice();
          };

          I guess my real problem is how to push items into the vector? When I use this:

          Code:
          		vector<IrrigationComponent> component;
          
          		vComp.push_back(component);
          I get the error:

          Code:
           error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'std::vector<_Ty>' to 'const IrrigationComponent &'
          When I added the other vector(s), it was pushing items in, but the wrong way. Hope this makes some sense.

          Comment

          • fual
            New Member
            • Feb 2008
            • 28

            #6
            what you want to do to begin with is probably add some typedefs, this makes things nicer.

            Code:
            class irrigation_estimator
            {
            public:
            	typedef irrigation_estimator type;
            	typedef irrigation_component component_type;
            	typedef std::vector<component_type> component_vector_type;
            private:
            	component_vector_type componentVector_;
            	std::string companyName_;
            	std::string businessName_;
            public:
            	irrigation_estimator( );
            	bool read_data( void );
            	std::string write_invoice( void );
            };
            The beauty of the typedef is that if you decide to change the name of your classes then you only need to change one or two lines of code and it creates consistency over all your classes, which makes it easier to read and code. It is also a good idea to suffix all of your private data members with an underscore; this distinguishes them from other variables and again makes code more readable. I would also say don't be afraid to have slightly longer variable names to improve readability, i.e businessName_ rather than busName_.

            Next you need to add a method for adding components
            Code:
            class irrigation_estimator
            {
            public:
            	typedef irrigation_estimator type;
            	typedef irrigation_component component_type;
            	typedef std::vector<component_type> component_vector_type;
            private:
            	component_vector_type componentVector_;
            	std::string companyName_;
            	std::string businessName_;
            public:
            	irrigation_estimator( );
            	bool read_data( void );
            	std::string write_invoice( void );
            	void add_component( const component_type& component ) {
            		componentVector_.push_back( component );
            	}
            };

            Comment

            • ejack
              New Member
              • Mar 2008
              • 12

              #7
              Studlyami:

              Thank you for your help.

              Comment

              • Studlyami
                Recognized Expert Contributor
                • Sep 2007
                • 464

                #8
                I don't believe you need a vector of vectors in this project. Your Estimator class should have one vector of IrrigationCompo nent. I think you don't quite understand the vector class and you should take a look at this reference.
                vector<Irrigati onComponent> component;
                vComp.push_back (component);

                I get the error:

                error C2664: 'std::vector<_T y>::push_back' : cannot convert parameter 1 from 'std::vector<_T y>' to 'const IrrigationCompo nent &'
                the problem there is the same as the first problem you had. You declare a vector like so, vector<TYPE> Variable_Name. When you pushback an item into a vector it has to be the same TYPE as was specified in the TYPE field when you declared the vector. If you have a vector<Irrigati onComonent> when you do a pushback, you must push an object of IrrigationComon ent, not a vector of IrrigationComon ent. Here a little stub to hopefully help you.

                [CODE=cpp]
                class IrrigationEstim ator
                {
                private:
                vector<Irrigati onComponent> vComp;
                . . .
                }

                //where you create a new Component
                IrrigationCompo nent MyNewComponent;
                MyNewComponent. name = "Component Name";
                MyNewComponent. type = "Type";
                MyNewComponent. cost = 234; //would call your calc cost function.

                vComp.pushback( MyNewComponent) ; //add the object we just created to our vector.

                /*in your estimator class when you want to calc the total cost or create the invoice would go through your vector like so*/
                int iTotalCost = 0
                for(int i = 0; i<(int)vComp.si ze() ; i++)
                {
                iTotalCost += vComp[i].cost; //keep a running total of the cost for each component.
                }
                [/CODE]

                If you see any problems or don't understand something please post up any questions.

                Comment

                • ejack
                  New Member
                  • Mar 2008
                  • 12

                  #9
                  That's it! That's exactly it! Thank you, thank you, THANK YOU!!!! I was making it much harder than it had to be. Thank you so much for all your help.

                  Comment

                  Working...