954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem accessing private members and overloading operators

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

CrimsonTider
Newbie Poster
14 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Use the accessor functions of the classes instead of trying to access the class members directly.

For example: instead of using L.add[i].author use L.add[i].getAuthor()

h3xc0de
Junior Poster in Training
70 posts since Feb 2009
Reputation Points: 29
Solved Threads: 12
 

Worked perfectly thanks. I really should have done that from the beginning, not sure why I didn't. Too many hours at the computer probably lol

Any idea about the second set of errors?

CrimsonTider
Newbie Poster
14 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

recompile and post a list of the errors and I will take a look at them

h3xc0de
Junior Poster in Training
70 posts since Feb 2009
Reputation Points: 29
Solved Threads: 12
 

The other set of errors are coming from the statement L.arr[i]==b
It is trying to compare 2 books but it doesn't know how to compare a book yet because you haven't overloaded the == operator in the Book class

h3xc0de
Junior Poster in Training
70 posts since Feb 2009
Reputation Points: 29
Solved Threads: 12
 

Here's the errors:

1>------ Build started: Project: project 2, Configuration: Debug Win32 ------
1>Compiling...
1>lib.cpp
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(44) : 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(44) : 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(44) : 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(44) : 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(44) : 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(44) : 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(44) : 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(44) : 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(44) : error C2676: binary '==' : 'Book' does not define this operator or a conversion to a type acceptable to the predefined operator
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 - 9 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


And here's where the errors are coming from:

//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..."
		if(L.arr[i]==b){
			L.arr[i]=temp;
			--L.count;
		}
	}
}


When I replace (L.arr[i]==b) with (L.arr[i].getTitle()==b.getTitle()) it compiles just fine. But that means I'd need to run several if-statements to check each separate parameter of the object (auther, title, genre, pubyear, publisher). Is there an easier way to do this?

Thanks
RC

CrimsonTider
Newbie Poster
14 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Must have posted at the same time. Look at my post above.

h3xc0de
Junior Poster in Training
70 posts since Feb 2009
Reputation Points: 29
Solved Threads: 12
 

ohok so I'll just have to check each parameter inside a definition for a function overloading the == operator. thanks man!

CrimsonTider
Newbie Poster
14 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Yep, but you will still have to use several if statements in the overloaded == function.

h3xc0de
Junior Poster in Training
70 posts since Feb 2009
Reputation Points: 29
Solved Threads: 12
 

Alright new problem lol. The program compiles just fine, but there's still some kind of problem with at least one of my overloaded operators. It looks like the books are not being added to the library. Here's my new lib.cpp:

#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(0); i<L.count; i++){
		cout<< "Title: " << L.arr[i].getTitle() << endl;
		cout<< "Author: " << L.arr[i].getAuthor() << endl;
		cout<< "Genre: " << L.arr[i].getGenre() << endl;
		cout<< "Publisher: " << L.arr[i].getPublisher() << endl;
		cout<< "Publication Year: " << L.arr[i].getPubYear() << endl << endl;
		return os;
	}
}

//Compares two books to see if they are the same book and returns true/false
bool operator== (Book a, Book b){
	if(a.getAuthor()==b.getAuthor() && 
		a.getTitle()==b.getTitle() && 
		a.getGenre()==b.getGenre() && 
		a.getPubYear()==b.getPubYear() &&
		a.getPublisher()==b.getPublisher()){
			return true;
	}
	else 
		return false;
}

//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++){
		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 the output I'm getting:

Glenn Brookshear Computer Science: an Overview CS 2009
Enter Title:
testing the title
Enter Author's Name:
testing the author
Enter Genre:
testing the genre
Enter Publisher:
testing the publisher
Enter Publication Year (####):
2010
Title: "Python Programming"
Author: John Zelle
Genre: CS
Publisher: Franklin, Beedle & Associates
Publication Year: 2004

Title: "Computer Science: an Overview"
Author: Glenn Brookshear
Genre: CS
Publisher: Pearson-Addision-Wesley
Publication Year: 2009

Title: "Starting out with Python"
Author: Tony Gaddis
Genre: CS
Publisher: Pearson-Addision-Wesley
Publication Year: 2009

Title: "Computer Science: an Overview"
Author: Glenn Brookshear
Genre: CS
Publisher: Pearson-Addision-Wesley
Publication Year: 2009

Title: "testing the title"
Author: testing the author
Genre: testing the genre
Publisher: testing the publisher
Publication Year: 2010

Books in Library: 0
Library Title: CS Books
Current Books in Library (less than 2 years old): 0


As you can see from looking at the main.cpp , there are supposed to be 5 books added to the library and then one book taken out, but according to the output, the library has no books...

any ideas?

CrimsonTider
Newbie Poster
14 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Look in to c++ operator overloading, you don't have them coded correctly! Do a Google search on them. When I got some more time in a few I can post an example.

h3xc0de
Junior Poster in Training
70 posts since Feb 2009
Reputation Points: 29
Solved Threads: 12
 

The only thing I can think of is that maybe they shouldn't be "friends"? Which means I'd need to take out their first parameters and define them using Lib::operator...right? I don't have time to do that right now cause I've got to run, but I'll be back in a couple of hours. If that's not right then I'm at a loss for the moment lol

CrimsonTider
Newbie Poster
14 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

Okay so that wasn't it lol. I've gone over the syntax multiple times and I can't seem to find/solve the problem. I'm sure I'm missing something easy but I just can't find it.

I did mess around with the main.cpp a little bit so that I could isolate the problem and it is definitely the overloaded operator+ (though there could be something wrong with my overloaded operator- also, but I'll worry about that once the + is fixed)

CrimsonTider
Newbie Poster
14 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

I think I see the problem! Give me a few min to make sure I'm correct and I will post an example in a few!

h3xc0de
Junior Poster in Training
70 posts since Feb 2009
Reputation Points: 29
Solved Threads: 12
 

Can you reupload all your files or pm them to me so I can make sure I got all the newest versions.

h3xc0de
Junior Poster in Training
70 posts since Feb 2009
Reputation Points: 29
Solved Threads: 12
 

Well I spoke a little too soon on that last post. I tried taking the friend off of operater+ and operator- again and then made the necessary changes to the definitions and this time that seemed to work. But I'm having a problem with my operator== now.

Here's my updated lib.cpp:

#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(0); i<L.count; i++){
		cout<< "Title: " << "\"" << L.arr[i].getTitle() << "\"" << endl;
		cout<< "Author: " << L.arr[i].getAuthor() << endl;
		cout<< "Genre: " << L.arr[i].getGenre() << endl;
		cout<< "Publisher: " << L.arr[i].getPublisher() << endl;
		cout<< "Publication Year: " << L.arr[i].getPubYear() << endl << endl;
	}
return os;
}

//Compares two books to see if they are the same book and returns true/false
bool Lib::operator ==(Book b){
	if(author==b.getAuthor() && 
		title==b.getTitle() && 
		genre==b.getGenre() && 
		pubyear==b.getPubYear() &&
		publisher==b.getPublisher()){
			return true;
	}
	else 
		return false;
}

//Adds a Book to the Lib
void Lib::operator +(Book b){
	arr[count]=b;
	count=(count+1);
}

//Takes a Book out of the Lib
void Lib::operator -(Book b){
	Book temp;
	for(int i(0);i<count;i++){
		if(arr[i]==b){
			arr[i]=temp;
			count=(count-1);
		}
	}
}

//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;
}


Here's the errors I'm getting (they're coming from my operator==):

1>------ Build started: Project: project 2, Configuration: Debug Win32 ------
1>Compiling...
1>lib.cpp
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(33) : error C2065: 'author' : undeclared identifier
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(35) : error C2065: 'genre' : undeclared identifier
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(36) : error C2065: 'pubyear' : undeclared identifier
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(37) : error C2065: 'publisher' : undeclared identifier
1>c:\users\ryan clardy\documents\visual studio 2008\projects\cs 124\project 2\project 2\lib.cpp(55) : 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(55) : 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(55) : 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(55) : 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(55) : 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(55) : 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(55) : 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(55) : 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(55) : error C2676: binary '==' : 'Book' does not define this operator or a conversion to a type acceptable to the predefined operator
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 - 13 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


It seems to take title just fine, but not the rest of the identifiers...

CrimsonTider
Newbie Poster
14 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

yeah here's the rest of my files...

main.cpp: http://cs.ua.edu/124/Spring2009/Projects/main.cpp

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);
	bool operator== (Book b);
	void operator+ (Book b);
	void operator- (Book b);
	int getLibTotal( );
	int getCurrentBooks( );
	

};

#endif


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


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 << endl;
	return os;
}


here ya go, thanks!

CrimsonTider
Newbie Poster
14 posts since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

One problem I see right away is that your overloaded == operator should be in your book class to compare 2 books.

h3xc0de
Junior Poster in Training
70 posts since Feb 2009
Reputation Points: 29
Solved Threads: 12
 
bool Book::operator ==(const Book &b)
{
	if(author==b.author && 
		title==b.title && 
		genre==b.genre && 
		pubyear==b.pubyear &&
		publisher==b.publisher)
	{
			return true;
	}
	else 
	{
		return false;
	}
}
h3xc0de
Junior Poster in Training
70 posts since Feb 2009
Reputation Points: 29
Solved Threads: 12
 
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 << endl;
	return os;
}

This is wrong! How do you know that when the << operator is being use it is to the cout, you should be applying it to the stream you are given 'os' you have named it! Not going straight to cout.

Please read about these things :\

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You