C++;错误:在‘之前应为初始值设定项&’;代币 两年前在SUSE10.1 Linux机器上编译的一段C++代码。 #ifndef DATA_H #define DATA_H #include <iostream> #include <iomanip> inline double sqr(double x) { return x*x; } enum Direction { X,Y,Z }; inline Direction next(const Direction d) { switch(d) { case X: return Y; case Y: return Z; case Z: return X; } } inline ostream& operator<<(ostream& os,const Direction d) { switch(d) { case X: return os << "X"; case Y: return os << "Y"; case Z: return os << "Z"; } } ... ...

C++;错误:在‘之前应为初始值设定项&’;代币 两年前在SUSE10.1 Linux机器上编译的一段C++代码。 #ifndef DATA_H #define DATA_H #include <iostream> #include <iomanip> inline double sqr(double x) { return x*x; } enum Direction { X,Y,Z }; inline Direction next(const Direction d) { switch(d) { case X: return Y; case Y: return Z; case Z: return X; } } inline ostream& operator<<(ostream& os,const Direction d) { switch(d) { case X: return os << "X"; case Y: return os << "Y"; case Z: return os << "Z"; } } ... ...,c++,C++,这是指: inline ostream& operator<<(ostream& os,const Direction d) 你能给我一些关于这个错误的提示吗 谢谢 p.D.如果我使用std::ostream,我会得到以下错误: data.h:20: error: declaration of ‘operator<<’ as non-function data.h:20: error: ‘ostream’ was not declared in this

这是指:

inline ostream& operator<<(ostream& os,const Direction d)
你能给我一些关于这个错误的提示吗

谢谢


p.D.如果我使用std::ostream,我会得到以下错误:

data.h:20: error: declaration of ‘operator<<’ as non-function
data.h:20: error: ‘ostream’ was not declared in this scope
data.h:20: error: ‘os’ was not declared in this scope
data.h:20: error: expected primary-expression before ‘const’

<代码>数据:h:20:错误:“操作符< P>声明<代码> oSturis/Cuff>类是C++标准IoSt流库的一部分,并在命名空间<代码> STD< /CONT>

中定义 因此,您可能应该在
ostream
之前添加
std::

但是,如其中一条评论所述:

绝不应使用
使用名称空间
std
在标头中,因为它可以传播 转到其他文件


作为C++标准库中的所有内容, OsFrase/COD>存在于<代码> STD命名空间中,所以它是<代码> STD::OFSUCTp>


我相信,如果这用于编译,这是错误的

您忘记在每次出现的
ostream
前面加上std::了。此外,您应该将方向作为参考(在这种情况下,它不会造成伤害):


inline std::ostream&operator缺少分号

inline Direction next(const Direction d)
{
   switch(d)
   {
     case X: return Y;
     case Y: return Z;
     case Z: return X;
   }
}**;**//missing semicolon

inline ostream& operator<<(ostream& os,const Direction d)
{
  switch(d)
  {
     case X: return os << "X";
     case Y: return os << "Y";
     case Z: return os << "Z";
  }
}`    
下一个内联方向(常量方向d)
{
开关(d)
{
案例X:返回Y;
案例Y:返回Z;
案例Z:返回X;
}
}**;**//缺少分号

内联ostream和operator-1。你不应该在头文件中使用
使用namespace std
,因为它可能会传播到其他文件。这不是在回答中不提及它的原因,他可能在cpp文件中也有同样的问题(很有可能)。但我同意我应该加上一条免责声明:)我自己从来没有这样做过,所以我没有考虑过——修正完成了,如果你原谅我,你可以收回你的反对票:)@f4:原谅并删除<代码>:)
嗨,我这样做了,然后我得到了数据。h:23:错误:声明'operator@Werner:不太好。你应该用新信息编辑你的问题。也就是说,使用backticks:`code goes here`获取一些格式:
code goes here
。我用反斜杠转义了前面的反斜杠。如果我使用std::ostream,我会得到错误:
\data.h:20:error:declaration'operator@Werner:从技术上讲,不能保证
包含
(尽管我很难想象一个实现如何定义
std::cout
)。所以你应该包括
。无论如何,您的
operator@Werner只需将std::添加到ostream的每个引用中,而不仅仅是第一个。处理对枚举的引用有什么好处?无论是作为引用还是
int
,它的数据量都是相同的:32位还是64位。作为常量引用传递内置或枚举不是一个好的做法。通过值传递它们,就像在操作代码中一样,是非常好的。这就是为什么我说:
(在这种情况下,它不会有任何伤害):-)
这并不是说它不会有任何伤害:它只是不适用于这种情况。老实说,你真的认为它会改变什么吗?正如sbi在他的一条评论中所说:“不管怎样,你的操作员在
os
的定义中也使用了
std::ostream
?你现在得到的错误表明你没有。为什么你认为他在第一个内联函数中缺少分号,而在第二个内联函数中没有?
data.h:20: error: declaration of ‘operator<<’ as non-function
data.h:20: error: ‘ostream’ was not declared in this scope
data.h:20: error: ‘os’ was not declared in this scope
data.h:20: error: expected primary-expression before ‘const’
using namespace std;
inline std::ostream& operator<<(std::ostream& os,const Direction& d)
inline Direction next(const Direction d)
{
   switch(d)
   {
     case X: return Y;
     case Y: return Z;
     case Z: return X;
   }
}**;**//missing semicolon

inline ostream& operator<<(ostream& os,const Direction d)
{
  switch(d)
  {
     case X: return os << "X";
     case Y: return os << "Y";
     case Z: return os << "Z";
  }
}`