1
Hello!
I’m having a problem with a member of the string type of my object. When I do the object.word assignment = "some word", the program even compiles and runs, but when I print the content, it shows totally different symbols.
Here the main code:
#include "palavra.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
Palavra p1;
Palavra p2;
p1.palavra = "abc";
p2.palavra = "Olani";
printf("%s, %s\n", p1.palavra, p2.palavra);
return(0);
}
Word.cpp:
#include "palavra.h"
#include <iostream>
//passar a arvore
Palavra Palavra::palavrasemelhante(const Palavra& p1, const Palavra& p2)
{
//se for igual as duas primeiras letras entao sao semelhantes
if(p1.palavra[0] == p2.palavra[0])
{
if(p1.palavra[1] == p2.palavra[1]){
return (p2);
}
}
return (p1);
}
const string Palavra::getString()
{
//retornar palavra para comparação de igual igual
return (this->palavra);
}
//sobrecarga de operador == para comparar dois objetos da classe Palavra
bool Palavra::operator == (Palavra &p2)
{
//palavra==p2.getstring ou desse jeitoif ( 0 == strcmp(palavra, p2.getString()))
if (palavra == p2.getString())
return (true);
else
return (false);
}
and Word. h:
#include <iostream>
#include <string>
using std::string;
class Palavra
{
private:
//int tamanho;
//string palavra;
public:
Palavra palavrasemelhante(const Palavra &p1, const Palavra &p2);
bool operator==(Palavra &p2);
const string getString();
string palavra;
};
When compiling and executing no error is pointed out, but when I see the content is like this:
Thanks for the help!
A class
Palavra
that encapsulates an attributestring palavra
to makepalavra.palavra
seems rather strange at least. Why not simplify and use twostrings
normal ? This even avoided having to define the operator==
that ended up complicating even more, and the same for thetamanho
which already obtain directly fromstring
withsize()
. In short, what is the purpose of all this abstraction/complication ?– Isac