2012-12-12 129 views
0

好的,所以我現在想編譯一些東西,而我是C++的新手,所以也許代碼本身導致錯誤,但是Eclipse沒有顯示代碼本身顯示的紅色標記我。mingwcc編譯錯誤(Eclipse Juno)

下面是錯誤說

c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/move.h:128:7: error: assignment of read-only reference '__a'

c:\mingw\bin../lib/gcc/mingw32/4.6.2/include/c++/bits/move.h:129:7: error: assignment of read-only reference '__b'

任何想法什麼我需要做什麼?在Win7上,使用Eclipse Juno for C++與MingwCC

下面是我正在編譯的內容,我添加的唯一新東西就是這個「交換」的東西,有人告訴我用於我的置換程序。

修訂 Permutation.cc

#include <iostream> // for cout 
#include <cstdio>  // for printf() 
#include <sstream> // for stringstream 
#include <stdio.h> 
#include <string.h> 
#include "Permutation.h" 
using namespace std; 

Permutation::Permutation() { 
    /* nothing needed in the constructor */ 
} 

void Permutation::permute(string str) { 

    int low = 0; 
    int high = str.length(); 
     int j; 
     if (low == high) { 
      cout << str << endl; 
     } else { 
      for (j = low; j <= high; j++) { 
      std::swap(str[low], str[j]); 
      permute(str, low + 1, high); 
      std::swap(str[low], str[j]); 
     } 
     } 
    } 


void Permutation::permute(string str, int low, int high) { 
// int j; 
// if (low == high) { 
//  cout << str << endl; 
// } else { 
//  for (j = low; j <= high; j++) { 
//   std::swap(str[j + low], str[j + j]); 
//   permute(str, low + 1, high); 
//   std::swap(str[j + low], str[j + j]); 
//  } 
// } 
} 

Permutation.h

#pragma once 
#include <string> 
using namespace std; 
class Permutation { 
    public: 
     Permutation(); 

     void permute (string); 
     void permute (string, int, int); 
    private: 
     /* attemp to solve this problem without adding 
     * any instance variables/data members, but 
     * you may add private helper function members 
     * as many as you need */ 
}; 

main.cc

#include "Permutation.h" 

int main() 
{ 
    Permutation p; 


    p.permute ("Permute"); 
    p.permute ("--*--", 2, 3); 
} 
+1

你不能換一個字符串常量的兩個字符。 – chris

+0

@chris我想做類似str [j]的事嗎?我認爲這就是C++的子字符串。或者還有什麼我需要做的? 縱觀整體情況,我至少有點正確? 謝謝 編輯:等等,我已經這樣做了....現在我失去了 – Austin

+0

如果你不想改變原始字符串,只需要通過值傳遞它,如果你需要修改參數。通過這種方式,您可以獲得一份副本進行修改,而原稿保持不變。 – chris

回答

0

我改寫了你的C++鏈接到C代碼:

// this method should be private or protected because 
// str is passed by reference and will be modified ! 
// if you prefer a free standing function, don't add the 
// declaration to the header, this for internal use only 
void do_permute(std::string& str, unsigned i, unsigned n) { 
    // you COULD pass str by value here, which 
    // would remove the need to backtrack. 
    // however, it would create a new copy for every 
    // iteration which is terrible for performance, 
    // especially with long strings. 
    if(i==n) 
     std::cout << str << '\n'; 
    else 
     for(unsigned j=i; j<=n; ++j) { 
      std::swap(str[i],str[j]); 
      do_permute(str,i+1,n); 
      std::swap(str[i],str[j]); // backtrack (undo swap) 
     } 
} 

// this is the public method; 
// pass string by value (copy), to allow do_permute() 
// to modify the string. 
void permute(std::string str, unsigned i=0, unsigned n=0) { 
    if(n >= str.length()) 
     return; // prevent out of bounds access 
    // if n is 0 (default value) use the string length instead 
    do_permute(str, i, n ? n : (str.length()-1)); 
} 

int main() { 
    permute("BAR"); 
    permute("FO0BAR", 3); // FOO*** 
    permute("FO0BAR", 0, 2); // ***BAR 
} 
+0

@Anonymouse懦夫你好!感謝你的回答,我也終於弄清楚了。問題,爲了學習目的,你能向我解釋這條線的邏輯嗎?謝謝'do_permute(str,i,n?n:(str.length() - 1));' – Austin

+0

@奧斯汀這叫做[inline if,conditional operator,or tenary operator](http://en.wikipedia .ORG /維基/%3F :)。在上面的例子中,它轉化爲:如果n非零,那麼if(n)評估爲n,否則評估爲(str.length() - 1)。或者一般來說:'variable = condition? value_if_true:value_if_false'。請注意,value_if_true和value_if_false必須是相同的類型! (你不能混合,比如說double和string)。 –

0

想通了如何正確地將其交換。

int low = 0; 
int high = str.length() - 1; 
// make sure the string is a permutation and not a partial mix. 
if (low == high) { 
    cout << str << endl; 
} else { 
    //Takes each initial letter, then permutes the remaining string. Then moves to next character. 
    for (int i = low; i <= high; i++) { 
     std::swap(str[low], str[i]); 
     permute(str, low + 1, high); 
     std::swap(str[low], str[i]); 
    } 

}