2012-03-02 69 views
1

這個問題可能是很基本的,但今天我碰到一個錯誤說QString的複製

「錯誤:通過‘常量QChar則’爲‘本’的「QChar則& QChar則參數::運算符=(常量QChar則& )'丟棄限定符'

「我不知道這個assigment如何取消字符串到Qt ide預定義的字符串複製的資格」。

時被複制使用下面的代碼

for(int i=reqposition;;i++) 
{ 
    if(data.at(i)==',') 
     break; 

    temp.at(j)=data.at(i); // ERROR IS HERE 
    j++; 
} 

是什麼在此代碼的問題所需的I數據從一個串到另一個字符串。有沒有其他有效的方法將數據從一個字符串從指定索引複製到另一個字符串。 我如何解決這個問題。

謝謝你的時間!

回答

1

QString :: at返回const QChar

使用temp[j]=data.at(i);

+0

有什麼區別溫度[J]將使!我認爲這個語法temp。[j]也是錯誤的。 – scorpion 2012-03-02 17:36:00

+0

是的,這是一個錯字,現在已修復。 operator []返回一個對QString內部數據的引用,它允許你賦值給它。 – 2012-03-02 17:50:51

+0

temp [i] = data [i]和temp [i] = data.at(i)之間的區別是什麼? ,因爲我得到了temp [i] = data [i]的錯誤; 注意:temp和data都是QString對象 – scorpion 2012-03-02 18:08:16

1

你可能從QString的here尋找mid方法:

Returns a string that contains n characters of this string, starting at the specified position index.

0

這是從一個分裂代碼片段? 在這種情況下是不是更好使用QString.split函數?

+0

我不拆分字符串。其實我從通過循環找到的字符串複製數據,從指定索引到另一個字符串對象 – scorpion 2012-03-02 17:38:06

+0

在這種情況下,最好是獲得您需要的索引並使用中間函數。 差不多這樣 int i = reqposition;對於(;; i ++) if(data.at(i)==',') break; } QString tmp = data.midRef(reqposition,i); [/代碼] – LawfulHacker 2012-03-02 17:48:33

4
const QChar QString::at(int position); 

問題是你沒有分配給字符串;您正在分配QString::at(int)呼叫的const QChar結果。

你想,而不是修改的參考:從QString documentation:

QCharRef QString::operator[] (int position) Returns the character at the specified position in the string as a modifiable reference. Example: QString str;

if (str[0] == QChar('?')) str[0] = QChar('_'); The return value is of type QCharRef, a helper class for QString. When you get an object of type QCharRef, you can use it as if it were a QChar &. If you assign to it, the assignment will apply to the character in the QString from which you got the reference.

+0

確定沒有 用於當編譯精細的程序(INT I = reqposition ;;我++){ 如果 (data.at(ⅰ)== '') 中斷; temp [j] =數據。在(ⅰ); j ++; } 但是當我做了 temp [j] = data [j]; 我仍然有錯誤。這是爲什麼。你能告訴我 – scorpion 2012-03-02 17:41:03

+0

你在用什麼編譯器? 'temp [j] = data.at(i)'和'temp [j] = data [i]'對我來說沒有任何錯誤。 – tmpearce 2012-03-02 17:48:23

+0

我使用qt ide – scorpion 2012-03-02 17:50:38