-1

I have tried many ways to figure out this error, but still I am a beginner and can't figure it out. I have done exactly as in my handout. We are required to show an output of a matrix but due to some error my program is getting stuck. It says that, e.g., star has not been declared in this scope. I have tried removing using namespace std;, and I have tried changing the header files but still no solution.

#include <iostream> 
#include <stdlib.h> 
#include <iomanip>
 
using namespace std;
 
class  Matrix
{
    private:
        int numRows; 
        int numCols;
        float elements[3][3];
        
        public:
            Matrix (int rows=0, int cols=0 )
            {
                numRows=rows;
                numCols=cols;
            }
            
            friend ostream& operator<<(std::ostream&,Matrix&); 
            friend istream& operator>>(std::istream&,Matrix&);
            friend ostream& spaceFirst(std::ostream&);
            friend ostream& spaceBetween(std::ostream&);
            friend ostream& line(std::ostream&);
            friend ostream& newline( ostream&);
            friend ostream& star(std::ostream &);           
            friend ostream& sound(std::ostream&);
};

istream&operator>>(istream&input, Matrix&m)
{
    for (int i=0;i<m.numRows; i++)
    {
        for (int j=0;j,m.numCols;j++)
        {
            input>>m.elements[i][j];
        }
     } 
    return input; 
}
//defining the operator<<
ostream&operator<<(std::ostream &output, Matrix&m)
{
    for (int i=0;i<60; i++)
    {
        if(i==30)
        {
            output<<"Displaying th Matrix:";
        }
        else
        {
            output<<star;
        }
        
    }
    output<<newline;
    for(int r=0;r<m.numRows;r++)
    {
        output<<spacefirst<<line;
        for (int c=0;c<m.numCols;c++)
        {
            output<<spaceBetween<<m.elements[r][c]<<sound<<spaceBetween;
        }
        output<<spaceBetween<<line;
        output<<newline;
    }
    output<<newline;
    return output;
}
ostream & spacefirst(std::ostream & output)
{
    output<<setw(33);
    return output;
}

std::ostream & spaceBetween(std::ostream & output)
{
    output<<setw(4);
    return output;
}

std::ostream & line(std::ostream & output)
{
    output<<"|";
    return output;
}

std::ostream & newline(std::ostream & output)
{
    output<<endl;
    return output;
}

std::ostream & star(std::ostream & output)
{
    output<<"*";
    return output;
}

std::ostream & sound(std::ostream & output)
{
    output<<"\a";
    return output;
}
int main()
{
    Matrix matrix(3,3);
    cin>>matrix;
    cout<<matrix;
    system("Pause");
    return 0;
}
Yun
  • 3,056
  • 6
  • 9
  • 28
Abeer Naz
  • 1
  • 1
  • 2
    Please paste the exact error message – Tommy Andersen Aug 21 '21 at 10:51
  • 1
    Please try to create a [mre], with emphasis on the *minimal* bit. Then [edit] your question to show it, together with a full and complete copy-paste (as text) of the build output. Also please include comments in the code you show, on the lines where you get the errors. – Some programmer dude Aug 21 '21 at 10:52
  • 1
    Star is a function, which takes in an ostream.. It is not an overload of `<<`, you can't use it like `output< – ChrisMM Aug 21 '21 at 10:53
  • 1
    I'd start by fixing the obvious errors (case, spelling, and broken for-loop conditions). Second, declare the friended manipulators both inside the class, and outside (or implement them before first-use by operator<< and operator>>). – WhozCraig Aug 21 '21 at 10:54
  • 1
    @ChrisMM [yes, you can](https://godbolt.org/z/cdz34eKPE). A function taking an ostream reference single argument *and* returning an ostream reference is the very definition of a user-defined ostream manipulator, and is "sent" to the stream in precisely the form shown. But it has to be declared properly (the OP is relying on the friend decl to implicity declare it globally, and that dog don't hunt). – WhozCraig Aug 21 '21 at 10:56

1 Answers1

1

Friending those manipulators doesn't magically making the available in global namespace, and that's where they need to be for the friended free functions like operator << and operator >> for your matrix class to access them.

Foregoing the broken loop condition and obvious uncompilable misspelling of spaceFirst vs spacefirst, put the following before any use (before the class itself is an obvious choice):

#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

ostream &spaceFirst(std::ostream &);
ostream &spaceBetween(std::ostream &);
ostream &line(std::ostream &);
ostream &newline(ostream &);
ostream &star(std::ostream &);
ostream &sound(std::ostream &);

class Matrix
{
   // etc...

Keep the friend declarations as well (although honestly, most/all of these don't need friending).

Alternatively, you can relocate the bare manipulators before any usage within the source file as well.

See it live here.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • This has made my program error free but i am not getting my exact result as it must be a matrice – Abeer Naz Aug 21 '21 at 11:29
  • @AbeerNaz that has nothing to do with the original question. That's a bug in your code implementation, and worth of debugging now that your code compiles. – WhozCraig Aug 21 '21 at 15:02