Search code examples
c++compiler-errorsmingweclipse-juno

mingwCC compile error (Eclipse Juno)


Okay, so I am trying to compile something right now and I am new to C++ so maybe the code itself is causing the error however no red marks show up in the code itself that Eclipse is showing me.

Here is what the error says

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'

Any ideas on what I need to do? on a Win7, using Eclipse Juno for C++ with MingwCC

Here is what I am compiling, the only new thing I added was this "swap" thing that someone told me to use for my permutation program.

UPDATED 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);
}

Solution

  • I rewrote the C code you linked to in 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
    }