1
I created a class to display a string at the terminal, but in addition to displaying the string, also, displays the following message: "segmentation failure". I debug with GDB, but could not resolve the error. Follow the code below:
#ifndef CONNECTION_H
#define CONNECTION_H
#include<string>
#include<iostream>
using std::string;
class Connection {
public:
Connection();
~Connection();
string conexao;
string getConexao();
};
#endif // CONNECTION_H
#include"teste.h"
Connection::Connection()
{
conexao = "sete de setembro";
}
string Connection::getConexao()
{
std::cout << conexao << std::endl;
}
Connection::~Connection()
{
}
#include "teste.h"
int main(void)
{
Connection con;
con.getConexao();
return 0;
}
Bigown, Even without the return the error message continues to be displayed.
– L.Hora
But it needs feedback. I showed you working. If your code has something different, I can’t help. I can only talk about what I’m seeing. Transform into
void
also works, but it would be strange a function that starts withget
return nothing. http://ideone.com/KTs5zg– Maniero
I put get to refer to an even return function. I didn’t know that the compiler would understand get, because I thought it was optional.
– L.Hora
@L.Time A clue that the problem was this is the alert that appears on your compiler when you turn on all alerts. In GCC, this can be done with -Wall and gives the following message:
warning: no return statement in function returning non-void [-Wreturn-type]
 }
, which means you’re missing something in your function. As fellow @bigown said, get is kind of a default for functions that return values. Don’t use for functions that print things.– Pablo Almeida
Got it, Thanks to Bigown and you Pablo for the clarifications.
– L.Hora