user1858120
user1858120

Reputation: 19

Constructors and operators in c++ - not working

I'm trying to learn C++ and I don't understand why the following code is not working:

class String
{
public:
    String();
    String(const String& other);
    String& operator = (const String& other);
    String& operator = (const wchar_t* other);
    String& operator () (const wchar_t* other);
    ~String();
    operator const wchar_t* ();
            ...

Somewhere in the main function:

wchar_t* x = L"A test string";
String y = (String)x; //not working
String z = x;  //not working

The VC++ compiler tells me this:

Error   1   error C2440: 'type cast': cannot convert from 'wchar_t *' to 'String'   
Error   2   error C2440: 'initializing': cannot convert from 'wchar_t *' to 'String'    
IntelliSense: no suitable constructor exists to convert from "wchar_t *" to "String"

What am I doing wrong?

Upvotes: 1

Views: 160

Answers (2)

James Kanze
James Kanze

Reputation: 153899

None of the three lines "somewhere in main" use assignment, so we can ignore any assignment operators you might have defined. And you haven't defined a converting constructor, which takes a single argument (a wchar_t const*), to convert your wchar_t const*.

Upvotes: 3

John Kugelman
John Kugelman

Reputation: 361546

You need a constructor for wchar_t*.

String(const wchar_t*);

Upvotes: 6

Related Questions