0

I'm a noob trying to learn c++ and allegro and was following a tutorial which is how I came up with this code. My issue is at line:

"textout_centre_ex(screen, font1, Draw.c_str(), scrW / 2, scrH / 2, eBlue, -1);"

When it comes to 'Draw.c_str()' I get Error: argument of type "const char*" is incompatible with parameter of type "char*".

If I try and build I get "error C2664: 'void textout_centre_ex(BITMAP *,FONT *,char *,int,int,int,int)' : cannot convert argument 3 from 'const char *' to 'char *'"

How can I resolve this?

// Set variables
int counter = 0;
std::string Word = "SuperAwesomeTrivia";
std::string Draw = "";

FONT *font1 = load_font("font1.pcx", NULL, NULL);

while (!closeWindow){

// Update
    Draw += Word[counter];
    counter++;

    if (counter > Word.length() - 1)
    {
        closeWindow = true;
    }

    // Draw
    textout_centre_ex(screen, font1, Draw.c_str(), scrW / 2, scrH / 2, eBlue, -1);
    if (!closeWindow)
        rest(200);
    else
        rest(2000);
    clear_bitmap(screen);
}
destroy_font(font1);
allegro_exit();

return 0;
billybrian
  • 11
  • 3
  • 3
    What implementation of Allegro are you using? According to [documentation](http://liballeg.org/stabledocs/en/alleg018.html#textout_centre_ex), third argument is a `const char*`, not a `char*`. And it makes sense since the function shouldn't modify the string. – Jack Dec 30 '16 at 17:22
  • I'm using allegro 4.4.2 – billybrian Dec 30 '16 at 17:25
  • I guess something is wrong with your library then. – Jack Dec 30 '16 at 17:33
  • 2
    @Jack Allegro 4.4 defines `AL_CONST` as `const` only when compiling with GCC. ([non-GCC definition](https://github.com/liballeg/allegro5/blob/4.4/include/allegro/internal/alconfig.h#L209), [GCC-definition](https://github.com/liballeg/allegro5/blob/4.4/include/allegro/internal/alconfig.h#L151)) – Emil Laine Dec 30 '16 at 17:49
  • 1
    @tuple_cat: then a `const_cast` is the way to go but AL_CONST rationale seems quite obsolete since it refers to compiler which doesn't support `const`, there shouldn't be many nowadays. We don't know the OP compiler but I guess it could be safe to compile Allegro with `AL_CONST const` – Jack Dec 30 '16 at 17:55

2 Answers2

4

You can use const_cast to cast the argument to char* if the function is guaranteed to not mutate the pointed-to data:

textout_centre_ex(screen, font1, const_cast<char*>(Draw.c_str()), scrW / 2, scrH / 2, eBlue, -1);
                                 ^~~~~~~~~~~~~~~~~~            ~

A better solution would be to compile with GCC/Clang or #define AL_CONST const yourself before including any Allegro headers, so you get const in the places it should be in, and don't have to litter your code with ugly casts.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • 1
    @billybrian : You should really take a closer look at the lead Jack gave you in the initial post. According to documentation the function is supposed to take a `const char*`, which makes sense as it should not modify the data when printing it. I'd find out why yours does not, and find the real problem, not apply a ugly cast bandage. – Unimportant Dec 30 '16 at 17:50
  • 1
    @user1320881 Allegro 4.4 doesn't use `const` unless the library has been compiled with GCC. See my comment above on the original post. – Emil Laine Dec 30 '16 at 17:52
  • Where would I start.... should I reinstall allegro? Sorry guys but I am really new to this. I appreciate the help. – billybrian Dec 30 '16 at 17:57
  • @billybrian: telling what compiler you use would be a good start, as the problem seems diagnosed to you not using GCC. – Jongware Dec 30 '16 at 17:58
  • I would suggest using the latest Allegro 5 if you're starting a new project. Allegro 4 doesn't even support e.g. macOS. If you're compiling a legacy Allegro 4 project, you can get a precompiled version of Allegro 4 from your system's package manager, or from the Allegro website. – Emil Laine Dec 30 '16 at 18:03
  • I have been using ms visual studio 2013 and from what I can find it has it's own compiler... – billybrian Dec 30 '16 at 18:04
  • @billybrian Actually the simplest solution would be to define `AL_CONST` as `const` yourself before including any Allegro headers (preferably globally in your build system with e.g. the `/DAL_CONST=const` flag on MSVC). I think this should work. – Emil Laine Dec 30 '16 at 18:12
0

(heads up: My C++ experience is a bit rusty, I might remember some C++ details inaccurately)

You are proving a function a different type of data than it is asking for. It It is asking for something mutable and you give it something immutable.

I think in practical terms, the function still receives a copy of the char pointer (thus it would not matter if it changed the value of the pointer internally), but the compiler is probably right to complain anyway.

To solve such a problem you could copy the entire text (char* points to an array of chars, terminated by \0, right?) to a new char array and provide that to the function. Maybe there is a newer version of the API that has changed the signature to incorporate the "const" for the parameter.

Ray
  • 686
  • 3
  • 6
  • 2
    The diffirence between `char*` and `const char*` is not about the pointer beeing immutable or not, but about what is beeing pointed to. Yes, the function receives a copy of the pointer, but trough a const pointer it cannot mutate what is beeing pointed to while trough a non-const pointer it can. So the compiler MUST complain. – Unimportant Dec 30 '16 at 17:45