A
arnuld
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;
my_tree->word = NULL;
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>:perator=(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>:perator=(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>:perator=(_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.
/* 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;
my_tree->word = NULL;
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>:perator=(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>:perator=(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>:perator=(_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.