Create an object inside a memberfunction

Hello everyone.

Iam trying to make this really simple(atleast thats what I thought) program to put books in a vector. Then also add some more stuff but first things first.

If I do it in main, it works(See the multicomment at the bottom)

But I wanna do it via a memberfunction from the Library class. I dont get any errors in the IDE but when I run the code I get three errors from line 12

Errors :

Error C3861 'b2': identifier not found

Error C2065 'Book': undeclared identifier

Error C2146 syntax error: missing ';' before identifier 'b2'

I have tried so many things to get this to work but I cant get around it, feels like Im missing some simple detail, or Im totally out swimming on the whole program.

Thoughts?

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "pch.h"
#include <iostream>
#include <vector>
#include <string>

class Library
{
public:
	void createBook(std::string title, int isbn, int count)
	{
		//Error on line 12 when debugging
		Book b2(title, isbn, count);
	}
};

class Book
{
private:
	std::string title;
	int ISBN;
	int count;

public:
	Book(std::string Title, int iSBN, int Count)
	{
		title = Title;
		ISBN = iSBN;
		count = Count;
	}
};
int main()
{
	Library library;
	std::vector<Book>allBooks;

	std::string title;
	int ISBN;
	int count;

	std::cout << "Enter titel : ";
        std::cin.ignore();
	getline(std::cin, title);
	std::cout << "Enter ISBN : ";
	std::cin >> ISBN;
 	std::cout << "Enter total : ";
	std::cin >> count;

 	//This works and puts the book in the vector 'allBooks'
	/* 
	Book b1(title, ISBN, count);
 	allBooks.push_back(b1);
	*/
	
	library.createBook(title, ISBN, count);
}
Last edited on
Library does not know what "Book" is, define book above Library.
lord have mercy..I feel stupid, ofcourse..

Thanks alot @poteto

I would really appreciate some feedback on my code when Im done. Im spending many hours a day learning c++ on my own and dont wanna get bad habits.

Posting when Im done!
Last edited on
Topic archived. No new replies allowed.