3 Replies - 382 Views - Last Post: 09 December 2010 - 05:26 PM Rate Topic: -----

#1 Riskinit  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 157
  • Joined: 14-January 10

Help getting classes to communicate...

Posted 09 December 2010 - 01:55 PM

I am having trouble getting my hash table to point to a specific location.

#ifndef STUDENTRECORD_H_INCLUDED
#define STUDENTRECORD_H_INCLUDED

#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <windows.h>
#include <cstdlib>
#include <ctime>
using namespace std;

void menu();
void displayMenu();
void pickChoice();
void info();

class book
{
	friend class linkedList;
private:
	class student
	{
		friend class book;
		string name;
		string number;
		string gpa;
		student *left;
		student *right;
		
		student(string name1, string number1, string gpa1, student *left1 = NULL, student *right1 = NULL)
		{
			name = name1;
			number = number1;
			gpa = gpa1;
			left = left1;
			right = right1;
		}
	};

	int count;
	student *root;
	student *currentRoot;
	ofstream recordBook;
	
	void _add(student *&, string, string, string);
	void _display_book(student *);
	bool _search_for_name(string, string&, string&);
	bool _edit_entry(string &name, string &edited_number);
	void _del_person(student *&, string);
	void _save(student *root);
	void _del_Tree(student *);

    public:
		book(void);	//Constructor
		~book(void);//Destructor
		void add(string entry_name, string telephone_number, string gpa);
		void display_book();
		bool search_for_name(string find_name);
		bool edit_entry(string entered_name, string edited_number);
		void del_person(string entry_name);
		void save();
		void load_data();
};

class linkedList 
{
	friend class book;
	int someInt;
	struct node 
	{
	public:
		string key;
		node *link;
	}
	*pointer;
public:
	linkedList();
	~linkedList();
	void append(string &str);
	void del(string &str);
	void display();
};

class hashTable
{
	friend class book;
	linkedList *bucket;
	int arraySize;
	int numElements;//per array element.
public:
	hashTable(int tsize);
	~hashTable();
	int hashNumber(string str);
	void add(string &str);
	void remove(string &str);
	void output();
};
#endif


Above is the header file. As you can see in my class I have
student *currentRoot;


which is assigned to a node in my binary tree elsewhere in the program.

Below is where I try to point to where currentRoot is pointing.
void linkedList::append(string &str)
{
	node *q, *t;
	if(pointer == NULL)
	{
               // pointer = something...
	 	pointer = new node;
		pointer->key = str;
		pointer->link = NULL;
		someInt += 1;
	}
	else
	{
		q = pointer;
		while(q->link != NULL)
		{
			q = q->link;
		}
		t = new node;
		t->key = str;
		t->link = NULL;
		q->link = t;
		someInt += 1;
	}
}


Its not complete yet. My problem is in my header file under the linked_List class.
class linkedList
{
	friend class book;
	int someInt;
	struct node 
	{
	public:
		string key;
		node *link;
	}
	*pointer;
public:
	linkedList();
	~linkedList();
	void append(string &str);
	void del(string &str);
	void display();
};


Specifically what goes in the struct node. I need to declare a pointer. But not sure how to say I am pointing to where a pointer points to in another class.

public:
	string key;
	node *link;
        *p = NULL;
}

Well, p is supposed to start out as NULL but I'll assign it to *currentRoot later. Hopefully someone can explain why I can't do this or how I can do it. Just need to know what my mistake is.

This post has been edited by Riskinit: 09 December 2010 - 01:55 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Help getting classes to communicate...

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Help getting classes to communicate...

Posted 09 December 2010 - 02:37 PM

i think your looking for class member pointers. im not precisely sure what you asking but if you want to have a pointer point to item in a class whos type is defined in that class you need to declare the type using the scope operator with that class.

Foo::Bar* baz;



in this baz is a pointer to the Bar type defined in Foo.

This post has been edited by ishkabible: 09 December 2010 - 02:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 Riskinit  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 157
  • Joined: 14-January 10

Re: Help getting classes to communicate...

Posted 09 December 2010 - 03:07 PM

I will try to make what I am doing simpler.

class book
{
	friend class linkedList;
private:
       ...................other stuff.
    public:
		student *currentRoot;
                .......................other stuff.
};

class linkedList 
{
	friend class book;
	int someInt;
	struct node 
	{
	public:
		string key;
		node *link;
		node *link2;
	}
	*pointer;
public:
	int book::*currentRoot = &book::currentRoot;
	linkedList();
	~linkedList();
	void append(string &str);
	void del(string &str);
	void display();
};


Basically I have a pointer currentRoot which I would like to access from another class called linkedList. No, I need to set a pointer in linkedList to point to where currentRoot is pointing.

I have never dealt with class member pointers but google showed me an example... and I tried the following.

int book::*currentRoot = &book::currentRoot;



Which my compiler says "data member initializer is not allowed."


Being more clear... I have the following function in linkedList.

void linkedList::append(string &str)
{
	node *q, *t;
	if(pointer == NULL)
	{

		pointer = new node;
		pointer->key = str;
		pointer->link = NULL;
		pointer->link2 = currentRoot;
		someInt += 1;
	}
	else
	{
		q = pointer;
		while(q->link != NULL)
		{
			q = q->link;
		}
		t = new node;
		t->key = str;
		t->link = NULL;
		q->link = t;
		someInt += 1;
	}
}


Which is still incomplete but I want link2 to point to where currentRoot is... hopefully that makes sense.

This post has been edited by Riskinit: 09 December 2010 - 03:14 PM

Was This Post Helpful? 0
  • +
  • -

#4 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Help getting classes to communicate...

Posted 09 December 2010 - 05:26 PM

yes you want to declare link2 as book::student* instead of node*. now you also need to get rid of those "book::*currentRoot" things and trying to access currentRoot without a class object for it to be a member of. if you want currentRoot to be the same variable for all instances of book you can declare it static and then access it similarly but i doubt that's what you want.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1