Public Group

# [solved]array of auto_ptrs (no match for operator)

This topic is 3657 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I was trying to build my word gatherer, the trie part of it when I came across this error:
7 trie\trieNode.cpp no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(+(&std::cout)->std::basic_ostream<_CharT, _Traits>::operator<< [with _CharT = char, _Traits = std::char_traits<char>](i))), ((const char*)" ")) << ((trieNode*)this)->trieNode::children'
note C:\Dev-Cpp\include\c++\3.4.2\bits\ostream.tcc:63 candidates are: std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]


The header and implementation look like this:
#include <string>
#include <vector>
#include <iostream>
#include <memory>

class trieNode {
public:

bool word;

void insert( std::string word );
private:
std::auto_ptr< trieNode > children[ 26 ];
};


#include "trieNode.h"

void trieNode::insert( std::string word ){

std::cout << "Insert Called\n";
for ( unsigned int i = 0 ; i < 26 ; ++i )
std::cout << i << " " << children << std::endl;

std::cout << word << "   " << word[ 0 ] - 97 << "   " << word.length() << std::endl;
if ( word.length( ) == 1 ) word = true;
else {
std::cout << "works before if     " << children[ 0 ] << std::endl;
if ( children[ word[ 0 ] - 97 ] == NULL ) {
std::cout <<"works till if" << std::endl;
children[ word[ 0 ] - 97 ] = new trieNode();

}
std::cout <<"works after if " << children[ word[ 0 ] - 97 ] << std::endl;
children[ word[ 0 ] - 97 ]->insert( word.erase( 0 , 1 ) );

}

std::cout <<"returned" << std::endl;

return;
}


I'm confused as can be here, whats a good way to output the auto_ptr? [Edited by - atonalpanic on June 14, 2008 2:28:56 PM]

##### Share on other sites
I should also point out that anything with the children[] array doesn't work, including assignment.

##### Share on other sites
First of all you cant just inset an std::auto_ptr into a stream. If you want to see the value of the pointer then do this:

std::cout << myAutoPtr.get() << std::endl;

If the object to which your std::auto_ptr points to can be inserted into a stream then dereference it:

std::cout << *myAutoPtr << std::endl;

The bigger problem I'm afraid is that std::auto_ptr doesn't work the way you think it does.

It looks like you meant to use either boost::shared_ptr or boost::scoped_ptr.

##### Share on other sites
Quote:
Original post by fpsgamer
Quote:

I have no clue what the example titled "Arrays are not Polymorphic" is talking about. I don't see anything wrong with that code (If I missed something please let me know!)

:)

I should probably pay more attention to what the OP posted, and possibly read the google search result :-P

Sorry :-D

##### Share on other sites
Quote:

I don't see how that page is relevant. The OP has an array of std::auto_ptrs, not a std::auto_ptr holding a pointer to a heap allocated array.

What made me further lose confidence in that source is the fact that I have no clue what the example titled "Arrays are not Polymorphic" is talking about. I don't see anything wrong with that code (If I missed something please let me know!)

:)

##### Share on other sites
Now that there are conflicting opinions on this I would like more insight. I have updated the code to reflect the get() function like so:

#include "trieNode.h"void trieNode::insert( std::string word ){    std::cout << "Insert Called\n";  for ( unsigned int i = 0 ; i < 26 ; ++i )    std::cout << i << " " << children.get() << std::endl;    std::cout << word << "   " << word[ 0 ] - 97 << "   " << word.length() << std::endl;  if ( word.length( ) == 1 ) word = true;  else {    std::cout << "works before if     " << children[ 0 ].get() << std::endl;    if ( children[ word[ 0 ] - 97 ].get() == NULL ) {      std::cout <<"works till if" << std::endl;      children[ word[ 0 ] - 97 ] = new trieNode();          }    std::cout <<"works after if " << children[ word[ 0 ] - 97 ].get() << std::endl;    children[ word[ 0 ] - 97 ]->insert( word.erase( 0 , 1 ) );      }    std::cout <<"returned" << std::endl;    return;}

witch takes care of most of my errors. However, the big error still remains:
15 trie\trieNode.cpp no match for 'operator=' in '((trieNode*)this)->trieNode::children[(((int)(&word)->std::basic_string<_CharT, _Traits, _Alloc>::operator[] [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](0u)) - 97)] = (((trieNode*)operator new(108u)), (<anonymous>->trieNode::trieNode(), <anonymous>))'

But my understanding is that auto_ptr does have a operator= function.

• 38
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631362
• Total Posts
2999577
×