我在寫一個簡單的程序。它只有一個類。有一個私人成員'char * number'和兩個函數(會有更多,但首先這些應該正常工作:))。在C++中打印char *
第一個應該複製的「源」到「數字」變量(和我想的地方現在的問題):
LongNumber::LongNumber(const char * source){
int digits = strlen(source);
char* number = new char[digits+1];
strcpy(number, source);
// cout<<number<<endl; - if the line is uncommented,
// the output is correct and there isn't a problem
}
和打印功能:
void LongNumber::print(){
cout<<number<<endl;
// when I try to print with the same line of code here..it crashes
}
當然,我錯過了一些東西......但是什麼?
(由於這是我的第一篇......你認爲標籤corrected..how將您所標記的職位?)
預先感謝您:)
哎呀,似乎有一百萬人回答了這個問題,而我......對不起,關於這個:) – micmoo 2009-06-28 07:30:45