-1

There is two files one is .cpp file another is .h file , header file

This class is present in .h file

class DecodeTheCode
{
public:
char* decodeCode(char* encodedString);
};

now software which i am using asks

•Implement your logic in function char* decodeCode(char* encodedString)

but i have declared

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;

}

in .cpp file

now the software is showing

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

collect2: ld returned 1 exit status

/decodethecode/decodethecode.c: In function `char* decodeCode(const char*)':
/decodethecode/decodethecode.c:103: error: invalid conversion from `const char*' to `char*'

i am using const char* decodeCode(const char* encodedString) in place of char* decodeCode(char* encodedString) because otherwise i gets deprecated conversion from string constant to ‘char*’</p>

Have you any idea how I could fix it? If so, please write it down clearly (I am newbie...).

4

3 回答 3

6

您需要完全限定定义并更改声明中的类型以匹配定义类型。在.cpp文件中,这只是定义了一个自由函数:

const char* decodeCode(const char* encodedString)
{
}

并且与成员函数声明无关。完全符合条件:

const char* DecodeTheCode::decodeCode(const char* encodedString)
{
}

更改decodeCode()也声明中的类型。

更重要的是,您将返回一个指向名为 的局部变量的指针store。这将超出范围并给调用者留下一个悬空指针。要返回,char*您需要使用动态分配内存,new char[size]并且调用者必须记住delete[]返回的数据。const char*如果返回动态分配的内存,则返回类型不需要更改。但是您必须保持一致,特别是不要在偶尔返回字符串文字时,例如"",而在另一个返回动态分配的内存时,调用者将尝试delete[]存储它不应该的内存。

如果可能,请使用std::string而不是char*. 它消除了您管理动态分配内存的负担。

于 2012-09-04T08:58:55.163 回答
1

将函数声明从 更改const char* decodeCode(const char* encodedString) {const char* DecodeTheCode::decodeCode(const char* encodedString) {。该DecodeTheCode::部分告诉编译器“decodeCode()”函数是“DecodeTheCode”类的一部分,而不是某个其他类甚至全局的一部分。

于 2012-09-04T09:03:47.593 回答
0

改变

const char* decodeCode(const char* encodedString)

这样看:

char* DecodeTheCode::decodeCode(char* encodedString)

这样,它是一个类方法定义,而不仅仅是新代码中的新函数声明和定义。

我相信,从字符串常量到 'char*' 的弃用转换来自您的方法调用,而不是您声明它的方式。您可以展示如何使用此方法,以便我确定如何解决此问题。

于 2012-09-04T09:06:07.210 回答