Thread: vector problems

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    139

    Question vector problems

    I seem to be having problems getting this vector working (probably because I don't particular understand it perhaps?) I have a vector of an array of classes
    Code:
    typedef vector<CNamePair*> MYVECTOR;
    along with its corresponding iterator
    Code:
    typedef vector<CNamePair*>::iterator MYVECTORITER;
    after creating an instance of the object i run the following code in my class function add(string strName, string strValue). Not going to be the completed code but was just trying out the push functions.

    "list" is the class' private variable of type MYVECTOR.

    Code:
        bool CNamePairList::add(string strName, string strValue)
        {
          MYVECTORITER the_iterator;
          CNamePair temp2(strName,strValue);
    
          list.push_back(&temp2);
    
          for (the_iterator=list.begin(); the_iterator != list.end() ; the_iterator++)
            cout << (*the_iterator)->toString() <<'\n';
        }
    The size of the vector when it starts is 0. When I call the add function the first time it is fine and prints off the value okay, but when I do my second call to it to add another value to the list it overwrites the first value with the new value so that both positions in the vector now contain both of the same values.

    Is it something I am doing wrong?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    First, you are using a pointer to a local object...now suppose for arguments sake that pointer was 8 bytes under the stack's base pointer.......the next time you call the function, that object is recreated and (suprise suprise) it is created in the same stack position; base - 4 ...therefore both pointers point to the same object (this isnt a certain.. but I bet this is your problem)

    Secondly, you are using a vector of pointers........now this is legal, but unnessasary in most situations and can be a pain in the butt........You should try push a whole CNamePair onto the vector - then its state will be saved and it will work better

    typedef vector<CNamePair> MYVECTOR;

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Few ideas

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <utility>//for std::pair
    using namespace std;
    
    
    class foobar{
    	typedef vector<pair<string, string> > MYVECTOR;
    	typedef MYVECTOR::iterator MYVECTORITER;
    	
    	MYVECTOR vec;
    public:
    	
    	void AddPair(const string& lhs,const string& rhs){
    		
    		MYVECTORITER the_iterator;
          	pair<string,string> temp(lhs,rhs);
    
          	vec.push_back(temp);
    
          	for (the_iterator = vec.begin();
          		 the_iterator != vec.end();
          		 ++the_iterator){
    	        cout << (*the_iterator).first << ' ';
    	        cout << (*the_iterator).second << '\n';
    	    }
        }
    	
    	
    	
    };
    
    int main(){
    
    	foobar f;
    	
    	f.AddPair("Hello","World");
    	
    	cout << endl;
    	
    	f.AddPair("Eat at","Joes");
    	
    	cout << endl;
    	
    	f.AddPair("foo","bar");
    	
    	
    }

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    The program is like this: (for my benifet)

    Code:
    class CNamePairList
      private:
        MYVECTOR list
      public:
        add(string,string)
    list is a vector which contains the class CNamePair

    Code:
    class CNamePair
      private:
        string strName
        string strValue
      public:
        string toString();
    So are you saying instead of creating a local variable in CNamePairList::add() for my iterator I should put it in the private section of the class? Which would stop that error of overwriting to occur?

    Yes the vector of pointers ... but the requirements for this program says I must use those typedefs. *sigh*
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by lostminds
    So are you saying instead of creating a local variable in CNamePairList::add() for my iterator I should put it in the private section of the class? Which would stop that error of overwriting to occur?

    Yes the vector of pointers ... but the requirements for this program says I must use those typedefs. *sigh*
    If you are stuck with that typedef, then you must work around it....but creating that object on the stack (as you are when you CNamePair temp2(strName,strValue);) and then storing a pointer to that object for later use is a surefire way to get into trouble......

    You could create the objects on the heap...but then you must explicitly destroy them when your class dies...like so

    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <utility>//for std::pair
    using namespace std;
    
    
    class foobar{
    	typedef vector<pair<string, string>* > MYVECTOR;
    	typedef MYVECTOR::iterator MYVECTORITER;
    	
    	MYVECTOR vec;
    public:
    	
    	void AddPair(const string& lhs,const string& rhs){
    		
    		MYVECTORITER the_iterator;
          	pair<string,string>* temp = new pair<string,string>(lhs,rhs);
    
          	vec.push_back(temp);
    
          	for (the_iterator = vec.begin();
          		 the_iterator != vec.end();
          		 ++the_iterator){
    	        cout << (*the_iterator)->first << ' ';
    	        cout << (*the_iterator)->second << '\n';
    	    }
        }
        
    ~foobar(){//destructor
        	
    		for(int i = 0;i < vec.size();++i)
        	delete vec[i];//delete each
        
    
    	}	
    	
    	
    };
    
    int main(){
    
    	foobar f;
    	
    	f.AddPair("Hello","World");
    	
    	cout << endl;
    	
    	f.AddPair("Eat at","Joes");
    	
    	cout << endl;
    	
    	f.AddPair("foo","bar");
    	
    	
    }

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    Thank you so much I got it working now (with your help). cheers mate

    ps forgot about new and delete, guess i won't forget them again
    "The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."

Popular pages Recent additions subscribe to a feed