Hey guys, I'm new to the forum and pretty new to C++. I think I'm just about finished with this project, but I've run into a couple problems. Here's my assignment:
"Write class Lib and modify class Book such that this main works as implied. For each class create .h and .cpp files as described in lecture. For class Lib use an array (of type Book) of size 10."
I'll post all my code first, followed by my errors list. Everything I've done to this point in trying to fix the errors only ends up making things worse, so hopefully yall can help me out. Thanks!!
P.S. I apologize in advance for the length of this post, but as I am new to c++ I'm not really sure what pieces of code yall need to be able to see, so I figured I'd just post it all. Sorry
Here's the main.cpp: http://cs.ua.edu/124/Spring2009/Projects/main.cpp
Here's book.h:
#ifndef BOOK
#define BOOK
#include <iostream>
#include <string>
using namespace std;
class Book {
private:
string title, author, genre, pubyear, publisher;
public:
//Default, Copy, and "Title/Author" Constructors
Book ( );
Book (const Book& b);
Book (string t, string a, string g, string py, string p);
//Accessor methods (defined in book.cpp)
string getTitle( );
string getAuthor( );
string getGenre( );
string getPubYear( );
string getPublisher( );
//Mutator methods (defined in book.cpp)
void set(string a, string t, string g, string py, string p);
void setTitle(string t);
void setAuthor(string a);
void setGenre(string g);
void setPubYear(string py);
void setPublisher(string p);
//Operations (defined in book.cpp)
friend class Lib;
friend istream& operator>>(istream& is, Book& b);
friend ostream& operator<<(ostream& os, Book& b);
};
#endif
Here's book.cpp:
#include <iostream>
#include <string>
#include "book.h"
#include "lib.h"
using namespace std;
//Constructor definitions
Book::Book( ) : title("unknown"), author("unknown"), genre("unknown"), pubyear("0000"), publisher("unknown") { }
Book::Book(string a, string t, string g, string py, string p) : author(a), title(t), genre(g), pubyear(py), publisher(p) { }
Book::Book(const Book& b) : title(b.title), author(b.author), genre(b.genre), pubyear(b.pubyear), publisher(b.publisher) { }
//Accessor definitions
string Book::getTitle( ) {return title;}
string Book::getAuthor( ) {return author;}
string Book::getGenre( ) {return genre;}
string Book::getPubYear( ) {return pubyear;}
string Book::getPublisher( ) {return publisher;}
//Mutator definitions
void Book::set(string a, string t, string g, string py, string p) {author=a, title=t, genre=g, pubyear=py, publisher=p;}
void Book::setTitle(string t) {title=t;}
void Book::setAuthor(string a) {author=a;}
void Book::setGenre(string g) {genre=g;}
void Book::setPubYear(string py) {pubyear=py;}
void Book::setPublisher(string p) {publisher=p;}
//Allows user to input a new Book
istream& operator>>(istream& is, Book& b){
string t, a, g, py, p;
cout<< "Enter Title:" << endl;
getline(cin,t);
b.setTitle(t);
cout<< "Enter Author's Name:" << endl;
getline(cin,a);
b.setAuthor(a);
cout<< "Enter Genre:" << endl;
getline(cin,g);
b.setGenre(g);
cout<< "Enter Publisher:" << endl;
getline(cin,p);
b.setPublisher(p);
cout<< "Enter Publication Year (####):" << endl;
getline(cin,py);
b.setPubYear(py);
return is;
}
//Prints the Book information to the screen
ostream& operator<<(ostream& os, Book& b){
cout<< "Title: " << b.title << endl;
cout<< "Author: " << b.author << endl;
cout<< "Genre: " << b.genre << endl;
cout<< "Publisher: " << b.publisher << endl;
cout<< "Publication Year: " << b.pubyear << endl;
return os;
}
Here's lib.h:
#ifndef LIB
#define LIB
#include <iostream>
#include <string>
#include "book.h"
using namespace std;
class Lib {
private:
int count;
Book arr[10];
string title;
public:
//Constructors (defined in lib.cpp)
Lib( );
Lib(string t);
//Accessors (defined in lib.cpp)
string getTitle( );
//Mutators (defined in lib.cpp)
void setTitle(string t);
//Operators (defined in lib.cpp)
friend class Book;
friend ostream& operator<< (ostream& os, Lib& L);
friend void operator+ (Lib L, Book b);
friend void operator- (Lib L, Book b);
int getLibTotal( );
int getCurrentBooks( );
};
#endif
Here's lib.cpp (This is the file all my errors are showing up in. I added comments to the code to explain them):
#include <iostream>
#include <string>
#include <sstream>
#include "lib.h"
#include "book.h"
using namespace std;
//Constructor definitions
Lib::Lib( ) : title("Default Library"), count(0) { }
Lib::Lib(string t) : title(t), count(0) { }
//Accessor definitions
string Lib::getTitle( ) {return title;}
//Mutator definitions
void Lib::setTitle(string t) {title=t;}
//Prints all Books in the Lib to the screen
ostream& operator<< (ostream& os, Lib& L){
for(int i; i<L.count; i++){
//Here's where my first 5 errors are coming from...
//It says that each line "cannot access private member declared in class 'Book'"
//I thought I made each class a friend of the other, but apparently I didn't do it right...
cout<< "Title: " << L.arr[i].title << endl;
cout<< "Author: " << L.arr[i].author << endl;
cout<< "Genre: " << L.arr[i].genre << endl;
cout<< "Publisher: " << L.arr[i].publisher << endl;
cout<< "Publication Year: " << L.arr[i].pubyear << endl << endl;
return os;
}
}
//Adds a Book to the Lib
void operator + (Lib L, Book b){
L.arr[L.count]=b;
++L.count;
}
//Takes a Book out of the Lib
void operator- (Lib L, Book b){
Book temp;
for(int i=0;i<L.count;i++){
//Here's where the rest of my errors are showing up.
//Says that it "could not deduce template argument..."
//I don't really have any idea what the problem is here...
if(L.arr[i]==b){
L.arr[i]=temp;
--L.count;
}
}
}
//Returns the number of Books in the Lib
int Lib::getLibTotal( ) {
return count;
}
//Returns the number of Books in the Lib that have been published within the last two years
int Lib::getCurrentBooks( ) {
int current(0);
int temp;
for(int i=0;i<count;i++){
stringstream ss(arr[i].getPubYear());
ss >> temp;
if((2009-temp)<=2){
++current;
}
}
return current;
}
And here's my errors list:
1>------ Build started: Project: project 2, Configuration: Debug Win32 ------
1>Compiling...
1>book.cpp
1>lib.cpp
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(22) : error C2248: 'Book::title' : cannot access private member declared in class 'Book'
1> c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\book.h(11) : see declaration of 'Book::title'
1> c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\book.h(9) : see declaration of 'Book'
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(23) : error C2248: 'Book::author' : cannot access private member declared in class 'Book'
1> c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\book.h(11) : see declaration of 'Book::author'
1> c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\book.h(9) : see declaration of 'Book'
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(24) : error C2248: 'Book::genre' : cannot access private member declared in class 'Book'
1> c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\book.h(11) : see declaration of 'Book::genre'
1> c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\book.h(9) : see declaration of 'Book'
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(25) : error C2248: 'Book::publisher' : cannot access private member declared in class 'Book'
1> c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\book.h(11) : see declaration of 'Book::publisher'
1> c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\book.h(9) : see declaration of 'Book'
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(26) : error C2248: 'Book::pubyear' : cannot access private member declared in class 'Book'
1> c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\book.h(11) : see declaration of 'Book::pubyear'
1> c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\book.h(9) : see declaration of 'Book'
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(41) : error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'Book'
1> c:\program files\microsoft visual studio 9.0\vc\include\string(90) : see declaration of 'std::operator =='
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(41) : error C2784: 'bool std::operator ==(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'Book'
1> c:\program files\microsoft visual studio 9.0\vc\include\string(80) : see declaration of 'std::operator =='
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(41) : error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'Book'
1> c:\program files\microsoft visual studio 9.0\vc\include\string(70) : see declaration of 'std::operator =='
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(41) : error C2784: 'bool std::operator ==(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)' : could not deduce template argument for 'const std::istreambuf_iterator<_Elem,_Traits> &' from 'Book'
1> c:\program files\microsoft visual studio 9.0\vc\include\streambuf(548) : see declaration of 'std::operator =='
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(41) : error C2784: 'bool std::operator ==(const std::allocator<_Ty> &,const std::allocator<_Other> &) throw()' : could not deduce template argument for 'const std::allocator<_Ty> &' from 'Book'
1> c:\program files\microsoft visual studio 9.0\vc\include\xmemory(173) : see declaration of 'std::operator =='
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(41) : error C2784: 'bool std::operator ==(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'Book'
1> c:\program files\microsoft visual studio 9.0\vc\include\xutility(2246) : see declaration of 'std::operator =='
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(41) : error C2784: 'bool std::operator ==(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'Book'
1> c:\program files\microsoft visual studio 9.0\vc\include\xutility(2050) : see declaration of 'std::operator =='
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(41) : error C2784: 'bool std::operator ==(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'Book'
1> c:\program files\microsoft visual studio 9.0\vc\include\utility(83) : see declaration of 'std::operator =='
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(41) : error C2676: binary '==' : 'Book' does not define this operator or a conversion to a type acceptable to the predefined operator
1>Generating Code...
1>Skipping... (no relevant changes detected)
1>main.cpp
1>Build log was saved at "file://c:\Users\Ryan Clardy\Documents\Visual Studio 2008\Projects\CS 124\project 2\project 2\Debug\BuildLog.htm"
1>project 2 - 14 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Thanks again guys!
RC