create a vector of a class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sarahger9
    New Member
    • Oct 2007
    • 14

    create a vector of a class

    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:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    class stock {
    private:
    
    string symbol;
    int numshare;
    float price;
    
    public:
    	stock(string symbol1, int numshare1, float price1);
    	stock();
    };
    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:
    Code:
    for(int j = 0; j<namevector.size(); j++){
    	otherfiles.open(namevector[j].c_str());
    	while(!otherfiles.eof()){
    		otherfiles >> symbol >> numshare >> price;
    		if (!otherfiles.eof()){
    			stockvector.push_back(symbol, numshare, price);
    		}
    	}
    
    	otherfiles.close();
    	otherfiles.clear();
    	}
    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.
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    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.

    Comment

    • Sarahger9
      New Member
      • Oct 2007
      • 14

      #3
      would it possibly look something like this:
      Code:
      	for(int j = 0; j<namevector.size(); j++){
      	otherfiles.open(namevector[j].c_str());
      	while(!otherfiles.eof()){
      		otherfiles >> symbol >> numshare >> price;
      		customerstock.setsymbol(symbol);
      		customerstock.setnumshare(numshare);
      		customerstock.setprice(price);
      		if (!otherfiles.eof()){
      			stockvector.push_back(customerstock);
      		}
      	}
      if I had these functions defined in the class
      Code:
      void stock::setsymbol(string newsymbol){ 
      		symbol = newsymbol; 
      	}
      void stock::setnumshare(int newnumshare){ 
      		numshare=newnumshare;
      	}
      void stock::setprice(float newprice){ 
      		price=newprice;
      	}
      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.

      Comment

      • Studlyami
        Recognized Expert Contributor
        • Sep 2007
        • 464

        #4
        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;
        Code:
         vector<int> IntVector;
        int MyInt = 5;
        IntVector.push_back(MyInt);
        Now you can do the same thing with an object of a structure or a class

        Code:
        class ClassA
        {
            public:
            int VariableA;
            int VariableB;
        };
        
        //in main
        vector <ClassA> ClassVector;
        ClassA  MyObject;
        MyObject.VariableA = 5;
        MyObject.VariableB = 6;
        ClassVector.push_back(MyObject);
        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.

        Comment

        • Sarahger9
          New Member
          • Oct 2007
          • 14

          #5
          Thanks for your help. What is MyObject, that is what I am trying to put into the vector, correct?

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Exactly .

            Comment

            Working...