2012-12-12 62 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]); 
    } 

}