Thread: vector of pointers

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    230

    vector of pointers

    ive got a struct here which i then create a vector of pointers of type stuct people. in my problem i read in 2 files. i have comlpeted all of file 1 work. file 2 i have to read in 2 sets of names the first being the "child" and the second the "parent"... i have to link the parent to the child and then display the information. i think ive gotten where to do it im just getting an error.

    here is the code..
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    #include <fstream>
    using namespace std;
    
    struct Date{
          int month;
          int day;
          int year;
    };
    
    struct Person{
          string fn;
          string ln;
          Person* parent;
          Date bday;
    };
    
    //open file of names and bdays
    void openFile(ifstream& ifs);
    
    //function to fill vector with names and birthdays
    void processNames(ifstream& ifs, vector<Date*>& dat,
                     vector<Person*>& per);
    
    //display people from first file
    void displayNames(const vector<Date*>& dat, const vector<Person*>& per);
    
    void procParents(vector<Person*>& per, ifstream& ifs2);
    
    //process the child and parent info
    void displayParents(ifstream& ifs2, vector<Person*>& per,
    				vector<Date*>& dat);
    
    int main()
    {
       ifstream ifs1, ifs2; //streams for opening files
       openFile(ifs1); //open file
    
       vector<Date*> dat; // vector of pointers to struct date
       vector<Person*> per; //vector of pointers to struct Person
    
       processNames(ifs1, dat, per); //call function to fill vector
       displayNames(dat, per); //call function to display vectors
    
       openFile(ifs2); //open the second file
       procParents(per, ifs2);
       //Parents(per);
       displayParents(ifs2, per, dat); //update parent and child vector 
    
       return 0;
    
    }
    
    void openFile(ifstream& ifs) // open file
    {
        string fileName; //name of file given by user
        cout << "Enter file name: ";
        cin >> fileName;
        cout << endl;
        ifs.open(fileName.c_str()); //open the file
        if(!ifs) // check for failure
        {
                     cerr << "Error opening " << fileName << endl;
                     exit(1); // exit if file opening failed
        }
    
    }
    
    //process all the names and information into the 2 vectors
    void processNames(ifstream& ifs, vector<Date*>& dat,
                     vector<Person*>& per)
    {
           //temporary month day and year
           int m, d, y;
           //temporary first and last name
           string fName, lName;
    
           //loop while we have information in the file
           while (ifs >> fName >> lName >> m >> d >> y)
           {
                 Person* p = new Person; // point to a new struct person
                 per.push_back(p); //push back to accept new information
    
                 //pointer now holds new first name
                 p->fn = fName;
                 //new last name
                 p->ln = lName;
    			//initially there is no link to parents
    			 p->parent = NULL;
    
                 Date* da = new Date; //point to a new stuct date
                 dat.push_back(da); //push back to accept new information
    
                 //pointer now holds new month
                 da->month = m;
                 //new day
                 da->day = d;
                 //new year
                 da->year = y;
           }
    }
    
    void displayNames(const vector<Date*>& dat, const vector<Person*>& per)
    {
        //loop through vector. sizes are equal so size() of
        //either vector will work
        for(size_t i = 0; i < per.size(); ++i)
        {
                   //output all the information
                   cout << "Name: " << per[i]->ln << ", "
                        << per[i]->fn << "; DOB: " << dat[i]->month
                        << "/" << dat[i]->day << "/" <<dat[i]->year;
                   cout << endl;
        }
        cout << "=========================================="<<endl;
    }
    
    void procParents(vector<Person*>& per, ifstream& ifs2)
    {
    	string fn1, fn2; //first names from file
    	string ln1, ln2; //last names from file
    	while(ifs2 >> fn1 >> ln1 >> fn2 >> ln2)
    	{
    		for(size_t z=0; z < per.size(); ++z)
    		{
    			for(size_t y=0; y < per.size(); ++y)//we found the child
    			{
    				if(fn1 == per[z]->fn && fn2 == per[y]->fn)
    				{
    					per[z]->parent = &per[z];
    				}
    			}
    		}
    	}}
    
    
    
    //output final people w/ their parents
    //NOT DONE!!!!
    void displayParents(ifstream& ifs2, vector<Person*>& per,
    				vector<Date*>& dat)
    {
    	    for(size_t a = 0; a < per.size(); ++a)
    		{
                   //output all the information
                   cout << "Name: " << per[a]->ln << ", "
                        << per[a]->fn << "; DOB: " << dat[a]->month
                        << "/" << dat[a]->day << "/" <<dat[a]->year
    					<< "; parent: Name: ";
    			   cout << endl;
    		}
    }
    the red is where i should b doing this...
    i cant figure out how to link the child to the parent properly. i get this error on my laptop
    Code:
    c:\Documents and Settings\PolyUser\Desktop\CS1124\HW02\hw02\hw02\hw02.cpp(131): error C2440: '=' : cannot convert from 'std::allocator::value_type *__w64  ' to 'Person *'
            with
            [
                _Ty=Person *
            ]
    but on my pc i get this error same exact file
    Code:
    d:\Poly\CS1124\hw02\hw02\hw02.cpp(132): error C2440: '=' : cannot convert from 'Person ** ' to 'Person *'
    Basically i just need help in linking the two together.
    here is a link for the proper output that i should have
    http://cis.poly.edu/cs1124/Homework/hw02/output.txt
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Make it:

    Code:
    per[z]->parent = per[z];
    per[z] is a Person* (a Person pointer), which is what the Person.parent is expecting. If you give it the address of this element in the vector, than the Person.parent will point to a pointer which points to data, which I don't think is what you wanted

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    ok so ive put this in
    Code:
    per[z]->parent = per[y];

    now how could i display that in my displayParents function
    Code:
    void displayParents(ifstream& ifs2, vector<Person*>& per,
    				vector<Date*>& dat)
    {
    	    for(size_t a = 0; a < per.size(); ++a)
    		{
                   //output all the information
                   cout << "Name: " << per[a]->ln << ", "
                        << per[a]->fn << "; DOB: " << dat[a]->month
                        << "/" << dat[a]->day << "/" <<dat[a]->year
    					<< "; parent: Name: " << per[a]->parent->fn << ' ' << per[a]->parent->ln;
    			   cout << endl;
    		}
    }
    ok i want to output this and im sure this is how to do it... but when i run it after the first one works fine i get a run time error.. i was thinking of setting parent to null somewhere if it doesnt match but im not sure if thats right.. and if i di dwould i have to check when outputting ? ... lol a little more help thx
    Last edited by gamer4life687; 09-26-2005 at 09:32 PM.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    ahhh i got it working thx anyway
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM