Help with Vector Objects

Hello!

So I am working on creating vector objects, and I am getting an error when trying to run:
In function 'void populateVectors(std::vector<Library, std::allocator<Library> >&, std::vector<Members, std::allocator<Members> >&)':
assign6.cpp:82: error: 'getBook' was not declared in this scope

Ive attached the codes that pertain to this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Library
{
	private:
		string book, author, ISBN;
		float price;
		bool available;
		int memberNum;
	public:
		void getBook (ifstream);
		void getAuthor(ifstream);
		void getISBN (ifstream);
		void getPrice (ifstream);
		void getAvailable (ifstream);
		void getNum (ifstream);
		string showBook () const;
};

void Library::getBook (ifstream fin)
{
	fin >> book;
}

string Library::showBook () const
{
	return book;
}

Ignore the vector for members, its another class im using that I havent coded for yet.
1
2
3
4
5
6
7
8
9
10
void populateVectors(vector<Library>&, vector <Members>&);

int main()
{

	vector<Library> library;
	vector<Members> members;
	populateVectors(library, members);
	return 0;


And the function that calls this is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void populateVectors(vector<Library> &library, vector <Members> &members)
{
	ifstream fin;
	string filename;
	cout << "\n\n. Hello. Please enter the filename (with .txt extension) for the library: ";
	getline(cin, filename);
	fin.open(filename.c_str());
	while(fin.fail())
	{
		cout << "\nBad Filename. Please Re-Enter Filename: ";
		getline(cin, filename);
		fin.open(filename.c_str());
	}
	
	library.push_back(getBook(fin));
	cout << "\nBook name is " << library[0].showBook() << endl;

	fin.close();
}


Am I using pushback correctly with vector objects? I have no idea why it isnt recognizing getBook as a member of the class Library. I have tried searching around the forums for similar problems, but have no encountered any. Thank you!
I've also tried changing the code to

library.push_back(library.getBook(fin));

but it hasn't worked either :\

Also, ideally, I would like to be able to populate the whole class on the same vector. Then push back to a new object, and populate the whole thing before pushing back again. How would I approach that? Would I keep using push_back?

From what im imagining, if i created another constructor(function in object?)
1
2
3
4
void Library::getAuthor (ifstream fin)
{
	fin >> author;
}

but wrote it as this in the populateVectors function:
1
2
library.push_back(getBook(fin)); //after this line in the function
library.push_back(getAuthor(fin)); 

would that just set author in the new vector[1] ?

To clarify, would it be allocated like this:
library[0].showBook()
library[1].showAuthor()
??

Because I am trying to make it so it is like this:
library[0].showBook()
library[0].showAuthor()

Sorry if its hard to understand what Im trying to say. I basically just want to have the 1st object filled fully instead of just filling 1 member of an object, and then pushing to another one.
Last edited on
I think what you want is to replace line 15:
library.push_back(getBook(fin));

with:
1
2
3
Library newBook;
newBook.getBook(fin);
library.push_back(newBook);
Thanks, but now I just got a load of errors. Should I copy and paste it all here?
A second option is to change:
1
2
3
4
5
6
7
8
9
class Library 
{
    void getBook(ifstream);
    //...
};
void Library::getBook (ifstream fin)
{
	fin >> book;
}


to
1
2
3
4
5
6
7
8
9
10
11
class Library 
{
    friend Library getBook(ifstream);
    //...
};
Library getBook (ifstream fin)
{
	Library newLibrary;
	fin >> newLibrary.book;
	return newLibrary;
}


That being said, I think there's a general problem with your design.

To add a book, we have to create another library. Try using 2 classes, "library" and "book". The library class will have options to add books and will store the books in a vector.. The book will have options to add titles, and will store the info about each book. That will make things more intuitive for you. A vector of books makes more sense than a vector of libraries.
Last edited on
What function does the keyword friend play?
And if I just use library to store the books, would it be better if i just used library a s a struct?

EDIT: nvm about the above statement. Would Books be a vector then? I feel like we can just constantly replace books with the next books being read by the file, after storing it into library
Last edited on
Topic archived. No new replies allowed.