AnswerBun.com

Why is my friend function not working when put into a separate source code file in C++?

I have the following header file:

TwoDimArray.h

class TwoDimArray {
    int** data;
    int rows, cols;
public:
    TwoDimArray(int, int);
    TwoDimArray(const TwoDimArray& orig);
    virtual ~TwoDimArray();
    const TwoDimArray& operator=(const TwoDimArray &op2);
    void random();
    TwoDimArray operator+(TwoDimArray &op2);
    TwoDimArray operator+(const int &in);
    TwoDimArray operator*(TwoDimArray &op2);
    int* operator[](int i);
    friend ostream& operator<<(ostream &os, TwoDimArray &twoDimArray);
};

And I define the friend function in my .cpp file as follows:

TwoDimArray.cpp snippet

ostream& TwoDimArray::operator<<(ostream &os, TwoDimArray &twoDimArray)
{
    for (int i = 0; i < twoDimArray.rows; i++)
    {
        os << "{";
        for (int j = 0; j < twoDimArray.cols; j++)
        {
            os << twoDimArray.data[i][j];
             if (j < twoDimArray.cols - 1)
            {
                os << ", ";
            }
        }
        os << "}";
        if (i < twoDimArray.rows - 1)
        {
            os << ", ";
        }
     }
   os << endl;
    return os;
}

For some reason, I get the following error message: TwoDimArray.cpp:171:10: error: 'std::ostream& TwoDimArray::operator<<(std::ostream&, TwoDimArray&)' must have exactly one argument

After some research I found varying answers with some people referring to the fact that "this" is implied as well as the fact that you can’t define friend functions within classes.

But this does not make sense to me because originally, there was only a .cpp file with the main method and the TwoDimArray class with no header file. And the friend function was defined within that class and there was no such error:

enter image description here

So it looks like it is possible to have a friend function defined within a class, but something weird happens when you put a declaration in a header file.

Stack Overflow Asked by katie1245 on February 22, 2021

1 Answers

One Answer

Your declaration of the friend function is correct. But in your .cpp file, this definition:

ostream& TwoDimArray::operator<<(ostream &os, TwoDimArray &twoDimArray) {

is defining a member function of TwoDimArray, which is not what you declared in the .h file. (The compiler error message is telling you this).

Instead, you need to define a free function like this:

ostream& operator<<(ostream &os, TwoDimArray &twoDimArray) {

that matches the friend declaration.

Answered by cigien on February 22, 2021

Add your own answers!

Related Questions

Undefined reference to vtable

19  Asked on November 20, 2021 by ryang

     

Using wildcards in between a C# DataTable Select statement

3  Asked on November 20, 2021 by bugaboo

   

SendKeys.send is sending more than one character

1  Asked on November 20, 2021 by onur-klekci

   

pwsh -Command is removing quotation marks

3  Asked on November 20, 2021 by chris-g

 

Django when is concrete inheritance appropriate

1  Asked on November 20, 2021 by natehawkboss

   

Get the sum of numbers from array objects

6  Asked on November 20, 2021 by askmen

 

Function to keep listening a service Angular 8

2  Asked on November 20, 2021 by rob-none

         

How to perform mathematical operation which is stored as character in R?

1  Asked on November 20, 2021 by hidden-layer

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.