473,796 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vector of vectors

12 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. IrrigationEstimator.cpp
  2. 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 &'
  3.         with
Expand|Select|Wrap|Line Numbers
  1. #include "IrrigationComponent.h"
  2. #include <vector>
  3. #include <string>
  4. using namespace std;
  5. // This is IrrigationEstimator.h
  6.  
  7. class IrrigationEstimator
  8. {
  9. private:
  10.     vector<IrrigationComponent> vComp;
  11.     vector < vector<IrrigationComponent> > vItems;
  12.     string companyName, busName;
  13. public:
  14.     IrrigationEstimator();
  15.     bool ReadData();
  16.     string WriteInvoice();
  17. };
  18.  
  19. // This is my read function and where I am getting the error.  See below (line 53).
  20. bool IrrigationEstimator::ReadData()
  21. {
  22.     string filename;
  23.     ifstream input;
  24.     input.open("Job.txt");
  25.     if(!input)
  26.     {
  27.         return false;
  28.     }
  29.  
  30.     string component;
  31.     string type;
  32.     float gph;
  33.     float cost;
  34.     int required;
  35.  
  36.     getline(input, companyName);
  37.     while(!input.eof())
  38.     {
  39.         int i = 0;
  40.         getline(input, component);
  41.         getline(input, type);
  42.         input >> gph;
  43.         input >> cost;
  44.         input >> required;
  45.         input.ignore();
  46.  
  47.         vector<IrrigationComponent> component;
  48.         vector<IrrigationComponent> type;
  49.         vector<IrrigationComponent> gph;
  50.         vector<IrrigationComponent> cost;
  51.         vector<IrrigationComponent> required;
  52.  
  53.         vItems.push_back(component);
  54. (this being line 53)    vComp.push_back(vItems);
  55.  
  56.         vItems.push_back(type);
  57.         vItems.push_back(gph);
  58.         vItems.push_back(cost);
  59.         vItems.push_back(required);
  60.  
  61.         i++;
  62.  
  63.     }
  64.     input.close();
  65.     return true;
  66. }
Thank you for any and all help.
Mar 11 '08 #1
8 1866
Studlyami
464 Recognized Expert Contributor
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.
Expand|Select|Wrap|Line Numbers
  1. vector<IrrigationComponent> vComp;
  2. vector < vector<IrrigationComponent> > vItems;
  3.  
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
Expand|Select|Wrap|Line Numbers
  1.  vector< vector < vector <IrrigationComponent> > > vComp; 
  2.  
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.
Mar 11 '08 #2
ejack
12 New Member
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.

Expand|Select|Wrap|Line Numbers
  1. #include <string>
  2. using namespace std;
  3.  
  4.  
  5. class IrrigationComponent
  6. {
  7. private:
  8.     string type, name;
  9.     int required;
  10.     float gallons, gph, cost, totalCost;
  11.     void CalcCost();
  12.     void CalcGPH();
  13.  
  14. public:
  15.     IrrigationComponent();
  16.  
  17.     void SetName(string n);
  18.     void SetType(string t);
  19.     void SetRequired(int r);
  20.     void SetGallons(float g);
  21.     void SetCost(float c);
  22.  
  23.     float GetUnitCost();
  24.     float GetTotalCost();
  25.     float GetTotalGPH();
  26.     int GetNumber();
  27.     string GetName();
  28.     string GetType();
  29. };
Mar 12 '08 #3
Studlyami
464 Recognized Expert Contributor
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.
Mar 12 '08 #4
ejack
12 New Member
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:

Expand|Select|Wrap|Line Numbers
  1. class IrrigationEstimator
  2. {
  3. private:
  4.     vector<IrrigationComponent> vComp;
  5.     string companyName, busName;
  6. public:
  7.     IrrigationEstimator();
  8.     bool ReadData();
  9.     string WriteInvoice();
  10. };
  11.  

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

Expand|Select|Wrap|Line Numbers
  1.         vector<IrrigationComponent> component;
  2.  
  3.         vComp.push_back(component);
  4.  
I get the error:

Expand|Select|Wrap|Line Numbers
  1.  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.
Mar 12 '08 #5
fual
28 New Member
what you want to do to begin with is probably add some typedefs, this makes things nicer.

Expand|Select|Wrap|Line Numbers
  1. class irrigation_estimator
  2. {
  3. public:
  4.     typedef irrigation_estimator type;
  5.     typedef irrigation_component component_type;
  6.     typedef std::vector<component_type> component_vector_type;
  7. private:
  8.     component_vector_type componentVector_;
  9.     std::string companyName_;
  10.     std::string businessName_;
  11. public:
  12.     irrigation_estimator( );
  13.     bool read_data( void );
  14.     std::string write_invoice( void );
  15. };
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
Expand|Select|Wrap|Line Numbers
  1. class irrigation_estimator
  2. {
  3. public:
  4.     typedef irrigation_estimator type;
  5.     typedef irrigation_component component_type;
  6.     typedef std::vector<component_type> component_vector_type;
  7. private:
  8.     component_vector_type componentVector_;
  9.     std::string companyName_;
  10.     std::string businessName_;
  11. public:
  12.     irrigation_estimator( );
  13.     bool read_data( void );
  14.     std::string write_invoice( void );
  15.     void add_component( const component_type& component ) {
  16.         componentVector_.push_back( component );
  17.     }
  18. };
Mar 12 '08 #6
ejack
12 New Member
Studlyami:

Thank you for your help.
Mar 12 '08 #7
Studlyami
464 Recognized Expert Contributor
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.

Expand|Select|Wrap|Line Numbers
  1. class IrrigationEstimator
  2. {
  3. private:
  4.     vector<IrrigationComponent> vComp;
  5.  . . . 
  6. }
  7.  
  8. //where you create a new Component
  9. IrrigationComponent MyNewComponent;
  10. MyNewComponent.name = "Component Name";
  11. MyNewComponent.type = "Type";
  12. MyNewComponent.cost = 234; //would call your calc cost function.
  13.  
  14. vComp.pushback(MyNewComponent); //add the object we just created to our vector.
  15.  
  16. /*in your estimator class when you want to calc the total cost or create the invoice would go through your vector like so*/
  17. int iTotalCost = 0
  18. for(int i = 0; i<(int)vComp.size() ; i++)
  19. {
  20.    iTotalCost += vComp[i].cost; //keep a running total of the cost for each component.
  21. }
  22.  
If you see any problems or don't understand something please post up any questions.
Mar 12 '08 #8
ejack
12 New Member
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.
Mar 13 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

9
3209
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII "stereolithography" file (*.STL) into my program. This has the following syntax... Begin STL Snippet **********
12
2348
by: BCC | last post by:
If I create a vector of vectors of double: std::vector< std::vector<double> > table1; Are my vectors of doubles uninitialized? Do I have to loop through table1 and initialize each vector of doubles using new? And in cleaning up, manually delete each of these vectors of doubles? Thanks,
9
5945
by: Nancy Keuss | last post by:
Hi, I've created a vector of vectors of ints, and I want to pass it as a parameter to a function. Is this possible, and if so, then what is the syntax like for the function header and function prototype when I need to specify the variable type? Thank you, N.
1
2276
by: Dennis | last post by:
Hi I'm trying to implement a vector of vectors where find can be used to find a vector<double> in the vectors of vectors, that is hard to understand i guess. What I mean is that I got a vector foo containing vectors of the size 3. I then want to compare a vector<double> of size 3 (coord) with foo to find a sequence of elements in foo that equals coord. However, when I try this it returns when just one of the element of coord equals one...
34
4177
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and "push_back" a copy of this into a vector V. This is repeated many times in an iterative process. Ok whenever I "push_back" a copy of Class A, I also want to assign a pointer contained in an exisiting instance of a Class B to this
5
10580
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes the vector with the values I need (I know these values before hand). - What's the best way to initialize the vector<vector<int> >? Can I initilize it by enumerating its values? - If I do: v = new vector<vector<int> >(3) for example, is it
10
8480
by: mahurshi | last post by:
I've got a gate structure that looks like this /* Defining sGATE structure */ struct sGATE { string name; vector<int> input; int output; };
8
10953
by: cayblood | last post by:
Hello, I have been interested in something kind of like the next_permutation from the STL algorithm library, except that I want it to find possible combinations of vector elements. Here is a more detailed example of what I want: Given a vector containing an arbitrary number of vectors, each of which contains an arbitrary number of elements, generate a new vector in which each element consists of one element taken from its corresponding...
0
2144
by: acosgaya | last post by:
hi, I am working in this problem, where I have a set of N d-dimensional points, e.g. (4,5,6,8) (2,0,4,6), are 4-d points, which I have stored in a vector of vectors. I am trying to partition the vector of vectors according to the median value of one the dimensions. I tried to use the stl partition method like this: // S is a vector of vectors containing the points vector < vector <int> >::iterator iter;
5
18253
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " << v1.size( ) << endl; v1.clear( ); //clears the vector I have a few questions:
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5445
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.