I'm creating a threaded list in C ++ and in condition if (j ->n == x)
, in method wasVisited
that should check if an element has already been inserted in the list, the following message appears when I compile:
Exception thrown: Read access violation. j was 0xCCCCCC.
I can not solve 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;
}