use*_*595 -1 c++ compiler-errors header-files
有两个文件,一个是.cpp文件,另一个是.h文件,头文件
该类存在于.h文件中
class DecodeTheCode
{
public:
char* decodeCode(char* encodedString);
};
Run Code Online (Sandbox Code Playgroud)
现在我正在使用的软件问
•在函数char*decodeCode(char*encodedString)中实现逻辑
但我已宣布
const char* decodeCode(const char* encodedString)
{
const char* decodedString = "";
const char* a = encodedString;
char store[10000];
for(int j=0;j<strlen(a);j++)
{
if (isdigit(a[j]) || a[j] == '#')
continue;
else return "";
}
int i = 0,k=0;
while (i < strlen(a))
{
if (a[i] == '#') {i++; continue;}
else if(a[i] == a[i+1] && a[i+1] == a[i+2] && a[i+2] == a[i+3])
{
store[k++] = four(a[i]);
i += 4;
}
else if (a[i] == a[i+1] && a[i+1] == a[i+2])
{
store[k++] = three(a[i]);
i += 3;
}
else if (a[i] == a[i+1])
{
store[k++] = two(a[i]);
i += 2;
}
else
{
store[k++] = one(a[i]);
i++;
}
}
store[k]='\0';
decodedString=store; // line number 103
return decodedString;
Run Code Online (Sandbox Code Playgroud)
}
在.cpp文件中
现在软件正在显示
Error(s) encountered:
C:/Users/ADMINI~1/AppData/Loca/Temp/cco5baaa.o(.text+0x18f):TestDecodeTheCode.cpp:undefined reference to
DecodeTheCode::decodeCode(char*)'
C:/Users/ADMINI~1/AppData/Local/Temp/cco5baaa.o(.text+0x1a4):TestDecodeTheCode.cpp: undefined reference to `DecodeTheCode::decodeCode(char*)'
C:/Users/ADMINI~1/AppData/Local/Temp/cco5baaa.o(.text+0x24d):TestDecodeTheCode.cpp: undefined reference to `DecodeTheCode::decodeCode(char*)'
C:/Users/ADMINI~1/AppData/Local/Temp/cco5baaa.o(.text+0x262):TestDecodeTheCode.cpp: undefined reference to `DecodeTheCode::decodeCode(char*)'
C:/Users/ADMINI~1/AppData/Local/Temp/cco5baaa.o(.text+0x30b):TestDecodeTheCode.cpp: undefined reference to `DecodeTheCode::decodeCode(char*)'
C:/Users/ADMINI~1/AppData/Local/Temp/cco5baaa.o(.text+0x320):TestDecodeTheCode.cpp: more undefined references to `DecodeTheCode::decodeCode(char*)' follow
Run Code Online (Sandbox Code Playgroud)
collect2:ld返回1退出状态
/decodethecode/decodethecode.c: In function `char* decodeCode(const char*)':
/decodethecode/decodethecode.c:103: error: invalid conversion from `const char*' to `char*'
Run Code Online (Sandbox Code Playgroud)
我使用const char*decodeCode(const char*encodedString)代替char*decodeCode(char*encodedString),因为否则我将从字符串常量转换为'char*'
你知道我怎么解决它吗?如果是这样,请清楚地写下来(我是新手......).
您需要完全限定定义并更改声明中的类型以匹配定义类型.在.cpp文件中,这只是定义一个自由函数:
const char* decodeCode(const char* encodedString)
{
}
Run Code Online (Sandbox Code Playgroud)
并且与成员函数声明无关.完全符合资格:
const char* DecodeTheCode::decodeCode(const char* encodedString)
{
}
Run Code Online (Sandbox Code Playgroud)
也改变声明中的类型decodeCode().
更重要的是,您将返回一个指向名为的局部变量的指针store.这将超出范围并使调用者留下悬空指针.要返回a,char*您需要动态分配内存使用new char[size]和调用者必须记住delete[]返回的数据.const char*如果返回动态分配的内存,则返回类型不需要更改为.但是你必须保持一致,特别是不要在occassion上返回一个字符串文字,""然后在另一个返回动态分配的内存,否则调用者将尝试delete[]记忆它不应该.
如果可能,请使用std::string而不是char*.它消除了管理动态分配的内存的负担.