我在一个 Solaris 10 盒子上。我使用的编译器是:/opt/solstudio12.2/bin/CC
编译如下所示的文件 (myTest2.C) 失败。
% CC -I. -o myTest2 myTest2.C
"myTest2.C", line 30: Error: "{" expected instead of "myFunc".
"myTest2.C", line 33: Error: "{" expected instead of "myFunc".
2 Error(s) detected.
- 为什么在声明变量时调用 myFunc() 会出错?
- 是因为它不在任何功能范围内吗?
- 如何绕过它?
仅供参考,我正在编写一个字符串混淆器,它通过 C++ 代码行并用函数调用(例如:scrambleString("myString"))替换引用字符串的实例(例如:“myString”)。本例中的 myFunc 类似于 scrambleString
#include <iostream>
#include <string>
using namespace std;
char* myFunc(string inString)
{
char outString[1024];
int i;
for (i = 0; i <= inString.size()-1; i++)
outString[i] = inString[i];
outString[i+1] = '\0';
return outString;
}
// This Works:
static char myVariable1 [ ] = "MyString1" ;
// This Breaks:
static char myVariable2 [ ] = myFunc("MyString2") ; // Line #30
// This Breaks:
char myVariable5 [1024] = myFunc("MyString3"); // Line
#33
int main()
{
// This Works:
string Z = myFunc("Gdkkn Vnqkc");
cout << "Z = " << Z << endl << endl;
}