Building my own Set, queue and driver

Well, I'm trying to build a set based on an AVL tree. So far, I have a set based on a binary search tree. The set and the queue compile just fine, but when I try to compile main.cpp, things get difficult. Also, I know it's bad practice to code the functions within the .h files, but please forgive me on that:

Here's what the compiler says:
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
g++ main.cpp
In file included from main.cpp:7:0:
Set.h: In instantiation of ‘Set<ItemType>::Node* Set<ItemType>::insert(Set<ItemType>::Node*, ItemType) [with ItemType = std::basic_string<char>]’:
main.cpp:16:37:   required from here
Set.h:19:9: error: ‘Set<ItemType>::Node::Node(ItemType) [with ItemType = std::basic_string<char>]’ is private
Set.h:56:35: error: within this context
Set.h:15:18: error: ‘std::basic_string<char> Set<std::basic_string<char> >::Node::item’ is private
Set.h:57:8: error: within this context
Set.h:16:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::left’ is private
Set.h:57:27: error: within this context
Set.h:16:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::left’ is private
Set.h:57:27: error: within this context
Set.h:15:18: error: ‘std::basic_string<char> Set<std::basic_string<char> >::Node::item’ is private
Set.h:58:8: error: within this context
Set.h:17:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::right’ is private
Set.h:58:26: error: within this context
Set.h:58:26: error: invalid operands of types ‘Set<std::basic_string<char> >::Node*’ and ‘std::ios_base&(std::ios_base&)’ to binary ‘operator-’
Set.h: In instantiation of ‘void Set<ItemType>::print(Set<ItemType>::Node*, std::ofstream&, int) [with ItemType = std::basic_string<char>; std::ofstream = std::basic_ofstream<char>]’:
main.cpp:45:41:   required from here
Set.h:121:5: error: no matching function for call to ‘Queue<std::basic_string<char> >::append(Set<std::basic_string<char> >::Node*&)’
Set.h:121:5: note: candidate is:
In file included from Set.h:6:0,
                 from main.cpp:7:
queue.h:42:7: note: void Queue<ItemType>::append(ItemType) [with ItemType = std::basic_string<char>]
queue.h:42:7: note:   no known conversion for argument 1 from ‘Set<std::basic_string<char> >::Node*’ to ‘std::basic_string<char>’
In file included from main.cpp:7:0:
Set.h:124:4: error: cannot convert ‘std::basic_string<char>’ to ‘Set<std::basic_string<char> >::Node*’ in assignment
Set.h:15:18: error: ‘std::basic_string<char> Set<std::basic_string<char> >::Node::item’ is private
Set.h:125:4: error: within this context
Set.h:16:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::left’ is private
Set.h:126:4: error: within this context
Set.h:16:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::left’ is private
Set.h:126:35: error: within this context
Set.h:126:35: error: no matching function for call to ‘Queue<std::basic_string<char> >::append(Set<std::basic_string<char> >::Node*&)’
Set.h:126:35: note: candidate is:
In file included from Set.h:6:0,
                 from main.cpp:7:
queue.h:42:7: note: void Queue<ItemType>::append(ItemType) [with ItemType = std::basic_string<char>]
queue.h:42:7: note:   no known conversion for argument 1 from ‘Set<std::basic_string<char> >::Node*’ to ‘std::basic_string<char>’
In file included from main.cpp:7:0:
Set.h:17:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::right’ is private
Set.h:127:4: error: within this context
Set.h:17:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::right’ is private
Set.h:127:36: error: within this context
Set.h:127:36: error: no matching function for call to ‘Queue<std::basic_string<char> >::append(Set<std::basic_string<char> >::Node*&)’
Set.h:127:36: note: candidate is:
In file included from Set.h:6:0,
                 from main.cpp:7:
queue.h:42:7: note: void Queue<ItemType>::append(ItemType) [with ItemType = std::basic_string<char>]
queue.h:42:7: note:   no known conversion for argument 1 from ‘Set<std::basic_string<char> >::Node*’ to ‘std::basic_string<char>’
In file included from main.cpp:7:0:
Set.h: In instantiation of ‘Set<ItemType>::~Set() [with ItemType = std::basic_string<char>]’:
main.cpp:64:11:   required from here
Set.h:45:3: error: ‘koolaid’ was not declared in this scope
Set.h: In instantiation of ‘Set<ItemType>::Node* Set<ItemType>::remove(Set<ItemType>::Node*, const ItemType&) [with ItemType = std::basic_string<char>]’:
Set.h:64:3:   required from ‘void Set<ItemType>::remove(const ItemType&) [with ItemType = std::basic_string<char>]’
main.cpp:25:22:   required from here
Set.h:15:18: error: ‘std::basic_string<char> Set<std::basic_string<char> >::Node::item’ is private
Set.h:72:8: error: within this context
Set.h:16:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::left’ is private
Set.h:73:4: error: within this context
Set.h:16:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::left’ is private
Set.h:73:4: error: within this context
Set.h:15:18: error: ‘std::basic_string<char> Set<std::basic_string<char> >::Node::item’ is private
Set.h:75:8: error: within this context
Set.h:17:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::right’ is private
Set.h:76:4: error: within this context
Set.h:17:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::right’ is private
Set.h:76:4: error: within this context
Set.h:16:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::left’ is private
Set.h:78:8: error: within this context
Set.h:17:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::right’ is private
Set.h:79:14: error: within this context
Set.h:17:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::right’ is private
Set.h:81:8: error: within this context
Set.h:16:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::left’ is private
Set.h:82:14: error: within this context
Set.h:17:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::right’ is private
Set.h:85:4: error: within this context
Set.h:15:18: error: ‘std::basic_string<char> Set<std::basic_string<char> >::Node::item’ is private
Set.h:85:4: error: within this context
Set.h:85:4: error: invalid conversion from ‘Set<std::basic_string<char> >::Node*’ to ‘char’ [-fpermissive]
In file included from /usr/include/c++/4.7/string:54:0,
                 from /usr/include/c++/4.7/bits/locale_classes.h:42,
                 from /usr/include/c++/4.7/bits/ios_base.h:43,
                 from /usr/include/c++/4.7/ios:43,
                 from /usr/include/c++/4.7/ostream:40,
                 from /usr/include/c++/4.7/iostream:40,
                 from main.cpp:1:
/usr/include/c++/4.7/bits/basic_string.h:562:7: error:   initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]’ [-fpermissive]
In file included from main.cpp:7:0:
Set.h:17:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::right’ is private
Set.h:86:4: error: within this context
Set.h:15:18: error: ‘std::basic_string<char> Set<std::basic_string<char> >::Node::item’ is private

I'll continue the output in the next post.

Man, this site has some strict post size requirements, considering that it's for code. I'll post my files upon request.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Set.h:86:4: error: within this context
Set.h:17:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::right’ is private
Set.h:86:4: error: within this context
Set.h: In instantiation of ‘bool Set<ItemType>::find(Set<ItemType>::Node*, const ItemType&) const [with ItemType = std::basic_string<char>]’:
Set.h:100:25:   required from ‘bool Set<ItemType>::find(const ItemType&) const [with ItemType = std::basic_string<char>]’
main.cpp:35:37:   required from here
Set.h:15:18: error: ‘std::basic_string<char> Set<std::basic_string<char> >::Node::item’ is private
Set.h:108:8: error: within this context
Set.h:16:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::left’ is private
Set.h:109:29: error: within this context
Set.h:15:18: error: ‘std::basic_string<char> Set<std::basic_string<char> >::Node::item’ is private
Set.h:111:8: error: within this context
Set.h:17:15: error: ‘Set<std::basic_string<char> >::Node* Set<std::basic_string<char> >::Node::right’ is private
Set.h:112:30: error: within this context


Sorry if this double-post is impolite! I just needed to get the whole output here.
Last edited on
Looks like you're trying to access private members from outside the class.

Can't say more without seeing code.
http://pastebin.com/2tK77k3N

Here's the code. I don't think I am actually doing that, because I went through the code and deleted all the private: labels just to make sure. I even replaced them with public: labels in some cases and I'm still getting this.
Last edited on
Quick question: Can I compare strings with > or < ?
Here's the code


Problems:

1) Everything in the Set::Node and Queue::Node classes are private because there is no public: marker. Classes default to private unless you explicitly put the public marker there.

2) Set's destructor is trying to call koolaid ... but koolaid is not a member of Set.

3) This line in set.h:
else if (item>t->item) t-right = insert(t->right, item); t-right is supposed to be t->right

4) In Set::print:
1
2
3
4
	void print(Node* currentNode, ofstream& output, int level)							//PRINT
	{
		Queue<ItemType>* printMe = new Queue<ItemType>();
  		printMe->append(currentNode);


'currentNode' is of type Node*
printMe->append()'s parameter is of type ItemType

You cannot convert a Node* to an ItemType. You probably meant to do append(currentNode->item);

5) Just below that you have a similar problem:

currentNode = printMe->pop();

pop() returns an ItemType, not a Node*. Probably meant to do:
currentNode->item = printMe->pop();

6) Just below that... same problem as #4 two more times:
1
2
			if (currentNode->left != NULL) printMe->append(currentNode->left);
			if (currentNode->right != NULL) printMe->append(currentNode->right);


7) Similar type confusion in Set::remove:

t->item = findMin(t->right);
t->item is a ItemType but findMin returns a Node*.
Did you mean t = findMin(t->right);?


8) Set::balance has to return a value:

1
2
	Node* balance(Node* t)
	{} // <- you're not returning anything 






----------------------

After all those changes... it compiles.


TIPS:
-------------------------------------

1) Name your functions appropriately. Getting cute makes it harder for people to understand what's going on. "koolaid" is a terrible, terrible function name.

2) Compile more often and solve these problems as they come up. Don't try to code everything blind (going a long time without compiling) or else you'll end up with what happened here: a page of errors and you get overwhelmed.

3) A good IDE will tell you exactly what line the error is on. Most let you double click right on the error message and they'll jump you right to the line that has the problem. With that you probably could have been able to figure all these out yourself.

4) There is no reason for the 'koolaid' function to exist. It destroys the object... so just do that stuff in the destructor. Outside code should not have to call that function... if it's in the destructor it will run automatically when the node is destroyed.


Can I compare strings with > or < ?


Yes.
Last edited on
Thanks a ton! Great advice. I admit, the Kool-aid function was a bit of a bored pleasure of mine ;) I had recently rewritten all this code to incorporate some code given in class, so that's why I wasn't compiling as I went. All the same, good advice. As for the IDE, it may be a good idea for me to start using something other than gedit... lol :D

I can't remember why I put the kool-aid function there either, but all the same...

http://www.youtube.com/watch?v=2iE4uEsaBF0


Thanks for the help!
Topic archived. No new replies allowed.