473,503 Members | 756 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

create a vector of a class

14 New Member
I'm working on a program that reads in from a file a list of customers and puts them in a vector. I did that. Next, there is a file for each customer that I put in this vector. I need to open the file, and "assign the data to a new instance of the class in the vector." I don't really know what this means. I have a class of stocks, where a stock consists of three arguements, a symbol, amount of stock and price. My class looks like this:
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. class stock {
  7. private:
  8.  
  9. string symbol;
  10. int numshare;
  11. float price;
  12.  
  13. public:
  14.     stock(string symbol1, int numshare1, float price1);
  15.     stock();
  16. };
There are other things in the class but I believe this is all that is important for my problem.
The files I am reading in look something like this:
CSCO 100 12.34
MSFT 200 56.78
where the numbers are the symbol, followed by numshare, follewed by price.
This is what I have been trying, and it hasn't been working:
Expand|Select|Wrap|Line Numbers
  1. for(int j = 0; j<namevector.size(); j++){
  2.     otherfiles.open(namevector[j].c_str());
  3.     while(!otherfiles.eof()){
  4.         otherfiles >> symbol >> numshare >> price;
  5.         if (!otherfiles.eof()){
  6.             stockvector.push_back(symbol, numshare, price);
  7.         }
  8.     }
  9.  
  10.     otherfiles.close();
  11.     otherfiles.clear();
  12.     }
where namevector is my vector of customernames, they are the names of the files I have to open.
I'm really sorry, I know this probably isn't very clear, but it's the best job of explaining i can do.
Nov 19 '07 #1
5 1631
Laharl
849 Recognized Expert Contributor
Your problem is that you're not calling the constructor, you're calling push_back with three parameters. What you need to do is declare a stock item and then push_back that.
Nov 19 '07 #2
Sarahger9
14 New Member
would it possibly look something like this:
Expand|Select|Wrap|Line Numbers
  1.     for(int j = 0; j<namevector.size(); j++){
  2.     otherfiles.open(namevector[j].c_str());
  3.     while(!otherfiles.eof()){
  4.         otherfiles >> symbol >> numshare >> price;
  5.         customerstock.setsymbol(symbol);
  6.         customerstock.setnumshare(numshare);
  7.         customerstock.setprice(price);
  8.         if (!otherfiles.eof()){
  9.             stockvector.push_back(customerstock);
  10.         }
  11.     }
if I had these functions defined in the class
Expand|Select|Wrap|Line Numbers
  1. void stock::setsymbol(string newsymbol){ 
  2.         symbol = newsymbol; 
  3.     }
  4. void stock::setnumshare(int newnumshare){ 
  5.         numshare=newnumshare;
  6.     }
  7. void stock::setprice(float newprice){ 
  8.         price=newprice;
  9.     }
I'm sorry, I am having a problem with vector and classes and can't find many helpful resources, so I appreciate your patience with me.
Nov 19 '07 #3
Studlyami
464 Recognized Expert Contributor
First of all be sure you include the proper items. You need to add
#include <vector>

The vector is a container of a given type so you can't pass 3 parameters into it and expect the container to convert them to your structure. The vector should be like so: vector<type> variable_name;
Expand|Select|Wrap|Line Numbers
  1.  vector<int> IntVector;
  2. int MyInt = 5;
  3. IntVector.push_back(MyInt);
  4.  
Now you can do the same thing with an object of a structure or a class

Expand|Select|Wrap|Line Numbers
  1. class ClassA
  2. {
  3.     public:
  4.     int VariableA;
  5.     int VariableB;
  6. };
  7.  
  8. //in main
  9. vector <ClassA> ClassVector;
  10. ClassA  MyObject;
  11. MyObject.VariableA = 5;
  12. MyObject.VariableB = 6;
  13. ClassVector.push_back(MyObject);
  14.  
So first prepare and set all the items of your object then add them to your vector. In your future post please put up any errors you receive or what isn't working right.
Nov 19 '07 #4
Sarahger9
14 New Member
Thanks for your help. What is MyObject, that is what I am trying to put into the vector, correct?
Nov 19 '07 #5
Ganon11
3,652 Recognized Expert Specialist
Exactly .
Nov 19 '07 #6

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

Similar topics

14
2638
by: LumisROB | last post by:
Is it possible to create matrixes with vector <vector <double >> ? If it is possible which is the element m23 ? You excuse but I am not an expert Thanks ROB
6
2139
by: ken.carlino | last post by:
>From http://www.parashift.com/c++-faq-lite/references.html#faq-8.6, it said "Use references when you can, and pointers when you have to." And I need to convert this java class to c++: public...
23
7366
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
7
3589
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
5
2771
by: Jazi | last post by:
I am newbie to c++ and I would like to build a vector class that returns a vector of objects. I have done some thing like this: student class that contains some of student's properties such as...
9
2176
by: Jazi | last post by:
Hello everyone, I was wondering if someone can help me figuring out some errors I keep getting when I try to create a dll library from existing c++ code using VS2005. The issue is related to a...
7
2145
by: Rob | last post by:
This actually compiles and works but it doesn't seem like the best code, so I was wondering is there another way to do this? template <typename Tvector<T>* addDepth(T) { return new vector<T>;...
19
1500
by: Ramon F Herrera | last post by:
Newbie alert: I come from the C side, struggling to learn C++. I need a two-dimensional data structure. I first tried a regular vector and added the 2nd dimension with my own structs and...
3
1158
by: Alex | last post by:
Hi everyone, I have a question that implies thinking about some design issues with C ++. I have a method like this: class C { .... public:
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7089
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...
0
7282
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,...
0
7463
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...
0
5581
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,...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
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...
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
738
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.