我正在編寫自己的append函數,以便使用靜態字符緩衝區[50]在字符串數組1的另一個動態字符數組的末尾附加一個字符串數組2的動態字符數組。但是,編譯器會生成以下錯誤:[錯誤]將'char'賦值給'char [50]'時不兼容的類型。我試圖找出問題所在,但我似乎找不到解決方案。您的幫助將非常感激。我正在使用Dev-C++。代碼是波紋管。我的追加功能不能按預期工作。 C++
#include <iostream>
using namespace std;
char *Appendstring(char *a, char *b) // will append b to the end of a
{
static char buffer[50];
char *p=buffer=*a++; //[Error] incompatible types in assignment of 'char' to 'char[50]'
//[Error] invalid conversion from 'char*' to 'char'[-fpermissive]
p--;
while(*p++=b++);
p--; //append
while(*p++=*c++);
return buffer;
}
int main()
{
string str="Displaying: ";
string add=" Summer is coming";
Appendstring(str, add);
return 0;
}
好的,但爲什麼不使用'運算符+ ='爲'std :: string's? – 2013-05-03 15:24:10
重新實現字符串類及其成員是通過C或C++等低級語言學習自己的方式的常用方式。我沒有看到指出爲了解決問題而存在描述任務的操作員的相關性。 – 2013-05-03 15:40:59