0

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);
}
Austin
  • 3,010
  • 23
  • 62
  • 97
  • 1
    You can't swap two characters of a constant string. – chris Dec 12 '12 at 04:29
  • @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 lost – Austin Dec 12 '12 at 04:30
  • 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 Dec 12 '12 at 04:31
  • @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? http://www.geeksforgeeks.org/archives/767 – Austin Dec 12 '12 at 04:43
  • 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 Dec 12 '12 at 04:46
  • @chris I did that and changed the function in Permutation.h as well, now it crashed... I posted all 3 code snippets this time and updated the code before – Austin Dec 12 '12 at 04:53
  • Oh, when it says `swap(a+i, a+j)`, what works with this is `swap(a[i], a[j])`. As it is, you're going out of bounds. Be sure to change all calls that do that. – chris Dec 12 '12 at 04:57
  • @chris Okay cool! It didn't crash that time. (code updated). So, now where will I be doing the cout? Also I fail to see how str[j] is any new location...in my mind when I follow the logic it is swapping in the same location as j = low...so how you swap locations low and j? – Austin Dec 12 '12 at 05:02

2 Answers2

0

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

}
Austin
  • 3,010
  • 23
  • 62
  • 97
0

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
}
Anonymous Coward
  • 6,186
  • 1
  • 23
  • 29
  • @Anonymouse Coward Hello! Thanks for the answer, I was also able to finally figure it out. Question, can you explain to me the logic of this line, for learning purposes? Thanks `do_permute(str, i, n ? n : (str.length()-1) );` – Austin Dec 12 '12 at 06:18
  • @Austin this is called [inline if, conditional operator, or simply tenary operator](http://en.wikipedia.org/wiki/%3F:). In the example above it translates to: if n is nonzero `if(n)` evaluate to n, otherwise evaluate to (str.length()-1). or in general: `variable = condition ? value_if_true : value_if_false`. Please note that value_if_true and value_if_false must be of the same type! (you can't mix, say double and string). – Anonymous Coward Dec 12 '12 at 06:29