views:

304

answers:

3
+2  A: 

You need to provide a suitable conversion constructor:

 string_t(const std::basic_string<value_type>&);

Otherwise the compiler doesn't know how to construct a string_t from a std::basic_string<> when you're adding elements to the vector of string_ts.

Regarding the update:
A using declaration for operator=() doesn't help if it is private. Why not just implement your own operator=() instead and forward the assignment:

string_t& operator=(const string_t& s) 
{
    basic_string<value_type>::operator=(s); 
    return *this; 
}

With that it builds fine for me.

Georg Fritzsche
Praetorian
Why not give us a small compilable example that exhibits the problem then? Adding the conversion constructor makes the code compilable for me and doesn't show the first problem you're mentioning.
Georg Fritzsche
The conversion should NOT be private. He's getting problems because "conversion exists, but is inaccessible" ;v)
Potatoswatter
Aye, fixed.....
Georg Fritzsche
@gf - I've posted a compilable example that shows the error, as well as tracked down the line that's causing it.
Praetorian
A: 

Unfortunately, you've left out enough that it's uncertain how you're getting the error message you've noted (and it doesn't help that we don't know what was line 657 when you compiled it...).

The probably currently appears to be that substr() is returning an std::string, and the compiler isn't sure how to convert that to a string_t (which is what you're asking it to store in the vector). You probably need/want to add a ctor to handle this, something like:

string_t(std::string const &init) : std::string(init) {}

That lets the compiler know how to convert the string returned by substr() into a string_t that it can push into the vector as you requested.

Jerry Coffin
Praetorian
No, that accepts a `std::basic_string<value_type>` as a parameter, but fails to pass the parameter through to the base class.
Jerry Coffin
Praetorian
+3  A: 

Your default constructor should not be explicit. I think explicitness may be the reason it can't convert std::string to string_t as well, but you erased that construtor from your snippet :vP .

This program compiles and runs fine with GCC 4.2:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class string_t :
#if defined(UNICODE) || defined(_UNICODE)
  private std::basic_string<wchar_t>
#else
  private std::basic_string<char>
#endif
{
public:
  string_t() : basic_string<value_type>() {}

  string_t( const basic_string<value_type>& str )
    : basic_string<value_type>( str ) {}

  virtual ~string_t() {}

  using std::basic_string<value_type>::operator=; /* Line causing error */

  std::vector<string_t> split( const string_t& delims )
  {
    std::vector<string_t> tokens;

    for ( size_t pen = 0, next = 0; next != npos; pen = next + 1 ) {
        next = find_first_of( delims, pen );
        if ( pen != next ) tokens.push_back( substr( pen, next - pen ) );
    }
    return tokens;
  }

  template<class os>
  friend os &operator<<(os &, string_t const&);
};

template< class os_t >
os_t &operator<<( os_t &os, string_t const &str ) {
        return os << static_cast< string >(str);
}

int main( int argc, char ** argv ) {
        vector<string_t> mytoks = string_t( argv[1] ).split( string( "_" ) );

        for ( vector<string_t>::iterator it = mytoks.begin(); it != mytoks.end(); ++ it ) {
                cerr << * it << endl;
        }
        return 0;
}
Potatoswatter
I copy-pasted your code and tried to compile it but VS2008 gives me the same error!
Praetorian