class myString新程序员

时间:2015-03-17 04:24:09

标签: c++ constructor

class mystring
{

public:

    mystring(const char x[])
        : capacity(1024)
    {
        for (int i = 0; i < capacity; ++i)
        {
            if (x[i] == '\0') break;
            s[i] = x[i];
            length++;
        }
    }
    mystring()
        : capacity(1024), s('\0'), length(0)
    {}
//etc...

private:

    const int capacity;
    char s[1024];
    int length;
};

我收到了这个错误:

In file included from main.cpp:19:0:
mystring.h: In constructor ‘mystring::mystring()’:
mystring.h:21:44: error: incompatible types in assignment of ‘char’ to ‘char [1024]’
         : capacity(1024), s('\0'), length(0)

我不知道发生了什么。我对施工人员有点新意。提前谢谢!

2 个答案:

答案 0 :(得分:1)

s('\0')更改为s("\0")

当您使用单引号时,它是一个单个字符。您必须使用双引号将其作为字符串进行测试。

答案 1 :(得分:0)

您现在收到的错误是因为您试图将char放入char[]

char[]是一个char数组,char本身只是一个char; 定期char[]定义为char str[] = "Test";

考虑一下并尝试修复代码! 希望这有帮助