2010-04-22 101 views
1

我按照這裏的代碼示例初學C++問題

toupper c++ example

而在我自己的代碼來實現它,如下所示

void CharString::MakeUpper() 
{ 
char* str[strlen(m_pString)]; 
int i=0; 
str[strlen(m_pString)]=m_pString; 
char* c; 
while (str[i]) 
    { 
    c=str[i]; 
    putchar (toupper(c)); 
    i++; 
    } 
} 

但是這給了我下面的編譯器錯誤

CharString.cpp: In member function 'void CharString::MakeUpper()': 
CharString.cpp:276: error: invalid conversion from 'char*' to 'int' 
CharString.cpp:276: error: initializing argument 1of 'int toupper(int)' 
CharString.cpp: In member function 'void CharString::MakeLower()': 

這是行276

putchar (toupper(c)); 

據我所知,TOUPPER正在尋找INT作爲參數,還返回一個int,是什麼問題?如果是的話,這個例子如何工作?

+0

你可以給m_pString就是多一點澄清?你是否想要大寫一個單一的字符數組(C字符串)還是大寫字符串數組? – Eclipse 2010-04-22 16:19:53

+0

試圖在單個字符串中大寫每個字符 – 2010-04-22 16:25:13

+6

對於'char'和'char *'的使用,您** **根本上感到困惑**;你似乎正在玩它。在繼續之前,一些嚴重的C閱讀是**強烈推薦**。 – vladr 2010-04-22 16:30:02

回答

1

我打算假設m_pString是C風格的字符串(char *)。你正在做的事情比你需要做的更多。

void CharString::MakeUpper() 
{ 
    char* str = m_pString; // Since you're not modifying the string, there's no need to make a local copy, just get a pointer to the existing string. 
    while (*str) // You can use the string pointer as an iterator over the individual chars 
    { 
     putchar (toupper(*str)); // Dereference the pointer to get each char. 
     str++; // Move to the next char (you can merge this into the previous line if so desired, but there's no need. 
    } 
} 

在你舉的例子中,它的工作原因是因爲變量是如何聲明的。

int main() 
{ 
    int i=0; 
    char str[]="Test String.\n"; // This is a compile time string literal, so it's ok to initialize the array with it. Also, it's an array of `char`s not `char*`s. 
    char c; // Note that this is also a `char`, not a `char *` 
    while (str[i]) 
    { 
    c=str[i]; 
    putchar (toupper(c)); 
    i++; 
    } 
    return 0; 
} 

因爲使用C字符串的容易出錯的方式,最好的辦法是的std :: string:

void CharString::MakeUpper() 
{ 
    string str(m_pString); 
    transform(str.begin(), str.end(), ostream_iterator<char>(cout), &toupper); 
} 
+0

謝謝Eclipse,很好的解釋和100%正確 – 2010-04-22 16:45:17

+0

其實它是錯誤的 - 'while(str)'應該是while(* str)'。直到指針爲NULL,你纔會前進,直到指向的字符是'\ 0''。看到上面的更正版本。所有更多的理由遠離C字符串並使用'std :: string'。 – Eclipse 2010-04-22 17:41:52

+0

使用字符串已經引入了一個新的編譯器錯誤:(我包括,但我得到的字符串沒有定義的錯誤 – 2010-04-23 13:40:25

1

沒有從char *int的內置轉換,這就是發生錯誤的原因。既然你試圖利用一個字符,你需要取消引用指針。

putchar(toupper(*c));

+1

@Matt,這只是冰山一角。他在每個他應該使用'char'的地方使用'char *'。 – vladr 2010-04-22 16:31:11

2

你需要養活TOUPPER()一個int(或字符),而不是一個char *,這是你如何申報℃。

嘗試:

char c; 

此外,

char* str[strlen(m_pString)]; 

是一個指針數組,以字符,不只是一個單一的字符串。

這條線:

str[strlen(m_pString)]=m_pString; 

是分配給一個錯誤的指針,然後,因爲沒有分配。

4

此外,

char* str[strlen(m_pString)]; 
int i=0; 
str[strlen(m_pString)]=m_pString; 

無效C++ - 數組必須使用編譯時間常數的尺寸 - 這是一個C99的功能。我真的不認爲代碼會做你想做的事情,即使它是合法的,因爲你似乎正在訪問數組的末尾。如果您發佈了完整的類定義,那將很方便。

+0

數組必須使用編譯時間常量進行標註 - 有趣。這是來一個新的C++規範? – WhirlWind 2010-04-22 16:25:43

+1

@WhirlWind事情總是如此。它在C++ 0x中沒有改變。 – 2010-04-22 16:27:27

+2

@WhirlWind:這就是C++一直如此,以及C如何在C99之前。沒有動力在即將到來的C++標準中改變它,因爲'std :: vector <>'將處理可變長度並做更多事情。 – 2010-04-22 16:33:46

3

我不認爲你的代碼做你想做的事情,事實上,如果它編譯它會爆炸。



char* str[strlen(m_pString)]; // you've made an array of X C strings where 
           // X is the length of your original string. 
int i=0; 


str[strlen(m_pString)]=m_pString; // You've attempted to assign the C string in your array 
            // at location X to point at you m_pString. X is the 
            // same X as before and so is 1 past the end of the array 
            // This is a buffer overrun. 

我想你真正想做的是將m_pString的內容複製到str中。你會做到這一點,像這樣:



char * str = new char[strlen(m_pString)]; 
memcpy(str, m_pString); // I may have the operands reversed, see the docs. 

更簡單的方法來做到這一點,雖然是停止使用C字符串,並使用C++字符串:



std::string str = m_pString; 

還有更多的問題,但這應該得到你引導你更朝向正確的方向。