I am trying to learn how to pass pointers to vectors properly, and having a bit of trouble. I am using Dev-C++ IDE
here is the GIRLFRIEND class

#ifndef GIRLFRIEND_H
#define GIRLFRIEND_H

#include "player.h" // inheriting class's header file

/*
 * No description
 */
class GIRLFRIEND : public PLAYER
{
    private:
            bool hasheadache;
            bool hascut;
            string eyecolor;
            string bestfriend;
            
            vector<string> namelist;
            vector<string> eyeslist;
            vector<string> bestflist;
            vector<string> likelist;
            vector<string> dislikelist;
            vector<string> likedislikelist;
            
	public:
		// class constructor
		GIRLFRIEND();
		// class destructor
		~GIRLFRIEND();
		bool GetHasHeadache();
		bool GetHasCut();
		string GetEyeColor();
		string GetBestFriend();
		vector<string>* GetNameList();
		vector<string>* GetEyeList();
		vector<string>* GetBestfList();
		vector<string>* GetLikeList();
		vector<string>* GetDislikeList();
		vector<string>* GetLikeDislikeList();
		
		void SetHasHeadache(bool yesno);
		void SetHasCut(bool yesno);
		void SetEyeColor(string color);
		void SetBestFriend(string bfriend);
		void SetLikeList(vector<string>* likelist, vector<string>* gflikelist, vector<string>* gfdislikelist);
};

#endif // GIRLFRIEND_H
#include "Library.h"
#include "girlfriend.h" // class's header file

// class constructor
GIRLFRIEND::GIRLFRIEND()
{
	namelist.push_back("Stacy");
	namelist.push_back("Barbara");
	namelist.push_back("Jennifer");
	namelist.push_back("Nichole");
	namelist.push_back("Carol");
	sort(namelist.begin(), namelist.end());
	
	eyeslist.push_back("Blue");
	eyeslist.push_back("Brown");
	eyeslist.push_back("Green");
	eyeslist.push_back("Hazel");
	eyeslist.push_back("Baby Blue");
	sort(eyeslist.begin(), eyeslist.end());
	
	bestflist.push_back("Charlene");
	bestflist.push_back("Cynthia");
	bestflist.push_back("Debby");
	bestflist.push_back("Elizabeth");
	bestflist.push_back("Dorothy");
	sort(bestflist.begin(), bestflist.end());
	
    likedislikelist.push_back("cats");
    likedislikelist.push_back("dogs");
    likedislikelist.push_back("chocolate");
    likedislikelist.push_back("ice Cream");
    likedislikelist.push_back("children");
    likedislikelist.push_back("dancing");
    likedislikelist.push_back("talking on the phone");
    likedislikelist.push_back("surfing");
    likedislikelist.push_back("horseback riding");
    likedislikelist.push_back("skiing");
    sort(likedislikelist.begin(), likedislikelist.end());
    
	int num = 0;
	
    //Set her name
	num = rand() % 5;
	SetName(namelist[num]);

    //Set her eye color
    num = rand() % 5;
	SetEyeColor(eyeslist[num]);

    //Set her b/f's name
    num = rand() % 5;
	SetBestFriend(bestflist[num]);

    
    
    //set her likes & dislikes
    SetLikeList(&likedislikelist, &likelist, &dislikelist);
    
	SetGunScore(0);
	SetIntelligence((rand() & 50) + 25);
	SetCharm((rand() & 50) + 25);
	SetMoney(0);
	SetEnergy(0);
	SetTwoHanded(false);
}

// class destructor
GIRLFRIEND::~GIRLFRIEND()
{
	// insert your code here
}
bool GIRLFRIEND::GetHasHeadache(){
     return hasheadache;
     }
bool GIRLFRIEND::GetHasCut(){
     return hascut;
     }
string GIRLFRIEND::GetEyeColor(){
       return eyecolor;
       }
string GIRLFRIEND::GetBestFriend(){
       return bestfriend;
}

vector<string>* GIRLFRIEND::GetNameList(){
    return &namelist;
}
vector<string>* GIRLFRIEND::GetEyeList(){
    return &eyeslist;
}
vector<string>* GIRLFRIEND::GetBestfList(){
    return &bestflist;
}
vector<string>* GIRLFRIEND::GetLikeList(){
    return &likelist;
}
vector<string>* GIRLFRIEND::GetDislikeList(){
    return &dislikelist;
}
vector<string>* GIRLFRIEND::GetLikeDislikeList(){
    return &likedislikelist;
}


void GIRLFRIEND::SetHasHeadache(bool yesno){
     hasheadache = yesno;
     }
void GIRLFRIEND::SetHasCut(bool yesno){
     hascut = yesno;
     }
void GIRLFRIEND::SetEyeColor(string color){
     eyecolor = color;
     }
void GIRLFRIEND::SetBestFriend(string bfriend){
     bestfriend = bfriend;
     }
void GIRLFRIEND::SetLikeList(vector<string>* likelist, vector<string>* gflikelist, vector<string>* gfdislikelist){
     random_shuffle(likelist->begin(), likelist->end());
     vector<string>::iterator likeIT;
     vector<string>::iterator gflikeIT;
     vector<string>::iterator gfdislikeIT;
     for(likeIT = likelist->begin(), gflikeIT = gflikelist->begin(), gfdislikeIT = gfdislikelist->begin(); likeIT != likelist->begin() + 5; ++likeIT, ++gflikeIT, ++gfdislikeIT){
         *gflikeIT = *likeIT;
         *gfdislikeIT = *(likeIT + 5);
     }
}

Now the code compiles without any errors. but when I run the simulation it freezes up and tries to contact microsoft on an unknown error. The function I believe that is causing the problem is the function relating to setting the GIRLFRIEND's likes and dislikes. (because that is the last function I was working on before it all went to crap.

void GIRLFRIEND::SetLikeList(vector<string>* likelist, vector<string>* gflikelist, vector<string>* gfdislikelist){
     random_shuffle(likelist->begin(), likelist->end());
     vector<string>::iterator likeIT;
     vector<string>::iterator gflikeIT;
     vector<string>::iterator gfdislikeIT;
     for(likeIT = likelist->begin(), gflikeIT = gflikelist->begin(), gfdislikeIT = gfdislikelist->begin(); likeIT != likelist->begin() + 5; ++likeIT, ++gflikeIT, ++gfdislikeIT){
         *gflikeIT = *likeIT; //NOTICE: we didn't push any in prior to this call
         *gfdislikeIT = *(likeIT + 5);//NOTICE: nor did we push any prior to this call
     }
}

What I am trying to do is take 3 lists.
1. The mainlist with all of the total likes and dislikes combined
2. the g/f's like list
3. the g/f's dislike list
Shuffle the mainlist and set the first 5 elements to the g/f's like list and the last 5 elements to the g/f's dislike list.

Could someone help me out on what I am doing wrong?

Dani AI

Generated

Good catch, — crashes like that are usually caused by out-of-range access or iterator invalidation. Here are safer, idiomatic patterns to avoid similar problems and make the code easier to reason about and test.

Pass vectors by reference (const when read-only) instead of raw pointers, make a local copy of the source to shuffle, clear outputs before inserting, and use std::shuffle with a proper engine. Doing that avoids modifying a container while iterating it, prevents accidental writes past the end, and keeps ownership/lifetime obvious.

void setLikeDislike(const std::vector<std::string>& pool,
                    std::vector<std::string>& likes,
                    std::vector<std::string>& dislikes)
{
    if (pool.size() < 10) return; // validate input
    auto temp = pool; // work on a copy
    std::shuffle(temp.begin(), temp.end(), std::mt19937{std::random_device{}()});
    likes.clear(); dislikes.clear();
    likes.insert(likes.end(), temp.begin(), temp.begin() + 5);
    dislikes.insert(dislikes.end(), temp.begin() + 5, temp.begin() + 10);
}

Extra pointers: check container sizes before indexing, use reserve if you know final size to avoid reallocations, prefer returning const std::vector<T>& from getters instead of raw pointers (or return a copy when ownership must be isolated), and run under a debugger or AddressSanitizer to catch invalid memory access early. See , std::random_device / mt19937, and vector::insert / reserve for details.

NVM I figured it out on me own. none of the likes/dislikes of the girlfriend were "push_back()" they were assignments it caused an error

sorry for taking up space...The actual method (that works now)is:

void GIRLFRIEND::SetLikeList(vector<string>* likelist, vector<string>* gflikelist, vector<string>* gfdislikelist){
     random_shuffle(likelist->begin(), likelist->end());
     vector<string>::iterator likeIT;
     vector<string>::iterator gflikeIT;
     vector<string>::iterator gfdislikeIT;
     for(likeIT = likelist->begin(), gflikeIT = gflikelist->begin(), gfdislikeIT = gfdislikelist->begin(); likeIT != likelist->begin() + 5; ++likeIT, ++gflikeIT, ++gfdislikeIT){
         gflikelist->push_back(*likeIT); //<-HERE we push then in
         gfdislikelist->push_back(*(likeIT + 5)); //<- and HERE we push them in
     }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.