0
I am creating a chained list in C++ and in condition if (j ->n == x)
, in the method wasVisited
which should check if an element has already been inserted in the list, the following message appears when I compile:
Exception released: read access violation. j was 0xCCCCCC.
Can’t fix it, can anyone help me? I’m using visualstudio as IDE.
#include "stdafx.h"
#include <iostream>
using namespace std;
class Celula{
public:
int n;
Celula* prox;
Celula() {
n = -1;
prox = NULL;
}//fim Construtor
Celula(int x) {
n = x;
prox = NULL;
}//fim construotor
};
class Lista {
public:
Celula* origem;
Celula* fim;
Lista() {
Celula x;
origem = &x;
fim = &x;
}//fim construtor
void inserir(int x) {
Celula c(x);
fim->prox = fim = &c;
}//fim inserir
bool wasVisited(int x) {
bool res = false;
Celula* j = origem->prox;
do {
if (j ->n == x) {
res = true;
}//fim if
else {
j = j->prox;
}//fim else
} while (res == false && j != NULL);//fim do.while
return res;
}//fim wasVisited
};
int main()
{
Lista teste;
teste.inserir(2);
teste.wasVisited(2);
teste.wasVisited(1);
cin.get();
return 0;
}
it is already being started: "Cell* j = source->Prox;" the error you are giving is when comparing the value of the object element that the pointer j points to with the value 'x' sent as parameter.
– Henrique Schiess Pertussati