Operator overload in C ++

2

I'm learning operator overload. For this I am trying to overload the * operator so that it is interpreted as | object class string * variable type int = object class string concatenated with it so many times the type variable int. Probably there must already be something like this in standard c ++, but I'm doing it just to test. The problem is that the code is not working. The error messages are in the image below

Belowthecode:

Interfacefile.h

#pragmaonce#include"stdafx.h"
#include <string>
using namespace std;

class palavra
{
private:
    string word;
public:
    string &operator*(const int &);
    void get_word();
    void print_word() const;
};

Implementation.cpp file

#include "stdafx.h"
#include "interface.h"
#include <iostream>

string palavra::&operator*(const int &numero)
{
    string word2=word;
    int cont;
    for (cont = 1; cont < numero; cont++)
        word += word2;

    return word;
}

void palavra::get_word()
{
    cout << "Digite a palavra a ser concatenada com ela mesma: ";
    getline(cin, word);
    cout << endl << endl;
}

void palavra::print_word() const
{
    cout << word;
}

Main.cpp file

// Sobrecargadeoperadores.cpp: Define o ponto de entrada para a aplicação de 
console.
//
//Sobrecarregar o operador para * para que a operação string * x faça a 
concatenação da string nela mesma x vezes
#include "stdafx.h"
#include <iostream>
#include "interface.h"
using namespace std;
int main()
{
    palavra word1;//a palavra a ser concatenada com ela mesma
    int numero;// a quantidade de vezes em que vai ocrrer a concatenação

    word1.get_word();

    cout << "\nDigite a quabtidade de vezes que vc quer concatenar a palavra 
    " << word1.print_word() << ": ";
    cin >> numero;

    cout << "A palavra " << word1.print_word() << "concatenada " << numero 
    << "vezes: " << word1*numero;

#if WIN32
    system("PAUSE");
#endif
    return 0;
}

implemenation.cpp (5): error C2589: '&': invalid token on the right side of '::'

\ implemenation.cpp (5): error C2062: type 'unknown-type' unexpected

implemenation.cpp (6): error C2143: syntax error: ';' absent before '{' cpp (6): error C2447: '{': missing function header (formal list of old style?)

sobrecargadeoperadores.cpp (15): error C2679: '

asked by anonymous 12.09.2017 / 04:49

1 answer

1

Here is a working example based on your code illustrating how to implement the operator* multiplication operator and the operator<< and operator>> :

#include <iostream>
#include <cstring>

/* ************************************************************************** */
/* *                               INTERFACE                                * */
/* ************************************************************************** */

class Palavra
{
    public:

        Palavra( void );
        virtual ~Palavra( void );

        Palavra operator*( int n ) const;

        friend std::ostream &operator<<( std::ostream &out, Palavra obj );
        friend std::istream &operator>>( std::istream &in, Palavra &obj );

    private:

        std::string m_palavra;

};

/* ************************************************************************** */
/* *                             IMPLEMENTACAO                             *  */
/* ************************************************************************** */

Palavra::Palavra( void )
{
}

Palavra::~Palavra( void )
{
}

Palavra Palavra::operator*( int n ) const
{
    Palavra p;

    for( int i = 0; i < n; i++ )
        p.m_palavra.append(m_palavra);

    return p;
}

std::ostream &operator<<( std::ostream &out, Palavra obj )
{
    out << obj.m_palavra;
    return out;
}


std::istream &operator>>( std::istream &in, Palavra &obj )
{
    in >> obj.m_palavra;
    return in;
}


/* ************************************************************************** */
/* *                                    main()                              * */
/* ************************************************************************** */

int main( void )
{
    Palavra p;
    int qtd = 0;

    std::cout << "Digite a palavra a ser concatenada com ela mesma: ";
    std::cin >> p;

    std::cout << "Digite a quantidade de vezes que vc quer concatenar a palavra [" << p << "] com ela mesma: ";
    std::cin >> qtd;

    std::cout << "A palavra [" << p << "] concatenada " << qtd << " vezes com ela mesma: " << p * qtd << std::endl;

    return 0;
}
    
13.09.2017 / 16:50