Salt_Peter
11/7/2008 6:19:00 AM
On Nov 7, 12:46 am, arnuld <sunr...@invalid.address> wrote:
> Problem Statement is in the comments. I am getting compile time error:
>
> /* Stroustrup, Chapter 7, exercise 7
> *
> * Consider:
> * struct Tnode {
> * string word;
> * int count;
> * Tnode* left;
> * Tnode* right;
> * };
> *
> *
> * (1) Write a function for entering new words into a tree of Tnodes.
> * (2) Write a function to write out a tree of Tnodes.
> * (3) Write a function to write out a tree of Tnodes in alphabetical order.
> * (4) Modify Tnode so that it stores (only) a pointer to an arbitrary long
> * word stored as an array of characters on free store using new.
> * (5) Modify the functions to use the new definition of Tnode.
> *
> * Right now, I am at (1), will do it and only then will go on (2).
> *
> *
> * VERSION 1.0
> *
> */
>
> #include <iostream>
> #include <string>
>
> struct Tnode
> {
> std::string word;
> int count;
> Tnode* left;
> Tnode* right;
>
> };
>
> void add_some_words(Tnode* );
>
> int main()
> {
> Tnode* my_tree;
At this point you have a pointer my_tree, it *could* potentially point
to something.
In fact, it does point somewhere, some garbage/residual value.
A pointer does not invoke construction.
So far - there *is* no Tnode at that pointer.
Can you see why naming a pointer as such is helpful?
You could allocate it and later destroy the object using new / delete.
At least then you would have an object *at* that pointer to work with.
Tnode* p_node = new Tnode;
// do stuff
delete p_node;
or you might simply do it all without manual allocation / deallocation
and just pass the address of the object using the address_of operator
&.
Tnode node;
Tnode* p_node = &node;
// do stuff
>
> my_tree->word = NULL;
member word is a std::string, not a pointer
my_tree->word = "";
> my_tree->count = 0;
> my_tree->left = NULL;
> my_tree->right = NULL;
>
> add_some_words( my_tree );
>
> return 0;
>
> }
>
> void add_some_words( Tnode* add_to_tree )
> {
> std::cout << "Please enter some words that you want to add to the Tree\n";
> /* I want to use istream_iterator but can't find a way for 3rd argument to
> std::copy */
> /* std::copy( std::istream_iterator<std::string>(std::cin),
> std::istream_iterator<std::string(),
> ??????? );
> */
> std::string a_word;
> while( std::cin >> a_word )
> {
> if( NULL == add_to_tree->word )
> {
> add_to_tree->word = a_word;
> add_to_tree->count++;
> }
> else if( NULL == add_to_tree->left )
> {
> add_to_tree = add_to_tree->left;
> add_to_tree->word = a_word;
> add_to_tree->count++;
> add_to_tree->left = NULL;
> add_to_tree->right = NULL;
> }
> else if( NULL == add_to_tree->right )
> {
> add_to_tree->word = a_word;
> add_to_tree->count++;
> add_to_tree->left = NULL;
> add_to_tree->right = NULL;
> }
> }
>
> }
>
> ======================= OUTPUT ==============================
> [arnuld@dune cpp]$ g++4 -ansi -pedantic -Wall -Wextra 07_7.cpp
> 07_7.cpp: In function ‘int main()’:
> 07_7.cpp:47: error: ambiguous overload for ‘operator=’ in ‘my_tree->Tnode::word = 0’
> /usr/include/c++/3.4.3/bits/basic_string.h:434: note: candidates are:
> std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT,
> _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits,
> _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc =
> std::allocator<char>]
> /usr/include/c++/3.4.3/bits/basic_string.h:445: note:
> std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT,
> _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char, _Traits =
> std::char_traits<char>, _Alloc = std::allocator<char>]
> /usr/include/c++/3.4.3/bits/basic_string.h:459: note:
> 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>] 07_7.cpp: In
> function ‘void add_some_words(Tnode*)’: 07_7.cpp:72: error: no match
> for ‘operator==’ in ‘0 == add_to_tree->Tnode::word’
> [arnuld@dunecpp]$
>
> Line 47: my_tree->word = NULL;
>
> I don't see why it complains on that line. my_tree->word is a std::string
> and we use operator= for std::string all the time.
If you read the error, its complaining that its not able to use
that operator with an rvalue that is a pointer* .
so this would do:
std::string* p_s = 0; // points to nothing yet
and this would also:
std::string s = "";
Don't get discouraged, you need to detect the crucial difference
between
an instance of a type and a pointer.
A pointer points to nothing until properly initialized.