Problem with a string attribute of an object

2

Hello!

I'm having a problem with a string member of my object. When I do the assignment object.word="some word", the program even compiles and runs, but when I print the content, totally different symbols appear.

Here is the code main:

#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 it looks like this:

Thanks for the help!

    
asked by anonymous 13.08.2018 / 13:33

1 answer

0

The point is that you are using printf to show what a string would be in pure C, ie char* , but you actually have std::string and not char* .

This problem is visible when compiling. See the warning I get regarding this line:

printf("%s, %s\n", p1.palavra, p2.palavra);
  

|| === Build: Debug in TestC ++ (compiler: GNU GCC Compiler) === |

     

... main.cpp | 60 | warning: format '% s' expects argument of type 'char *' ...

     

... main.cpp | 60 | warning: format '% s' expects argument of type 'char *' ...

     

|| === Build finished: 0 error (s), 2 warning (s) (0 minute (s), 1 second (s)) === |

Minimize compiler messages only to what is relevant

You can resolve by keeping printf with %s but for this you need to access the chars array that is within std::string through the c_str :

printf("%s, %s\n", p1.palavra.c_str(), p2.palavra.c_str());
//                             ^---                ^---

See it working on Ideone

A more in-line approach to C ++ would be to use cout because it was designed to deal with std::string :

cout << p1.palavra << ", " << p2.palavra << endl;

See this example on Ideone

As a final note, see what I said. You currently have a multi-method class that literally stores only string , and ends up replicating logic that already exists in string . If you only need the word then simplify and use a string normal and create a normal function for palavrasemelhante . This way it will not only be simpler but more natural.

    
13.08.2018 / 14:02