Error: terminate called after throwing an instance of 'char const*'

I would like to write a list in .txt, but I understood this problem. Other problem is the function "textcolor" by conio.h that not run and not explicit error. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
  #include "Estado.h"
//#include "ListaEncadeada.h"
//#include "ListaEncadeada.cpp"
#include "conio.h"

using namespace std;
void EscreveDados(ListaEncadeada<Estado>& lista) {
	cout << "\t\t\t Criando arquivo de dados txt" << endl;
	ofstream escreve("arquivo.txt");
	if (!escreve.is_open()) {
		cout << "Falha ao abrir o arquivo" << endl;
		return;
	} else {
		Elemento<Estado> PercorreLista = lista.pos(0);
		while (PercorreLista.prox)
			escreve << PercorreLista.valor << endl;
		escreve.close();
	}
}

void LeDados(ListaEncadeada<Estado>& lista) {
	cout << "Lendo Arquivo" << endl;
	ifstream Ler("arquivo.txt");
	if (Ler.fail()) {
		cout << "Ocorreu um erro na Leitura do Arquivo" << endl;
	}
	else{

	}
	Ler.close();
}

int main() {
	string caminho;
	portaSerial s; // Cria a classe porta serial
	s.abrir(2); // abre uma conexão na COM2
	ListaEncadeada<Estado> lista;
	EscreveDados(lista);
	//Estado e; //TODO passar estado inicial
	//LeDados(lista);
	//Interface abrir;
	//abrir.Menu();
	//abrir.Controle(&s);
	//l.imprime();
	//TODO Criar função para salvar em arquivo
	//int a, b;
	//c_textcolor(RED);
	//cout << "Digite o número de leituras desejado:";
	//cin >> a;
	//cout << endl << "Escolha o tempo entre cada leitura: ";
	//cin >> b;
	//ExibeTemperatura g(a, b, &s);
	//g.ExibeTemperatura(a,b);
	//l.imprime();

	s.fechar();
	return 0;
}
What compiler do you use? Probably turbo C? "conio.h" is not part of the standard and outdated. See:

https://en.wikipedia.org/wiki/Conio.h
There's lots of code you haven't shown us, so it's hard to be specific.

Use your debugger to step through the code. That will enable you to find exactly where the exception is being thrown, and will enable you to look at the contents of memory to help find why it's being thrown.

In your loop on lines 15 - 16, nothing changes the value of PercorreLista.prox, so once the loop is entered, it will never finish.
@coder777 I use Eclipse and downloaded conio.c and conio.h by https://github.com/thradams/conio
Last edited on
@MikeyBoy I tried to change my code about you said, but this error occurred:
using namespace std;
void EscreveDados(ListaEncadeada<Estado>& lista) {
cout << "\t\t\t Criando arquivo de dados txt" << endl;
ofstream escreve("arquivo.txt");
if (!escreve.is_open()) {
cout << "Falha ao abrir o arquivo" << endl;
return;
} else {
Elemento<Estado> PercorreLista = lista.pos(0);
while (PercorreLista.prox) {
escreve << PercorreLista.valor << endl;
PercorreLista = PercorreLista.prox;
}
escreve.close();
}
}


no match for 'operator=' (operand types are 'Elemento<Estado>' and 'Elemento<Estado>*')
1) Please use code tags when posting code, to make it readable.

2) It's clear from the error message, if you'd taken the time to read it properly: you're trying to assign an an Elemento<Estado>* to an Elemento<Estado>. Obviously, those are not the same type; one is an object, and the other is a pointer, so the compiler can't possibly know how to do that.

You need to ensure that you're assigning things that it actually makes sense to assign. In this case, I suspect that PercorreLista should actually be a pointer to the first element in the list, not a copy of it.
Last edited on
Ok, I understood, maybe I need to change the "while" for "while(!PercorreLista.valor)".
Registered users can post here. Sign in or register to post.