2012-09-04 80 views
-1

有兩個文件一個是.cpp文件另一個是.h文件中,頭文件函數調用

這個類是目前在.h文件中

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

現在的軟件,我是使用要求

•實現功能的char你的邏輯* decodeCode(字符* 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; 

}

在.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 

collect2:LD返回1個退出狀態

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

我使用 爲const char * decodeCode(const char * encodedString) 代替 的char * decodeCode(字符* encodedString) ,否則我得到棄用從字符串常量「字符*」

你有什麼想法,我怎麼能修復它轉換?如果是這樣,請清楚寫下來(我是新手......)。

回答

6

您需要完全限定定義並更改聲明中的類型以匹配定義類型。在.cpp文件這只是定義了一個免費的功能:

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

,不與成員函數聲明有關。要完全符合條件:

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

也更改聲明decodeCode()的類型。

更重要的是,您正在返回一個指向名爲store的局部變量的指針。這將超出範圍,並留下一個懸掛指針的調用者。要返回char*,您需要使用new char[size]動態分配內存,並且調用者必須記得返回的數據爲delete[]。如果返回動態分配的內存,則返回類型不需要更改爲const char*。但是你必須一致,特別是不要在occassion返回字符串文字,如"",並在另一個返回動態分配內存,否則調用者將嘗試delete[]內存它不應該。

如果可能,請使用std::string而不是char*。它消除了管理動態分配內存的負擔。

+0

可以請你告訴我應該如何將store []傳遞給decodeString;我使用store []只是爲了使它成爲一種字符串,以便我可以使用返回函數傳遞它。 – user1590595

+0

@ user1590595,最簡單的方法就是'返回std :: string(store);'並將返回類型改爲'std :: string'。這是否允許? – hmjd

+0

不,我不能更改.h文件,並且返回類型必須相同。你能弄清楚別的嗎? – user1590595

1

將函數聲明從const char* decodeCode(const char* encodedString) {更改爲const char* DecodeTheCode::decodeCode(const char* encodedString) {DecodeTheCode::部分告訴編譯器'decodeCode()'函數是'DecodeTheCode'類的一部分,而不是其他類或甚至全局的一部分。

0

變化

const char* decodeCode(const char* encodedString)

看起來是這樣的:

char* DecodeTheCode::decodeCode(char* encodedString) 

這樣,它是一個類的方法定義,不只是一個新的函數聲明和定義爲在新的代碼。

從字符串常量到'char *'的棄用轉換來自您的方法調用,我相信,不是您聲明它的方式。你可以展示你如何使用這種方法,所以我肯定如何解決這個問題。