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

Oct 21, 2019 at 8:37pm
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;
}
Oct 22, 2019 at 6:29am
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
Oct 22, 2019 at 10:36am
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.
Oct 22, 2019 at 10:59am
@coder777 I use Eclipse and downloaded conio.c and conio.h by https://github.com/thradams/conio
Last edited on Oct 22, 2019 at 11:01am
Oct 22, 2019 at 11:15am
@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>*')
Oct 22, 2019 at 12:13pm
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 Oct 22, 2019 at 12:18pm
Oct 22, 2019 at 3:02pm
Ok, I understood, maybe I need to change the "while" for "while(!PercorreLista.valor)".
Topic archived. No new replies allowed.