简体   繁体   中英

expected primary expression before object c++

here is my code

#include "Square.h"
#include "tools.hpp"

ostream&
Square :: print(ostream& s)
{
  return s << "Square [" << row << " " << column << "]" << endl;
}

ostream&
SqState :: print(ostream& sq)
{

    return sq << "value: " << sq_value;
}
void testSquare();
void testSqState();
int main()
{
    banner();

    testSquare();
    testSqState();
    bye();

}

void testSqState()
{
   SqState sq('-', 4, 0);
   sq.print(ostream s); // << Error occurs here
}

void testSquare()
{
    Square s(4, 0);
    s.print(ostream st);  // << Error occurs here
}

The statements between the **..**, there were the error occured. saying that expected primary - expression s and expected primary - expression st

and the square.h had class Square and SqState.

please help me where is the actuall problem is

Considering this code:

 SqState sq('-', 4, 0); sq.print(ostream s); 

You can note that SqState has a method named print() , that is defined here:

 ostream& SqState :: print(ostream& sq) { return sq << "value: " << sq_value; } 

So, the parameter to this print() method is a reference ( & ) to an instance of ostream (actually, std::ostream ).
You should provide that instance at the call site, and an option is std::cout to print text on the standard console output:

sq.print(std::cout);

Similarly, for the other code in the testSquare() function.

You probably meant to write something like

void testSqState() {
    SqState sq('-', 4, 0);
    sq.print(std::cout);
}

void testSquare() {
    Square s(4, 0);
    s.print(std::cout);
}
**sq.print(ostream s);**
**s.print(ostream st);**

You don't need to put ostream there, assuming s and st are already defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM