Click here to Skip to main content
15,885,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am writing a program that allows users to input a music album and store them into vectors. Then the program asks the user whether to borrow it. To do that I tried using .erase of the given vector. However the vector function is defined in a constructor of a class and the modification is done later with a different function but within the same class. Any help would be much appreciated, thank you.

What I have tried:

C++
class Album :public Entry {
private:
        vector <string> vec;
	string singer, label;
	string combine; //to combine artist and record label
	int choice;
public:
	Album();
	void Borrow1() {};
};

Album::Album() {
	do
	{
		cout << "Enter the singer(type end to finish)" << endl;
		getline(cin, singer);
		if (singer == "end")
			break;
		cout << "Enter the label: " << endl;
		getline(cin, label);
		combine = singer + " ,by, " + label;
		cout << "The singer and label is: " << combine << endl;
		vec.push_back(combine);
	} while (true);

	cout << "you entered:  " << endl;
	for (int i = 0; i < vec.size(); i++) {
		cout << "Number " << i << ". Item is: " << vec[i] << endl;
	}
}

void Album::Borrow1() { 
	cout << "Which item is borrowed? Enter the number." << endl;
	cin >> choice;
	vec.erase(vec.begin() + choice);
	cout << "Done" << endl;
	cout << "The followings still remain:  " << endl;
	for (int i = 0; i < vec.size(); i++) {
		cout << "# : " << i << ". Album is: " << vec[i] << endl;
	}
}
Posted
Updated 9-May-18 11:02am
v4

1 solution

You have not explaine the problem. Although I suspect that your class declaration entry for Borrow1 is wrong. You have the following in your class:
C++
public:
Album();
void Borrow1() {}; // Borrow1 is an empty function.
};

And you later define Album::Borrow1() with a proper body. In my test this code will not even compile.
 
Share this answer
 
Comments
CPallini 9-May-18 17:06pm    
Indeed. 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900