Class problem c++

Pages: 12
Hi guys,
I've a problem, i've those class, that now works, because i've the attribute in public, but if i put it in private, or protected and inherited to the second class, it's not work, how can i do it?
I need to respect OOP rules

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
template <typename T>
class BinarySearchTree;

template <typename T>
class BinaryNode
{
	friend class BinarySearchTree<T>;
	T key;
	BinaryNode<T>* left;
	BinaryNode<T>* right;
	BinaryNode<T>* parent;
};

template <typename T>
class BinarySearchTree
{
private:
    BinaryNode<T>* root;
    BinaryNode<T>* newNode(T key);

    BinaryNode<T>* minimum(BinaryNode<T>* node);
    BinaryNode<T>* maximum(BinaryNode<T>* node);
    BinaryNode<T>* successor(BinaryNode<T>* node);

    void insert(BinaryNode<T>** node, T key);
    BinaryNode<T>* search(BinaryNode<T>* node, T key);
    void distance(BinaryNode<T>* node, T key);
    void inorderTreeWalk(BinaryNode<T>* node);
public:
    BinarySearchTree();
    ~BinarySearchTree();

    void insert(T key);
    BinaryNode<T>* search(T key);
    void distance(T key);
    void inorderTreeWalk();

    BinaryNode<T>* remove(BinaryNode<T>* node);
};


There's the header file:
https://www.dropbox.com/s/s8xsn3q01ysyy4f/classi.h

Can you show me how i can do?
Thanks,
Bye
Last edited on
I have to say that, when you declare a private member for a class, you can't use that member on the derived class
they must be public or protected
I tried also with protected, but nothing :\
Tried also with nested class (and public member)
Last edited on
You haven't told us which attributes you're talking about, nor how you're trying to use them that's causing an error, so there's really not a lot we can do to diagnose the problem.

Do you understand the difference between a private and a public member?
Yes,
but i don't know how to create methods for put all attributes (Left, right, parent, and key) privates.
What i must change in BinaryNode and BinarySearchTree Class?
I've done it with friend, but i need to be it with inherited or nested class, how can i do?
I'm not quite sure what you're asking.

The standard way to do this is to make your data members (i.e. attributes) private, because they're implementation details.

If you need the interface of your class to allow other classes to either get the values of those members, or set them, then you should write get and/or set methods in the public interface of your class. These methods can be called by any other class, because they're in the public interface.
can you tell the errors that you are getting?
Yes, my data members must be privates, and i need functions for gets and sets this members.. and this is that i don't know how to do
Well, there you go. As I said, you need to write public get/set methods, so that other classes can call those methods to either retrieve the value of an attribute, or to change the value of the attribute.

It's not difficult. Just think about it a little. If someone asked you write a method to return the value of, say, left, how do you think you'd write that?
BinaryNode<T>* getleft(){ return this->*left; }

It's right?

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
template <typename T>
class BinaryNode
{
private:
	T key;
	BinaryNode<T>* left;
	BinaryNode<T>* right;
	BinaryNode<T>* parent;
public:
	BinaryNode<T> getkey(){ return this->key; }
	BinaryNode<T>* getleft(){ return this->*left; }
	BinaryNode<T>* getright(){ return this->*right; }
	BinaryNode<T>* getparent(){ return this->*parent; }
};

template <typename T>
class BinarySearchTree :public BinaryNode
{
private:
	BinaryNode<T>* root; //radice dell'albero
	BinaryNode<T>* newNode(T key); //create nodes

	//metodi utilizzati dal metodo remove per ristabilire le proprieta' dell'ABR
	BinaryNode<T>* minimum(BinaryNode<T>* node);
	BinaryNode<T>* maximum(BinaryNode<T>* node);
	BinaryNode<T>* successor(BinaryNode<T>* node);

	//metodi richiamati dai metodi "publici"
	void insert(BinaryNode<T>** node, T key);
	BinaryNode<T>* search(BinaryNode<T>* node, T key);
	void distance(BinaryNode<T>* node, T key);
	void inorderTreeWalk(BinaryNode<T>* node);
public:
	BinarySearchTree();
	~BinarySearchTree();

	//metodi di visit, insert and search utilizzati in main
	void insert(T key);
	BinaryNode<T>* search(T key);
	void distance(T key);
	void inorderTreeWalk(); //visita inorder (per stampare in ordine alfabetico)

	BinaryNode<T>* remove(BinaryNode<T>* node);
};


how i change in the class BinarySearchTree?
example: method insert, how i change it?
Last edited on
Yes, that looks fine to me. Although you don't need the this-> - as this is a method of the class, just using return left; will work.
Last edited on
And for set?
BinaryNode<T>* setleft(T l){ left = l; }
it's ok this?
and in the class Binarysearchtree of i must call this?
Example:
void insert(BinaryNode<T>** node, T key);
This?
Well, you need to decide exactly what it is you want to allow other classes to do? Do you want them to supply an object of type T by value, to be copied into the position to the left of the node? Or do you want it it to provide a pointer to an existing object? Or do you want to provide a ready-built BinaryNode?

You have to think about how you want the class to work, and how you want other classes to interact with it.

If you were using public data members previously, presumably you already have some ideas about that.

With insert function, i want to create a new node (node = new BinaryNode)
1
2
3
4
5
6
7
8
9
10
BinaryNode<T>* BinarySearchTree<T>::newNode(T key)
{
	BinaryNode<T>* node = new BinaryNode<T>;
	node->setkey(key); //THIS WORK
	node->setleft(NULL); //But This, i got too many compiler ERRORS
	node->right = NULL;
	node->parent = NULL;

	return node;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template <typename T>
class BinaryNode
{
private:
	T key;
	BinaryNode<T>* left;
	BinaryNode<T>* right;
	BinaryNode<T>* parent;
public:
	BinaryNode<T> getkey(){ return key; }
	void setkey(T k){ key = k; }
	BinaryNode<T> getleft(){ return left; }
	void setleft(T l){ left = l; }
	BinaryNode<T> getright(){ return right; }
	void setright(T l){ right = r; }
	BinaryNode<T> getparent(){ return parent; }
	void setparent(T p){ parent = p; }

};


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
1>c:\users\davide\dropbox\davide\[asd]\progetto\[asd]vocabolario\[asd]vocabolario\classi.h(20): error C2440: '=': impossibile convertire da 'std::string' a 'BinaryNode<T> *'
1>          with
1>          [
1>              T=std::string
1>          ]
1>          Nessun operatore di conversione definito dall'utente è disponibile o è impossibile chiamare l'operatore
1>          c:\users\davide\dropbox\davide\[asd]\progetto\[asd]vocabolario\[asd]vocabolario\classi.h(20): durante la compilazione della classe modello, funzione membro 'void BinaryNode<T>::setleft(T)'
1>          with
1>          [
1>              T=std::string
1>          ]
1>          c:\users\davide\dropbox\davide\[asd]\progetto\[asd]vocabolario\[asd]vocabolario\classi.h(91): vedere il riferimento all'istanza del modello di funzione 'void BinaryNode<T>::setleft(T)' in corso di compilazione
1>          with
1>          [
1>              T=std::string
1>          ]
1>          c:\users\davide\dropbox\davide\[asd]\progetto\[asd]vocabolario\[asd]vocabolario\classi.h(241): vedere il riferimento all'istanza del modello di classe 'BinaryNode<T>' in corso di compilazione
1>          with
1>          [
1>              T=std::string
1>          ]
1>          c:\users\davide\dropbox\davide\[asd]\progetto\[asd]vocabolario\[asd]vocabolario\classi.h(237): durante la compilazione della classe modello, funzione membro 'BinaryNode<T> *BinarySearchTree<T>::remove(BinaryNode<T> *)'
1>          with
1>          [
1>              T=std::string
1>          ]
1>          c:\users\davide\dropbox\davide\[asd]\progetto\[asd]vocabolario\[asd]vocabolario\main.cpp(34): vedere il riferimento all'istanza del modello di funzione 'BinaryNode<T> *BinarySearchTree<T>::remove(BinaryNode<T> *)' in corso di compilazione
1>          with
1>          [
1>              T=std::string
1>          ]
1>          c:\users\davide\dropbox\davide\[asd]\progetto\[asd]vocabolario\[asd]vocabolario\main.cpp(9): vedere il riferimento all'istanza del modello di classe 'BinarySearchTree<std::string>' in corso di compilazione
========== Compilazione: 0 completate, 1 non riuscite, 0 aggiornate, 0 ignorate ==========


This is the main.cpp
https://www.dropbox.com/s/dgh5pwvsd6jozpw/main.cpp
Last edited on
Ok, i edited all code...
Getkey and Setkey works...
Only Getleft and Setlefts not work!!! :(
(left right and parent, all pointer....)
> class BinarySearchTree :public BinaryNode
that doesn't look right.


Provide a testcase http://www.eelis.net/iso-c++/testcase.xhtml (don't worry about points 8, 9; but take especial consideration in point 6)

About the error
void setleft(T l){ left = l; }
`left' is not `T', but `BinaryNode<T>*'

> If you need the interface of your class to allow other classes
> to either get the values of those members, or set them,
Note the emphasis.
http://www.javaworld.com/article/2073723/core-java/why-getter-and-setter-methods-are-evil.html


1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename T>
class BinarySearchTree
{
private:
   struct BinaryNode
   {
      T key;
      BinaryNode<T>* left;
      BinaryNode<T>* right;
      BinaryNode<T>* parent;
   };

   BinaryNode* root;
Ok for setleft (right and parent) it's ok now works,
but for getlefts? it's not works...
It's right this?
BinaryNode<T>* getleft(){ return left; }
(same error)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template <typename T>
void BinarySearchTree<T>::insert(BinaryNode<T>** node, T key)
{
	if (*node == NULL)
	{
		*node = this->newNode(key);
		cout << "-Creating... " << (*node)->getkey() << endl << endl;
	}
	else
	{
		if (key <= (*node)->getkey())
		{
			cout << "<-Left" << endl;
			insert(&(*node)->getleft(), key); //not working getleft() same error
			(*node)->getleft()->parent = *node;
		}
		else
		{
			cout << "Right->" << endl;
			insert(&(*node)->getright(), key); //not working getright() same error
			(*node)->getright()->parent = *node;
		}
	}
}
Last edited on
What error?
1>------ Inizio compilazione: Progetto: [ASD]Vocabolario, Configurazione: Debug Win32 ------
1> main.cpp
1>c:\users\davide\dropbox\davide\[asd]\progetto\[asd]vocabolario\[asd]vocabolario\classi.h(124): error C2102: '&' richiede un l-value
1> c:\users\davide\dropbox\davide\[asd]\progetto\[asd]vocabolario\[asd]vocabolario\classi.h(113): durante la compilazione della classe modello, funzione membro 'void BinarySearchTree<std::string>::insert(BinaryNode<T> **,T)'
1> with
1> [
1> T=std::string
1> ]
1> c:\users\davide\dropbox\davide\[asd]\progetto\[asd]vocabolario\[asd]vocabolario\classi.h(104): vedere il riferimento all'istanza del modello di funzione 'void BinarySearchTree<std::string>::insert(BinaryNode<T> **,T)' in corso di compilazione
1> with
1> [
1> T=std::string
1> ]
1> c:\users\davide\dropbox\davide\[asd]\progetto\[asd]vocabolario\[asd]vocabolario\classi.h(103): durante la compilazione della classe modello, funzione membro 'void BinarySearchTree<std::string>::insert(T)'
1> with
1> [
1> T=std::string
1> ]
1> c:\users\davide\dropbox\davide\[asd]\progetto\[asd]vocabolario\[asd]vocabolario\main.cpp(25): vedere il riferimento all'istanza del modello di funzione 'void BinarySearchTree<std::string>::insert(T)' in corso di compilazione
1> with
1> [
1> T=std::string
1> ]
1> c:\users\davide\dropbox\davide\[asd]\progetto\[asd]vocabolario\[asd]vocabolario\main.cpp(9): vedere il riferimento all'istanza del modello di classe 'BinarySearchTree<std::string>' in corso di compilazione
1>c:\users\davide\dropbox\davide\[asd]\progetto\[asd]vocabolario\[asd]vocabolario\classi.h(130): error C2102: '&' richiede un l-value
========== Compilazione: 0 completate, 1 non riuscite, 0 aggiornate, 0 ignorate ==========


Only this, because this works:
(*node)->getright()->parent = *node;
Last edited on
Pages: 12