Hey guys!

Im having problems with a snippit of code ive written. I know this is basic C++ so its a little embarasing that i cant work it out myself. Ive googled the run time error message but im not 100% sure i understand what it means.

Unhandeled exeption, access violation

#ifndef SETTINGS_H
#define SETTINGS_H

#include<string>
#include<vector>
#include<iostream>

using namespace std;

class Settings
{
private:
    vector<string>board;
protected:
public:
    Settings();

    bool loadAndDrawFont();
};


#endif

#include "Settings.h"
#include<allegro5\allegro.h>
#include<allegro5\allegro_native_dialog.h>
#include<allegro5\allegro_ttf.h>
#include<allegro5\allegro_font.h>
#include<iostream>

Settings::Settings()
{
    board.push_back("050106007");
    board.push_back("620054090");
    board.push_back("000908002");
    board.push_back("300000568");
    board.push_back("080605020");
    board.push_back("546000009");
    board.push_back("700502000");
    board.push_back("030840016");
    board.push_back("800301040");
}

bool Settings::loadAndDrawFont()
{
    //int blockSize = 36;
    ALLEGRO_FONT *font = al_load_font("Assets/Fonts/Airmole Shaded.ttf", 36, NULL);

    if(!font)//if the font didnt load
    {
        al_show_native_message_box(NULL, "Error Message", "Error", "Font not loaded", NULL, ALLEGRO_MESSAGEBOX_ERROR);
        return -1;//end program
    }

    for(int i = 0; i < 9; i++)
    {
        for(int j = 0; j < 9; j++)
        {
            if(board[i][j] == ('1'))//this is the line thats the problem
            {
                al_draw_text(font, al_map_rgb(44, 117, 255), 100, 100, ALLEGRO_ALIGN_CENTRE, "1");
            }
        }
     }

The problem must be soemthing else in the program that has corrupted stack or program memory. The code below works ok for me

#include<string>
#include<vector>
#include<iostream>

using namespace std;

class Settings
{
private:
    vector<string>board;
protected:
public:
    Settings();

    bool loadAndDrawFont();
};



Settings::Settings()
{
    board.push_back("050106007");
    board.push_back("620054090");
    board.push_back("000908002");
    board.push_back("300000568");
    board.push_back("080605020");
    board.push_back("546000009");
    board.push_back("700502000");
    board.push_back("030840016");
    board.push_back("800301040");
}

bool Settings::loadAndDrawFont()
{

    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
        {
            if (board[i][j] == ('1'))//this is the line thats the problem
            {
                cout << board[i][j] << '\n';
            }
        }
    }
    return true;
}
    int main()
    {
        Settings s;
        s.loadAndDrawFont();
    }

But everything worked untill i put that function in. And it works when i comment that line out. Ill have another look, see if i can find anything else. Thanks for your reply!

The problem might be line 61 -- al_draw_text(...), I don't have that library installed so I couldn't call it.

Nah that line works and draws to screen when i comment the line above out

Ive fixed the problem but im unsure as to why it works. Maybe someone could clarify for me. I noticed in your main you called the class and not a pointer to the class like i was

Settings *settings; 

why does using a pointer make a difference?

This works ok too

int main()
    {
        Settings *s = new Settings;
        s->loadAndDrawFont();
        delete s;
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.