Color Palette?
Nov 5, 2016 at 5:11pm UTC
I am trying to create a reusable color palette. What I have so far doesn't show any errors until runtime. Here is what I have:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
struct COLORPALETTE {
static const ALLEGRO_COLOR RED;
static const ALLEGRO_COLOR BLACK;
static const ALLEGRO_COLOR YELLOW;
COLORPALETTE() {
ALLEGRO_COLOR RED = al_map_rgb(255,0,0);
ALLEGRO_COLOR BLACK = al_map_rgb(0,0,0);
ALLEGRO_COLOR YELLOW = al_map_rgb(255,255,0);
}
};
COLORPALETTE Color;
int main() {
ALLEGRO_COLOR testClr = Color.RED;
return 0;
}
The error I get is:
1>main.obj : error LNK2001: unresolved external symbol "public: static struct ALLEGRO_COLOR const COLORPALETTE::RED" (?RED@COLORPALETTE@@2UALLEGRO_COLOR@@B)
What am I doing wrong?
Last edited on Nov 5, 2016 at 5:12pm UTC
Nov 5, 2016 at 5:34pm UTC
Static class variables need to be defined outside the class body.
1 2 3
const COLORPALETTE::ALLEGRO_COLOR RED = al_map_rgb(255,0,0);
const COLORPALETTE::ALLEGRO_COLOR BLACK = al_map_rgb(0,0,0);
const COLORPALETTE::ALLEGRO_COLOR YELLOW = al_map_rgb(255,255,0);
This is unfortunately not a solution because you're not allowed to call Allegro functions before you have initialized the Allegro library.
Last edited on Nov 5, 2016 at 5:35pm UTC
Nov 5, 2016 at 5:40pm UTC
Thanks, this works. In another header (loaded before these colors) I have this, so allegro initialization is already taken care of:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#pragma once
#ifndef ALLEGRO_INITIALIZE_HEADER_H_
#define ALLEGRO_INITIALIZE_HEADER_H_
#include <iostream>
#include <exception>
#include <allegro5/allegro.h>
#include <allegro5/allegro_native_dialog.h>
#include <allegro5/allegro_ttf.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_primitives.h>
#include <allegro5/allegro_image.h>
#include <allegro5/allegro_windows.h>
bool initializeAllegro5() {
try {
if (!al_init()) throw ("Allegro failed to initialize." );
al_init_font_addon();
if (!al_init_ttf_addon()) throw ("Failed to initialize TTF font file support." );
if (!al_init_primitives_addon()) throw ("Failed to initialize allegro primitives support." );
if (!al_install_keyboard()) throw ("Failed to initialize allegro keyboard support" );
if (!al_install_mouse()) throw ("Failed to initialize allegro mouse support." );
if (!al_init_image_addon()) throw ("Failed to initialize allegro image support." );
} catch (const char * e) {
std::cout << e << std::endl;
return false ;
} catch (const std::exception& e) {
std::cout << "Unexpected initialization error: " << e.what() << std::endl;
return false ;
}
return true ;
}
const static bool INITIALIZE_ALLEGRO = initializeAllegro5();
#endif
Last edited on Nov 5, 2016 at 5:46pm UTC
Nov 16, 2016 at 3:00pm UTC
Hello!, I saw you helped someone with a battleship program a while back, is there any way you could help me with mine? I just posted on my profile the parameters, I could really use help
Nov 16, 2016 at 3:39pm UTC
Thanks, this works. In another header (loaded before these colors) I have this, so allegro initialization is already taken care of:
That is not really much of a solution either. You will be initializing the allegro library once per translation unit that includes that header, and you do not want to initialize the allegro library more than once.
Topic archived. No new replies allowed.