我有一个来自 C++ 教科书的简单示例,它定义了一个 C 字符串,然后循环以用 X 替换所有不是 \0 字符的元素。
char ourString[5] = "Hi";
int index = 0;
while(ourString[index] != "\0")
{
ourString[index] = "X";
index++;
}
cout << ourString[] << endl;
但是,当我编译并运行时,出现以下错误:
C:\Users\Ben\Dropbox\Learning Programming\C++\Strings\Strings.cpp||In function 'int main()':|
C:\Users\Ben\Dropbox\Learning Programming\C++\Strings\Strings.cpp|17|error: ISO C++ forbids comparison between pointer and integer [-fpermissive]|
C:\Users\Ben\Dropbox\Learning Programming\C++\Strings\Strings.cpp|19|error: invalid conversion from 'const char*' to 'char' [-fpermissive]|
C:\Users\Ben\Dropbox\Learning Programming\C++\Strings\Strings.cpp|23|error: expected primary-expression before ']' token|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 0 seconds) ===|
我不明白为什么它不让我(据我了解错误)将作为字符的元素与字符 \0 进行比较的问题。我不认为我正在尝试将指针与整数进行比较......