简体   繁体   English

在对象c ++之前预期的主表达式

[英]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 说预期的初级表达和预期的初级表达st

and the square.h had class Square and SqState. square.h有类Square和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: 您可以注意到SqState有一个名为print()的方法,它在此处定义:

 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 ). 因此,此print()方法的参数是ostream实例的引用& )(实际上是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: 您应该在调用站点提供该实例,并且选项是std::cout以在标准控制台输出上打印文本:

sq.print(std::cout);

Similarly, for the other code in the testSquare() function. 同样,对于testSquare()函数中的其他代码。

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. 假设s和st已经定义,你不需要在那里放置ostream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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