我想要一种具有固定最大长度的 cstring,比如说 5。如果可能的话,我还希望能够使用标准cstring
函数。然而,有了这个,我在函数参数方面遇到了很多问题。我对 typedef 还不是很熟悉,但我不确定这是否是错误的方法,或者我只是错过了一些东西。C++ (mingw-w64)
typedef char Name[5];
std::ostream& operator<<(std::ostream& os, Name& car)
{
os << car;
return os;
}
void Change(Name str)
{
str[2] = '0';
}
Name ChangeRtn(Name str)
{
str[2] = '1';
return str;
}
void ChangeCat(Name * strp)
{
strcat(strp,"3");
}
int main ()
{
Name test = "abcd";
Change(test);
std::cout << test << std::endl;
Name test2 = ChangeRtn(test);
std::cout << test2 << std::endl;
Name * test3;
//*test3 = "12"; //incompatible types... make a operator= func
strcpy (*test3,"12"); //a temp replacement of operater=?
ChangeCat(test3);
std::cout << test3 << std::endl;
return 0;
}
返回错误...
typedef.cpp:15:24: error: 'ChangeRtn' declared as function returning an array
Name ChangeRtn(Name str)
^
typedef.cpp: In function 'void ChangeCat(char (*)[5])':
typedef.cpp:22:17: error: cannot convert 'char (*)[5]' to 'char*' for argument '1' to 'char* strcat(char*, const char*)'
strcat(strp,"3");
^
typedef.cpp: In function 'int main()':
typedef.cpp:30:29: error: 'ChangeRtn' was not declared in this scope
Name test2 = ChangeRtn(test);
^