簡體   English   中英

C ++中的構造方法和運算符-不起作用

[英]Constructors and operators in c++ - not working

我正在嘗試學習C ++,但我不明白為什么以下代碼無法正常工作:

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* ();
            ...

主要功能中的某處:

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

VC ++編譯器告訴我:

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"

我究竟做錯了什么?

您需要wchar_t*的構造函數。

String(const wchar_t*);

“主目錄中的某處”三行均未使用賦值,因此我們可以忽略您可能已定義的任何賦值運算符。 而且您還沒有定義一個轉換構造函數,該構造函數只接受一個參數( wchar_t const* )來轉換wchar_t const*

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM