Hi!

As the headline suggests I am getting the following error message:
First-chance exception at 0x00d690d1 in Nyår.exe: 0xC0000005: Access violation reading location 0xcccccccc.
Unhandled exception at 0x00d690d1 in Nyår.exe: 0xC0000005: Access violation reading location 0xcccccccc.

#include<iostream>
#include<cstdlib>
#include "Participantsregistryhead.h"

int main()
{
    _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
    ParticipatorRegister Registry;

    Registry.typeOut();

    system("pause");
    return 0;
}

// The ParticipatorRegister object header file: ///////////////////////////////////////

#ifndef PARTICIPANTSREGISTRY_H
#define PARTICIPANTSREGISTRY_H

#include<iostream>
#include<fstream>

#include "Participatorhead.h"

class ParticipatorRegister
{

public:
ParticipatorRegister(); // default constructor
~ParticipatorRegister(); // destructor

ParticipatorRegister(const ParticipatorRegister &obj); // copy constructor

void typeOut();


void createObject();

void operator=(const ParticipatorRegister &obj);

private:
Participator **Participants;
int size;
std::string something;

};


#endif

// The ParticipatorRegister object cpp: ///////////////////////////////////////

ParticipatorRegister::ParticipatorRegister()
{
    size = 1;


    Participator **Participants = new Participator*[size];

    for (int i = 0; i < size; i ++)
    {
        Participants[i] = new Participator();
    }


}

ParticipatorRegister::~ParticipatorRegister()
{
}

void ParticipatorRegister::typeOut()
{
    std::cout << Participants[0]->toString(); // this is where the problem occurs
}

// The Participator object header file: ///////////////////////////////////////

#ifndef PARTICIPATOR_H
#define PARTICIPATOR_H

#include<iostream>
#include<fstream>
#include<sstream>

class Participator
{
private:
    std::string name;
    std::string adress;
    std::string completestring;
    std::string paidstatus;
    bool paid;

public:
    Participator(std::string name, std::string adress); // constructor
    Participator(); // default constructor
    ~Participator(); // destruktor

    // Set
    void setName(std::string name);
    void setAdress(std::string adress);
    void setPaid(bool paid);

    // Get
    std::string getName() const;
    std::string getAdress() const;
    bool getPaid() const;

    // Save function
    // Read function

    void save(std::ofstream &out);
    void read(std::ifstream &in);

    std::string toString(); // returnerar string med info om deltagaren

    int operator<(const Participator &obj); // sorts objects by name(alphabetical order)

    int operator==(const Participator &obj); // searches for object by name
};

// The Participator object cpp: ///////////////////////////////////////

#include "Participatorhead.h"

Participator::Participator(std::string name, std::string adress) // constructor
{
    this->name = name;
    this->adress = adress;
    paid = false;

    completestring = "";
    paidstatus = "Avgift ej betald";
}

Participator::Participator() // default construtor
{
    name = "Bertil";
    completestring = "";
    paidstatus = "Avgift ej betald";
}

Participator::~Participator() // destructor
{
}

// set

void Participator::setName(std::string name)
{
    this->name = name;
}

void Participator::setAdress(std::string adress)
{
    this->adress = adress;
}

void Participator::setPaid(bool paid)
{
    this->paid = paid;
}

// get

std::string Participator::getName() const
{
    return this->name;
}

std::string Participator::getAdress() const
{
    return this->adress;
}

bool Participator::getPaid() const
{
    return this->paid;
}

// read and save

void Participator::save(std::ofstream &out)
{
    out << toString();
}

void Participator::read(std::ifstream &in)
{
        getline(in, name);
        getline(in, adress);
        getline(in, paidstatus);
        if (paidstatus == "Avgift betald")
        {
            setPaid(true);
        }
        else if (paidstatus == "Avgift ej betald")
        {
            setPaid(false);
        }
}



// create string

std::string Participator::toString()
{
    this->completestring = this->name + " \n" + this->adress + " \n";
    if (this->paid)
    {
        this->completestring += "Avgift betald\n";
    }
    else if (!this->paid)
    {
        this->completestring += "Avgift ej betald\n";
    }

    return this->completestring;
}

// Sort object by name(alphabetical order)

int Participator::operator<(const Participator &obj)
{
    if(this->getName() < obj.getName())
    {
    return 1;
    }
    else
    {
    return 0;
    }
}

int Participator::operator==(const Participator &obj)
{
    if(this->getName() == obj.getName())
    {
        return 1;
    }
    return 0;
}

So far If I allocate the pointer to pointer array in the typeOut() function everything works just fine, however this is not how I want it to work. It seems the allocation of the array in the constructor is completely non-existant when I try to exectue the typeOut function without it containing the allocation of the array.

Suggestions anyone?

line 59: notice the pointer is local to the constructor. As soon as the constructor ends the pointer goes away and all memory alloted to it is lost (memory leak).

Correct the problem by allocating memory for the pointer that is declared in the class, like this:
this->Participants = new Participator*[size];

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.