0

I am getting a weird error when running my program inside CLion. I checked my code and couldn't find any open braces or semicolon missing or hanging. I have 1 class (.cpp and .h) and a main.cpp. It's a really small program but I can't figure out what's wrong. The error I get is as follow

internal compiler error: in finish_expr_stmt, at cp/semantics.c:677 xword::xword(map words) {

This application has requested the Runtime to terminate it in an unusual way.

Please contact the application's support team for more information. C:\Users\marti\CLionProjects\crossword\xword.cpp:8:37: internal compiler error: Aborted

This application has requested the Runtime to terminate it in an unusual way.

Please contact the application's support team for more information. g++.exe: internal compiler error: Aborted (program cc1plus)

Please submit a full bug report, with preprocessed source if appropriate.

xword.cpp

#include <iostream>
#include <sstream>
#include "xword.h"
//Constructor que acepta las palabras con su orientacion en un mapa como parametro y basicamente
//crea e inicializa la matriz
//ademas llena los espacios vacios con letras al azar

xword::xword(map<string, char> n_words) {
    //Inicializamos la matr9z
    grid = new char*[SIZE];
    for(int x = 0;x < SIZE;x++){
        grid[x] = new char[SIZE];
    }

    //Se valida las palabras y su orientacion
    //en caso sean validas se añaden a la lista
    //sino son descartadas
    for(pair<const string, char> & w : n_words){
        if(validate_word(w.first, w.second)){
            add_word(w.first, w.second);
        }
    }

    srand(NULL);

}
bool xword::validate_word(string word, char pos) {
    //Validacion de longitud
    if(word.length() > 10)
        return false;
    //Validacion de que no este repetida
    for(const auto& iter : words_i){
        if(iter.first.compare(word) == 0)
            return false;
    }

    //Validacion de espacion para la orientacion deseada
    for(int x = 0;x < SIZE;x++){
        for(int y = 0;y < SIZE;y++){
            char tmp  = grid[x][y];
            if(tmp != 0){
                break;
            }
            if(!has_space(word.size(), pos, x, y))
                return false;
        }
    }

    //TODO verificar caracteres validos


    return true;
}

//Añadir una palabra al mapa miembro
bool xword::add_word(string word, char pos){
    if(validate_word(word, pos)){
        words_i[word] = pos;
        int x1, y1, x2, y2;
        get_espacio_libre(word.size(), pos, x1, y1, x2, y2);

        for(int x = x1, count = 0;x <= x2;x++){
            for(int y = y1;y < y2;y++){
                grid[x][y] = word[count];
                count++;
            }
        }
        return true;
    }
    return false;
}

//Verificacion de que la matriz tenga espacio en la orientacion
//dadas las posiciones x e y
bool xword::has_space(char word_size, char orientation, char xpos, char ypos) {
    if(orientation == 'V')
    {
        for(int x = xpos;x < word_size;x++){
            char tmp = grid[x][ypos];
            if(tmp != 0)
                return false;
        }
    }
    else if(orientation == 'H')
    {
        for(int y = ypos;y < word_size;y++){
            char tmp = grid[xpos][y];
            if(tmp != 0)
                return false;
        }
    }
    return true;
}
//Consigue el primer espacion libre para una palabra de len word_size y orientacion definida
void xword::get_espacio_libre(char word_size, char orientation, int& x1, int& y1, int& x2, int& y2){
    for(char x = 0;x < SIZE;x++){
        for(char y = 0;y < SIZE;y++){
            if(grid[x][y] == 0 && has_space(word_size, orientation, x, y))
            {
                if(orientation == 'V'){
                    x1 = x;
                    y1 = y;
                    x2 * x+word_size;
                    y2 = y;
                    return;
                }
                else{
                    x1 = x;
                    y1 = y;

                    x2 = x;
                    y2 = y+word_size;
                    return;
                }
            }
        }
    }
};

xword.h

#include <string>
#include <vector>
#include <map>

//Tamaña predefinido de matriz eg. 10x10
#define SIZE 10

#ifndef XWORD_XWORD_H
#define XWORD_XWORD_H


using namespace std;

class xword {
private:
    //Mapa de las palabras y orientaciones
    map<string, char> words_i;
    //Palabras encontradas
    vector<string> p_encontradas;
    //Matriz
    char** grid = nullptr;

    //Validar palabra y orienta
    bool validate_word(string word, char pos);

    //Validar espacion en matriz
    bool has_space(char word_size, char orientation, char xpos, char ypos);

    //Retornar coordenadas de espacio libre en los numeros que se le pasan
    void get_espacio_libre(char word_size, char orientation,  int& x1, int& y1, int& x2, int& y2);

public:
    //Añade palabra al mapa
    bool add_word(string word, char pos);
    //Constructor que deberia ser usado
    explicit xword(map<string, char> words);

    //Constructor vacio
    xword(void);

};


#endif //PUPILETRAS_XWORD_H

main.cpp

#include <iostream>
#include <assert.h>
#include "xword.h"

#define VERTICAL 'V'
#define HORIZONTAL 'H'
xword* xw;
map<string, char> lista_palabras =
        {
        make_pair("cuadro", VERTICAL),
        make_pair("mesa", HORIZONTAL),
        make_pair("palabra", VERTICAL),
        make_pair("raton", HORIZONTAL),
        make_pair("poste", VERTICAL),
        make_pair("comida", HORIZONTAL),
        make_pair("letrero", VERTICAL),
        make_pair("usar", HORIZONTAL),
        make_pair("dos", VERTICAL),
        make_pair("quince", HORIZONTAL)
        };
using namespace std;

void ingresar_palabras(){
    map<string, char> words;
    string inpt;
    cout << "Ingresa la palabra seguida de un espacio y una letra indicando la orientacion deseada " << endl;
    cout << "o un 0(cero) para que sea al azar eg( palabra (V, vertical, v, VERTICAL) )" << endl;
    //Mientras se siga alimentando palabras y orientaciones el programa sigue corriendo
    while(getline(cin, inpt)){
        if(inpt.empty())
            break;
        string name;
        char orient;

        string tmp = inpt.substr(inpt.find(" "));
        name = inpt.substr(0, inpt.find(" "));
        //Asignamos la orientacion basado en lo que se encuentra despues de la palabra inicial al macro VERTICAL o HORIZONTAL
        orient = (tmp.compare("v") || tmp.compare("V") || tmp.compare("vertical") || tmp.compare("VERTICAL")) ? VERTICAL :
                 (tmp.compare("h") || tmp.compare("H") || tmp.compare("horizontal") || tmp.compare("HORIZONTAL")) ? HORIZONTAL :
                 HORIZONTAL;

        words[name] = orient;
    }
    lista_palabras = words;
}
bool in_lista(string palabra){
    for(pair<string, char> pal : lista_palabras){
        if(pal.first == palabra)
            return true;
    }
    return false;
}
bool main_loop(){
    bool inicia = true;
    while(inicia){
        xw->prnt_grid();
        int x1, y1, x2, y2;
        cout << "Elige una posicion x y";
        cin >> x1, y1;
        cout << "Elige otra posicion x y";
        cin >> x2, y2;
        string palabra = xw->palabra_at(x1, y1, x2, y2);
        if(in_lista(palabra) ){
            cout << "Encontraste una palabra!";
            xw->encontrar_palabra(palabra);
            if(xw->ganado()) {
                cout << "Ganaste el juego!";
                break;
            }
            continue;
        }
        cout << "Esa palabra no es valida, sigue intentando";

    }
}

int main() {
    //Eliges usar la lista que ua hay de palabras o ingresar una nueva lista
    int opt;
    cout << "Desea ingresar palabras (0) o usar la lista predetermina de palabras (1): ";
    cin >> opt;

    //Se asegura de que sea opcion 1 o 2 sino muestra un error
    assert(opt == 1 | opt == 0);

    if(opt == 0) {
        ingresar_palabras();
    }

    xw = new xword(lista_palabras);
    main_loop();

    return 0;
}

EDIT: I was able to replicate the code with the minimal amount of code that I replaced in for the originala code I posted. I was able to pinpoint the compiler error to char ls[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; however I don't know why it would cause a compiler error.

MartinStone
  • 184
  • 1
  • 2
  • 14
  • Unrelated to your problem, but you don't need to have your own loop to check if a key exists in a map, just [*find*](http://en.cppreference.com/w/cpp/container/map/find) it. – Some programmer dude Nov 29 '17 at 04:07
  • You need to give us a [mcve](https://stackoverflow.com/help/mcve). – David G Nov 29 '17 at 04:07
  • You also have *undefined behavior* when you check the contents of `grid[x][y]` before it has been initialized. Its contents will be *indeterminate*. Doing e.g. `new char[SIZE]` will not initialize the memory being allocated. – Some programmer dude Nov 29 '17 at 04:08
  • And why don't you use a vector (of vectors) for `grid`? Why the dynamic allocation? Or why not an array of arrays since the size is known at compile-time? – Some programmer dude Nov 29 '17 at 04:10
  • Lastly, where in *your* code do you get the error? And try to narrow it down to a [**Minimal**, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Nov 29 '17 at 04:11
  • On the constructor of xword.cpp I am going to zero all the matrix and then add the words however I ran this to test if everything else was okay and got this error – MartinStone Nov 29 '17 at 04:17
  • As for msve should I just cut out parts of the code? I coded it ran it and it worked fine. Then I worked on it a bit and just tried to ran it and got the error at the top of the post. That's all the CLion console tells me. I don't even know what's that suppoused to mean, points the error to my constructor implementation – MartinStone Nov 29 '17 at 04:19
  • Oh by the way, doing `using namespace std;` [is bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Doing it [in a header file doubly so](https://stackoverflow.com/questions/14575799/using-namespace-std-in-a-header-file). You should probably learn about *references* as well, and how to pass by reference (or rather `const` reference). – Some programmer dude Nov 29 '17 at 04:47
  • "Internal compiler error" is a polite name for a compiler bug. You should report it to the compiler developers as instructed. – n. m. could be an AI Nov 29 '17 at 07:10
  • FWIW gcc 6.4.0 doesn't terminate with an ICE and reports quite a few problems with your code. Perhaps you should consider upgrading the compiler. – n. m. could be an AI Nov 29 '17 at 07:18
  • @Somprogrammerdude I moved it to cpp yesterday while trying to fix it but it's the same, I will post updated code in a bit. I also updated MinGW through mingw-get update and upgrade and it shows everything is up to date. – MartinStone Nov 29 '17 at 16:13

0 Answers0