Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when using overloaded operator () C++

Here is a C++ code, it is simple, but I get error which I don't understand.
I don't know how else to use overloaded operator (), but the way I did.
Program should do the following.

Type your word: Programming
n: 5
P
Pr
Pro
Prog
Progr

Error I get:

no match for 'operator<<' in 'std::cout << Word::operator()(int)(n)'

Word.h

#ifndef WORD_H
#define WORD_H


class Word
            {
            private:
                    char *str;
            public:
                    Word();
                    ~Word();
                    Word operator()(int); //overloading operator ()
                    friend std::ostream& operator<<(std::ostream&,Word&);
                    friend std::istream& operator>>(std::istream&,Word&);
            };

#endif

Word.cpp

#include <iostream>
#include <cstring>
#include "Word.h"

Word::Word()
    {
    str=new char[100];
    }

Word::~Word()
    {
    delete[] str;
    }


Word Word::operator()(int d)  //overloading operator ()
    {
    Word pom;
    strncpy(pom.str,str,d);
    return pom;
    }


std::ostream& operator<<(std::ostream& out,Word& s)
    {    
    out<<s.str; return out;
    }


std::istream& operator>>(std::istream& in,Word& s)
    {
    in>>s.str; return in;
    }

main.cpp

#include<iostream>
#include "Word.h"


int main()
{
   Word r;
   std::cout<<"Type your word: "; 
   std::cin>>r;

   int n;
   std::cout<<"n:"; 
   std::cin>>n;

   for (int i=1; i<=n; i++) std::cout << r(i) << std::endl; //error when using r(i)
}
like image 749
misty Avatar asked Jun 29 '26 19:06

misty


1 Answers

The problem is the signature of

std::ostream& operator<<(std::ostream& out,Word& s);

This accepts a Word by reference.

Word operator()(int);

returns a Word by value. In the expression

cout << r(i);

you end up trying to bind a temporary value to a reference, which is forbidden by the language. Only a reference to const can bind to a temporary value. Change your operator<< signature (in the declaration and definition) to:

std::ostream& operator<<(std::ostream& out, const Word& s);

and it should work. This is also a sensible choice, since operator<< should not change the Word argument.

like image 83
pmr Avatar answered Jul 02 '26 08:07

pmr