0
votes

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);
}
2
You can't swap two characters of a constant string.chris
@chris Will I want to do something like str[j]? I think that's what substring is for C++. Or is there something else I need to do? Looking at the big picture I am at least getting it somewhat right? Thanks EDIT: Wait, I am already doing that....now im lostAustin
If you don't want to change the original string, just pass it in by value if you need to modify the parameter. That way, you get a copy to modify and the original stays untouched.chris
@chris that's my problem I feel like... Originally I need to use a string, however the post I am trying to follow in doing this uses a char so I'm getting my mindset mixed up. Here is what I am following, should I scrap it? geeksforgeeks.org/archives/767Austin
The one you've commented out should work the same way as the one in the article if you change const string & to string.chris

2 Answers

0
votes

Figured out how to swap it correctly.

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

}
0
votes

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
}