繁体   English   中英

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

[英]expected primary expression before object c++

这是我的代码

#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
}

** .. **之间的陈述,发生了错误。 说预期的初级表达和预期的初级表达st

square.h有类Square和SqState。

请帮帮我,实际问题在哪里

考虑这段代码:

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

您可以注意到SqState有一个名为print()的方法,它在此处定义:

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

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

sq.print(std::cout);

同样,对于testSquare()函数中的其他代码。

你可能想要写一些类似的东西

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);**

假设s和st已经定义,你不需要在那里放置ostream。

暂无
暂无

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

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