Unsure how to access private member functions from .cpp file!

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 718 Views - Last Post: 25 August 2011 - 04:36 PM Rate Topic: -----

#1 lauw0203  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 24-August 11

Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 01:53 PM

Hi all,

I am creating a database that allows a user to add, search, browse, delete and edit a book into the system, which saves into a CSV file. I am trying to use three-tier hierarchical structure, 3 levels of inheritance, a double/linked list or a binary tree with as many of my functions throw exceptions and call exceptions being put in a catch block.

I have been trying many things and it got to the point where I was getting so many errors that I have decided to restart and take it one step at a time. The first structure I built was quite poor and my classes were not correct at all!

This time round, I am fairly confident with my classes but I am struggling to understand how to pull access to private member functions. I have created my classes in a header file

#include <string>

using std::string;

//Root class that all books will inherit from
class bookBase
{
private: 
	string publisher;
	string title;

public:

	void setBookPublisher(string bookPublisher)
	{
		publisher = bookPublisher;
	}
	string getBookPublisher()
	{
		return publisher;
	}

	void setBookTitle(string bookTitle)
	{
		title = bookTitle;
	}
	string getBookTitle()
	{
		return title;
	}
};

//searchable book
class searchableBook : public bookBase
{
private:
	double ISBNNumber;

public:

	void setBookISBN(int bookISBN)
	{
		ISBNNumber = bookISBN;
	}
	int getBookISBN()
	{
		return ISBNNumber;
	}
};

//general book class
class book : public searchableBook
{
private:
	string author;

public:

	void setBookAuthor(string bookAuthor)
	{
		author = bookAuthor;
	}
	string getBookAuthor()
	{
		return author;
	}
};

//proceedings book class
class proceedingsBook : public searchableBook
{
private:
	string editor;
	int conferenceDate;

public:

	void setBookEditor(string bookEditor)
	{
		editor = bookEditor;
	}
	string getBookEditor()
	{
		return editor;
	}

	void setBookConfDate(int bookConfDate)
	{
		conferenceDate = bookConfDate;
	}
	int getBookConfDate()
	{
		return conferenceDate;
	}
};



and my main code in the .cpp file which is where the problem lies.


#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <sstream>

#include "bookSystem.h"

using namespace std; 

book* buildG(string dataLine);					
proceedingsBook* buildP(string dataLine);					

void displayG(book* gPtr);						
void displayP(proceedingsBook* pPtr);

string extractLastString(string & st);
int extractLastInt(string & st);

int _tmain(int argc, _TCHAR* argv[])
{}

void displayG(book* gPtr)
{
	cout << "Book Title: " << gPtr->title << endl;
	cout << "Book Author: " << gPtr->author << endl;
	cout << "Book Publisher: " << gPtr->publisher << endl;
	cout << "Book ISBN Number: " << gPtr->ISBNNumber << endl;
}

void displayP(proceedingsBook* pPtr)
{					
	cout << "Book Title: " << pPtr->title << endl;
	cout << "Book Editor: " << pPtr->editor << endl;
	cout << "Book Publisher: " << pPtr->publisher << endl;
	cout << "Book Conference Date: " << pPtr->conferenceDate << endl;
	cout << "Book ISBN Number: " << pPtr->ISBNNumber << endl;
}

book* buildG(string dataLine)
{
	book* ptr = new book;
	ptr->title = extractLastString(dataLine);
	ptr->author = extractLastString(dataLine);
	ptr->publisher = extractLastString(dataLine);
	ptr->ISBNNumber = extractLastInt(dataLine)

	return ptr;
}

proceedingsBook* buildP(string dataLine)
{
	proceedingsBook* ptr = new proceedingsBook;
	ptr->title = extractLastString(dataLine);
	ptr->editor = extractLastString(dataLine);
	ptr->publisher = extractLastString(dataLine);
	ptr->conferenceDate = extractLastInt(dataLine);
	ptr->ISBNNumber = extractLastInt(dataLine);

	return ptr;
}

string extractLastString(string & st)
{
	unsigned posLastComma = st.find_last_of(",");	// find last ',' in string
	string result = st.substr(posLastComma+1);		// extract && save everything after the last ,
	st = st.substr(0,posLastComma);					// remove chars from last , including ,
	return result;									// return extracted substring
}
int extractLastInt(string & st)
{
	string numAsString = extractLastString(st);		// extract number as string
	istringstream iss(numAsString);					// research input stringstreams
	int result;
	iss >> result;
	return result;
}




As to be expected, I am generating errors when I call attributes from the classes and I really need some help in figuring out why!

Also, I wanted to ask this: I am incorporating a linked list and at the moment I have a struct at the top of my .h file called "Books";


struct Books
{
	string title;
	string author;
	string publisher;
	//string editor;
	//int conferenceDate;
	string isbn;
};




Is it possible to use my classes that I have created and two of my variables ideally I would like to be an int, but is string the only option I have?

Any help is appreciated :)

Is This A Good Question/Topic? 0
  • +

Replies To: Unsure how to access private member functions from .cpp file!

#2 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1150
  • View blog
  • Posts: 2,528
  • Joined: 05-May 05

Re: Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 02:01 PM

cout << "Book Author: " << gPtr->author << endl;


You can't access a private class field outside the class. Use Book.getBookAuthor.

Quote

Is it possible to use my classes that I have created and two of my variables ideally I would like to be an int, but is string the only option I have?


I don't understand your question. Are you asking how to create a linked list of books? What's the struct for?

This post has been edited by blackcompe: 25 August 2011 - 02:01 PM

Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 02:04 PM

I think you want to make these "protected" rather than private. That way the derived classes have access to those members.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,317
  • Joined: 25-December 09

Re: Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 02:05 PM

Quote

Is it possible to use my classes that I have created and two of my variables ideally I would like to be an int, but is string the only option I have?

A class or structure can contain any type of variable, including other classes and structures.

Jim

This post has been edited by jimblumberg: 25 August 2011 - 02:06 PM

Was This Post Helpful? 0
  • +
  • -

#5 lauw0203  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 24-August 11

Re: Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 02:14 PM

Thanks everyone for your replies,

I have changed "private" to "protected" and have had nearly double the amount of errors, all similar to, "Error 29 error C2247: 'bookBase::getBookPublisher' not accessible because 'searchableBook' uses 'private' to inherit from 'bookBase'".

Also, tried "Book.Get.." and this did not work either.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,317
  • Joined: 25-December 09

Re: Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 02:22 PM

Post your current code along with the error messages exactly as they appear in your development environment.


Jim
Was This Post Helpful? 0
  • +
  • -

#7 lauw0203  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 24-August 11

Re: Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 02:29 PM

BookSystem.cpp


#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <sstream>

#include "bookSystem.h"

using namespace std; 

book* buildG(string dataLine);					
proceedingsBook* buildP(string dataLine);					

void displayG(book* gPtr);						
void displayP(proceedingsBook* pPtr);

string extractLastString(string & st);
int extractLastInt(string & st);


int _tmain(int argc, _TCHAR* argv[])
{

}

void displayG(book* gPtr)
{
	cout << "Book Title: " << gPtr->title << endl;
	cout << "Book Author: " << gPtr->author << endl;
	cout << "Book Publisher: " << gPtr->publisher  << endl;
	cout << "Book ISBN Number: " << gPtr->ISBNNumber << endl;
}

void displayP(proceedingsBook* pPtr)
{					
	cout << "Book Title: " << pPtr->title << endl;
	cout << "Book Editor: " << pPtr->editor << endl;
	cout << "Book Publisher: " << pPtr->publisher << endl;
	cout << "Book Conference Date: " << pPtr->conferenceDate << endl;
	cout << "Book ISBN Number: " << pPtr->ISBNNumber << endl;
}

book* buildG(string dataLine)
{
	book* ptr = new book;
	ptr->title = extractLastString(dataLine);
	ptr->author = extractLastString(dataLine);
	ptr->publisher = extractLastString(dataLine);
	ptr->ISBNNumber = extractLastInt(dataLine)

	return ptr;
}

proceedingsBook* buildP(string dataLine)
{
	proceedingsBook* ptr = new proceedingsBook;
	ptr->title = extractLastString(dataLine);
	ptr->editor = extractLastString(dataLine);
	ptr->publisher = extractLastString(dataLine);
	ptr->conferenceDate = extractLastInt(dataLine);
	ptr->ISBNNumber = extractLastInt(dataLine);

	return ptr;
}

string extractLastString(string & st)
{
	unsigned posLastComma = st.find_last_of(",");	// find last ',' in string
	string result = st.substr(posLastComma+1);		// extract && save everything after the last ,
	st = st.substr(0,posLastComma);					// remove chars from last , including ,
	return result;									// return extracted substring
}
int extractLastInt(string & st)
{
	string numAsString = extractLastString(st);		// extract number as string
	istringstream iss(numAsString);					// research input stringstreams
	int result;
	iss >> result;
	return result;
}







bookSystem.h (changed to protected as suggested above)


#include <string>

using std::string;

//Root class that all books will inherit from
class bookBase
{
protected: 
	string publisher;
	string title;

public:

	void setBookPublisher(string bookPublisher)
	{
		publisher = bookPublisher;
	}
	string getBookPublisher()
	{
		return publisher;
	}

	void setBookTitle(string bookTitle)
	{
		title = bookTitle;
	}
	string getBookTitle()
	{
		return title;
	}
};

//searchable book
class searchableBook : bookBase
{
protected:
	int ISBNNumber;

public:

	void setBookISBN(int bookISBN)
	{
		ISBNNumber = bookISBN;
	}
	int getBookISBN()
	{
		return ISBNNumber;
	}
};

//general book class
class book : searchableBook
{
protected:
	string author;

public:

	void setBookAuthor(string bookAuthor)
	{
		author = bookAuthor;
	}
	string getBookAuthor()
	{
		return author;
	}
};

//proceedings book class
class proceedingsBook : searchableBook
{
protected:
	string editor;
	int conferenceDate;

public:

	void setBookEditor(string bookEditor)
	{
		editor = bookEditor;
	}
	string getBookEditor()
	{
		return editor;
	}

	void setBookConfDate(int bookConfDate)
	{
		conferenceDate = bookConfDate;
	}
	int getBookConfDate()
	{
		return conferenceDate;
	}
};




Errors:

Error	21	error C2332: 'class' : missing tag name	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	14

Error	22	error C2059: syntax error : '}'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	14

Error	23	error C2143: syntax error : missing ';' before '}'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	14

Error	24	error C2238: unexpected token(s) preceding ';'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	14

Error	1	error C2248: 'bookBase::title' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	27

Error	52	error C2039: 'isbn' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.cpp	27

Error	2	error C2248: 'book::author' : cannot access protected member declared in class 'book'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	28

Error	3	error C2248: 'bookBase::publisher' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	29

Error	4	error C2248: 'searchableBook::ISBNNumber' : cannot access protected member declared in class 'searchableBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem
\booksystem.cpp	30

Error	25	error C4430: missing type specifier - int assumed. Note: C++ does not support 
default-int	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	34

Error	26	error C2143: syntax error : missing ',' before '&'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	34

Error	5	error C2248: 'bookBase::title' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	35

Error	53	error C2039: 'isbn' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.cpp	35

Error	54	fatal error C1903: unable to recover from previous error(s); stopping compilation	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.cpp	35

Error	6	error C2248: 'proceedingsBook::editor' : cannot access protected member declared in class 'proceedingsBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem
\booksystem.cpp	36

Error	7	error C2248: 'bookBase::publisher' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	37

Error	8	error C2248: 'proceedingsBook::conferenceDate' : cannot access protected member declared in class 'proceedingsBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	38

Error	9	error C2248: 'searchableBook::ISBNNumber' : cannot access protected member declared in class 'searchableBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	39

Error	33	error C2039: 'isbn' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	41

Error	34	error C2039: 'isbn' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	43

Error	35	error C2039: 'isbn' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	43

Error	36	error C2039: 'title' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	44

Error	37	error C2039: 'title' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	44

Error	10	error C2248: 'bookBase::title' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	45

Error	38	error C2039: 'author' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	45

Error	39	error C2039: 'author' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	45

Error	11	error C2248: 'book::author' : cannot access protected member declared in class 'book'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	46

Error	40	error C2039: 'publisher' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	46

Error	41	error C2039: 'publisher' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	46

Error	12	error C2248: 'bookBase::publisher' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	47

Error	13	error C2248: 'searchableBook::ISBNNumber' : cannot access protected member declared in class 'searchableBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem
\booksystem.cpp	48

Error	42	error C2039: 'isbn' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	48

Error	43	error C2039: 'title' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	49

Error	14	error C2143: syntax error : missing ';' before 'return'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	50

Error	44	error C2039: 'author' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	50

Error	45	error C2065: 'count' : undeclared identifier	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	51

Error	46	error C2039: 'publisher' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	51

Error	15	error C2248: 'bookBase::title' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	56

Error	16	error C2248: 'proceedingsBook::editor' : cannot access protected member declared in class 'proceedingsBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	57

Error	17	error C2248: 'bookBase::publisher' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	58

Error	18	error C2248: 'proceedingsBook::conferenceDate' : cannot access protected member declared in class 'proceedingsBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	59

Error	19	error C2248: 'searchableBook::ISBNNumber' : cannot access protected member declared in class 'searchableBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	60

Error	27	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	64

Error	28	error C2143: syntax error : missing ',' before '&'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	64

Error	47	error C2039: 'isbn' : is not a member of 'Books'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	71

Error	29	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	94

Error	30	error C2143: syntax error : missing ',' before '&'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	94

Error	31	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	95

Error	32	error C2143: syntax error : missing ',' before '&'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	95

Error	48	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	100

Error	49	error C2143: syntax error : missing ',' before '&'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	100

Error	50	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	101

Error	51	error C2143: syntax error : missing ',' before '&'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	101




Thanks for taking the time to look at my code :)

This post has been edited by lauw0203: 25 August 2011 - 02:30 PM

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,317
  • Joined: 25-December 09

Re: Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 02:39 PM

Your first four errors are in your linkedlist.h file.

Quote

Error 21 error C2332: 'class' : missing tag name c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h 14

Error 22 error C2059: syntax error : '}' c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h 14

Error 23 error C2143: syntax error : missing ';' before '}' c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h 14

Error 24 error C2238: unexpected token(s) preceding ';' c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h 14


Which you have not posted. You should always work on your errors from the first to the last, sometimes when you fix one error many other errors will also be fixed.

Edit: For most of the other errors, you can not access private or protected members outside of the class, you should be using the public get/set functions to access these variables. And in some cases you have not defined the variables you are trying to access.

Jim

This post has been edited by jimblumberg: 25 August 2011 - 02:42 PM

Was This Post Helpful? 0
  • +
  • -

#9 lauw0203  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 24-August 11

Re: Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 02:44 PM

Sorry,

That was just something I was playing around with and hadn't completed.

linkedList.h


#include <iostream>
#include <string>
using namespace std;
 
typedef struct Books ElementType;
typedef std::string KeyType;
 
//struct which has three elements: key, name, and title
struct Books
{
	string title;
	string author;
	string publisher;
	//string editor;
	//int conferenceDate;
	string isbn;
};
 
class LinkedList
{

private:
	
	class Node
	{

	public:
		ElementType data;
		Node * next;
		Node(ElementType dataValue){
		data = dataValue;
		next = NULL;
	}
};

typedef Node * NodePointer;
Node *LinkedList::found(const ISBNType &isbn)
{
	NodePointer ptr;
	ptr = first;
	ElementType item;
	while (ptr != NULL)
	{
		if (ptr->data.isbn == isbn)
		{
			item.isbn = ptr->data.isbn;
			item.title = ptr->data.title;
			item.author = ptr->data.author;
			item.publisher = ptr->data.publisher;

			cout << " isbn: " << item.isbn << endl;
			cout << " Title: " << item.title << endl;
			cout << " Author: " << item.author << endl;
			count << " Publisher: " << item.publisher << endl;

			return ptr;
		}

		else
		ptr = ptr->next;
	}

	if (ptr == NULL)
	return NULL;
}

Node *LinkedList::deleter(const ISBNType &isbn)
{
	NodePointer ptr;
	ptr = first;
	ElementType item;
		while (ptr != NULL)
		{
			if (ptr->data.isbn == isbn)
			{
			predPtr->next=ptr->next;
			delete ptr;
			return predPtr;
			}

		else
		ptr = ptr->next;

		}

		if (ptr == NULL)
		return NULL;
}

NodePointer first;
NodePointer predPtr;

public:

	LinkedList();
	void insert (const ElementType &item);
	bool find (const ISBNType &isbn, ElementType &item);
	bool erase (const ISBNType &isbn);
	void display();

};

	bool operator<(const ElementType &e1, const ISBNType &isbn);
	bool operator>=(const ElementType &e1, const ISBNType &isbn);




linkedList.cpp


#include <iostream>
#include <iomanip>

using namespace std;

#include "linkedList.h"
 
//contructor
LinkedList::LinkedList()
{
	first = NULL;
}
 
//insert function
void LinkedList::insert(const ElementType &dataValue)
{
	NodePointer nPtr = new Node(dataValue);
	predPtr = first;
	NodePointer afterPtr;

	if (first == NULL)
	{
		nPtr->next = first;
		first = nPtr;
	}

	else if (predPtr->data >= nPtr->data.isbn)
	{
		nPtr->next = first;
		first = nPtr;
	}

	else
	{
		while (!((predPtr->next == NULL) || (predPtr->next->data >= nPtr->data.isbn)))
		{
			predPtr=predPtr->next;
		}

	nPtr->next = predPtr->next;
	predPtr->next = nPtr;
		
	}
}
 
//find function
bool LinkedList::find(const ISBNType &isbn, ElementType &item)
{
	if (found(isbn))
	{
		return true;
	}

	else if (!found(isbn))
		return false;
}
 
//erase function
bool LinkedList::erase(const ISBNType &isbn)
{
	if (deleter(isbn))
	{
		return true;
	}

	else if (!deleter(isbn))
		return false;
}
 
//display function
void LinkedList::display()
{
	LinkedList::NodePointer ptr;
	ptr = first;
	cout << "--------------------------------------------------------------------------------" << endl;
	cout << " ISBN Author Title Publisher" << endl;
	cout << "--------------------------------------------------------------------------------" << endl;
	
	while (ptr != NULL)
	{
		cout << left << setw(20) << ptr->data.isbn << " " << setw(20) << ptr->data.name << " " << setw(40) << ptr->data.title << endl;
		ptr	= ptr->next;
	}
}
 
//overloading ==
bool operator==(const ElementType &e1, const ISBNType &isbn)
{
	if (e1.isbn == isbn)
		return true;

	else
		return false;
}
 
//overloading >=
bool operator>=(const ElementType &e1, const ISBNType &isbn)
{
	if (e1.isbn >= isbn)
		return true;

	else
		return false;
}




I know only have 37 errors and it is now the top one that is relevant to my question;


Error	1	error C2248: 'bookBase::title' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	27

Error	35	error C2679: binary '>=' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.cpp	27

Error	2	error C2248: 'book::author' : cannot access protected member declared in class 'book'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	28

Error	3	error C2248: 'bookBase::publisher' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	29

Error	4	error C2248: 'searchableBook::ISBNNumber' : cannot access protected member declared in class 'searchableBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem
\booksystem.cpp	30

Error	5	error C2248: 'bookBase::title' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	35

Error	36	error C2679: binary '>=' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.cpp	35

Error	37	fatal error C1903: unable to recover from previous error(s); stopping 
compilation	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.cpp	35

Error	6	error C2248: 'proceedingsBook::editor' : cannot access protected member declared in class 'proceedingsBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	36

Error	7	error C2248: 'bookBase::publisher' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	37

Error	20	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	37

Error	21	error C2143: syntax error : missing ',' before '&'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	37

Error	8	error C2248: 'proceedingsBook::conferenceDate' : cannot access protected member declared in class 'proceedingsBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	38

Error	9	error C2248: 'searchableBook::ISBNNumber' : cannot access protected member declared in class 'searchableBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	39

Error	28	error C2065: 'isbn' : undeclared identifier	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	44

Error	10	error C2248: 'bookBase::title' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	45

Error	11	error C2248: 'book::author' : cannot access protected member declared in class 'book'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	46

Error	12	error C2248: 'bookBase::publisher' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	47

Error	13	error C2248: 'searchableBook::ISBNNumber' : cannot access protected member declared in class 'searchableBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	48

Error	14	error C2143: syntax error : missing ';' before 'return'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	50

Error	29	error C2065: 'count' : undeclared identifier	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	54

Error	15	error C2248: 'bookBase::title' : cannot access protected member declared in class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	56

Error	16	error C2248: 'proceedingsBook::editor' : cannot access protected member declared in class 'proceedingsBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	57

Error	17	error C2248: 'bookBase::publisher' : cannot access protected member declared in 
class 'bookBase'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	58

Error	18	error C2248: 'proceedingsBook::conferenceDate' : cannot access protected member 
declared in class 'proceedingsBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	59

Error	19	error C2248: 'searchableBook::ISBNNumber' : cannot access protected member declared in class 'searchableBook'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\booksystem.cpp	60

Error	22	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	67

Error	23	error C2143: syntax error : missing ',' before '&'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	67

Error	30	error C2065: 'isbn' : undeclared identifier	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	74

Error	24	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	97

Error	25	error C2143: syntax error : missing ',' before '&'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	97

Error	26	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	98

Error	27	error C2143: syntax error : missing ',' before '&'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	98

Error	31	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	103

Error	32	error C2143: syntax error : missing ',' before '&'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	103

Error	33	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	104

Error	34	error C2143: syntax error : missing ',' before '&'	c:\users\lauraelizabeth\desktop\booksystem\booksystem\linkedlist.h	104



Was This Post Helpful? 0
  • +
  • -

#10 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1150
  • View blog
  • Posts: 2,528
  • Joined: 05-May 05

Re: Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 03:19 PM

You shouldn't be accessing fields directly. Use get/set methods. The reason your get/set method calls are throwing compile-time errors is because your not using public inheritance. Instead, do:

class book : public searchableBook


then you can write:

cout << "Book Title: " << gPtr->getBookTitle() << endl;


Your code should be written like this:

#include <iostream>
#include <string>
using namespace std;

class Book
{
    protected:
        string title;
    public:
        Book(const char* t): title(t) {}
        string getTitle() { return title; }
};

class Ebook: public Book
{
    string url;
    public:
        Ebook(const char* u, const char* t): Book(t), url(u) {}
        string getURL() { return url; }
};

int main()
{
    Ebook* e = new Ebook("http://www.amazon.com", "The C++ Programming Language");
    cout<<e->getTitle();
}



This post has been edited by blackcompe: 25 August 2011 - 03:25 PM

Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1311
  • View blog
  • Posts: 4,503
  • Joined: 19-February 09

Re: Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 03:21 PM

Were you given these functions to use?

book* buildG(string dataLine);					
proceedingsBook* buildP(string dataLine);					

void displayG(book* gPtr);						
void displayP(proceedingsBook* pPtr);

string extractLastString(string & st);
int extractLastInt(string & st);


Was This Post Helpful? 0
  • +
  • -

#12 lauw0203  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 24-August 11

Re: Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 03:48 PM

They were in an example given, but the example didn't use linked lists etc and didn't use classes, but it used structs! Going to have a go at that blackcompe :)
Was This Post Helpful? 0
  • +
  • -

#13 lauw0203  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 24-August 11

Re: Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 03:59 PM

Blackcompe, where you have used "EBook", if I were to use my "class searchableBook : public bookBase", is it possible for me to keep the "ISBNNumber" as an "int", might be a silly question but I've got a feeling it won't work.

Also, is there no "protected" in front of "string url" for a reason? I'm guessing if it is intentional it's so that the other classes can access it? If so, I need to try and make all of the attributes private.

!UPDATE!

Just tried that code, the class has to be in a separate file for what I am producing.

Thanks :)

This post has been edited by lauw0203: 25 August 2011 - 04:09 PM

Was This Post Helpful? 0
  • +
  • -

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 04:33 PM

unfortunately an ISBN number is 10 digits (although 1 is a check digit) so you can't store a raw ISBN in a 32bit integer as the largest value of an unsigned integer is 4,294,967,295 which has the 10 digits we need but alas the digit to the far left has to be less than 4.

Of course you could not use a floating pointer number either since you may encounter errors related to rounding.

you could use a 64bit integer (long long). Of you could break the number up in some way.
Was This Post Helpful? 0
  • +
  • -

#15 stackoverflow  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 164
  • View blog
  • Posts: 545
  • Joined: 06-July 11

Re: Unsure how to access private member functions from .cpp file!

Posted 25 August 2011 - 04:34 PM

It's actually bad OOP to create a private attribute and then create a public method to get or set a private attribute. It's quite common but it's also considered one of the most common OOP mistakes.
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2