I've got an array of pointers to my class "Rule" Now I'm trying to access it from within a member function AddRule. But on the first line I keep getting "Memory Access Violation"

Can anyone tell what's wrong?

class RuleSet
{
  private:
  
  public:
  Rule *Rules[200];
  int RuleNum;

  void AddRule(Rule);
  void ProcessCell(int);
  RuleSet();      
};

void RuleSet::AddRule(Rule A)
{
  Rules[RuleNum]->min.MembraneOut[0]=A.min.MembraneOut[0]; // !!! THE BAD LINE
  RuleNum++;
}

RuleSet::RuleSet()
{
  this->RuleNum=0;
  int i;
  for (i=0;i<200;i++)
  {
    Rules[i]=new(Rule);
  }
};

Quick nickpicks: I don't see a destructor, and keep rules[] private. Normally variable names are lowercase.

Why are you assigning it zero?

Rule *rules;
rules = new Rule [200];
delete [] rules;

Well I did like you told, but still it doesn't work.

And using watches I found that RuleNum 's value is ???? (four question marks
)

Can I see some code?

class Rule
{
  public:
  LivingCell min;
  LivingCell max;
  LivingCell IgnoredParametres;
  int Action;
  int Parameter;
};
// ------------------- Класс "Генотип" -----------------
class RuleSet
{
  private:
  Rule *Rules;
  public:
  
  int RuleNum;

  void AddRule(Rule);
  void ProcessCell(int);
  RuleSet();
};

RuleSet *Genome;

// ------------------- Функции класса "Генотип" -----------------
void RuleSet::AddRule(Rule A)
{
  Rules[this->RuleNum].min.MembraneOut[0]=A.min.MembraneOut[0]; // That's the bad line
  this->RuleNum++;
}

RuleSet::RuleSet()
{
  this->RuleNum=0;
  int i;
  Rules=new Rule[200];
};

Note that this->RuleNum actually doesn't work either (value is ????)

But even if I change it into a constant (0 for instance) I've got no effect - still "Memory Access Violation"

wouldn't it have to be

Rules[this->RuleNum]->min->MembraneOut[0]=A.min.MembraneOut[0];

Chris

No It wouldn't. Rules[SomeInt] is a class, not a pointer to one.

oh sorry lol i read you had created an array of pointers...then just glanced over the code...but you hadn't created and array of pointers

watches also told me that in function in question this = NULL

That's supposed to be a member function!

I just don't get it...

Show us the code where you create a RuleSet and then where you add a Rule to the RuleSet. RuleSet * Genome declares a pointer to a RuleSet but never actually creates one.

If you don't add any code and then call Genome->AddRule(...) Genome is an undefined pointer (probably NULL) and will fault in the manner described.

Did you mean somewhere to do Genome = new RuleSet(); or would you just prefer to declare it: RuleSet Genome; .

Here Genome is creat6ed:

__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
  Genome=new(RuleSet);

}

I've tracked it through a constructor - everything's OK.

That's how I call AddRule:

void __fastcall TfrmRuleSet::Button1Click(TObject *Sender)
{
  Rule AddingRule;
  if (this->WorkMode==0)
  {
    AddingRule.IgnoredParametres.ConcentrationInside[0]=this->chbInC1->Checked;
    AddingRule.IgnoredParametres.ConcentrationInside[1]=this->chbInC2->Checked;
    
    /// Blah-Blah-Blah... 3 pages of similar code

    AddingRule.max.NumberOfNeighbours=atoi(this->txtNeghbTo->Text.c_str());

    AddingRule.Action=this->cmbAction->ItemIndex;
    AddingRule.Parameter=atoi(this->txtParameter->Text.c_str());
    Genome.AddRule(AddingRule);
  }
}

I found the way that works but it is not a correct solution.

When changing

Rules[this->RuleNum]->min->MembraneOut[0]=A.min.MembraneOut[0];

into a

Genome->Rules[this->RuleNum]->min->MembraneOut[0]=A.min.MembraneOut[0];

Everything works. Yes I know that using an instance of a class inside its' class member function is a very wrong thing to do, so I still wonder why the hell this==NULL

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.