Jump to content
  • Advertisement
Sign in to follow this  
atonalpanic

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by fpsgamer
Quote:
Original post by F1N1TY
Read this regarding children: http://aszt.inf.elte.hu/~gsd/halado_cpp/ch05s04.html


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 this post


Link to post
Share on other sites
Quote:
Original post by F1N1TY
Read this regarding children: http://aszt.inf.elte.hu/~gsd/halado_cpp/ch05s04.html


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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!