C++ 错误:“&”标记之前的预期初始化程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2506909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:44:43  来源:igfitidea点击:

C++ error: expected initializer before ‘&’ token

c++

提问by Open the way

the following piece of C++ code compiled two years ago in a suse 10.1 Linux machine.

以下是两年前在 suse 10.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";
  }
}
...
...

Now, I am trying to compile it on Ubuntu 9.10 and I get the error:

现在,我尝试在 Ubuntu 9.10 上编译它,但出现错误:

data.h:20: error: expected initializer before ‘&' token

which is referred to the line of:

这是指:

inline ostream& operator<<(ostream& os,const Direction d)

the g++ used on this machine is:

这台机器上使用的g++是:

Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.1-4ubuntu9' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu9) 

Could you give me some hint about this error?

你能给我一些关于这个错误的提示吗?

Thanks

谢谢

P.D. If i do std::ostream, I get the errors:

PD 如果我执行 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'

回答by sbi

As everything in the C++ standard library, ostreamlives in the stdnamespace, so it's std::ostream.

因为一切都在C ++标准库,ostream住在std命名空间,所以它的std::ostream

I believe that, if this used to compile, this was in error.

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

回答by f4.

The ostreamclass is part of the C++ standard iostream library, and is defined in the namespace std

ostream类是C ++标准iostream库的一部分,并在命名空间定义std

so you probably should add std::before ostream
or

所以你可能应该std::ostream
或之前添加

using namespace std;

but, as stated in one of the comments :

但是,正如评论之一所述:

You should never use using namespace stdin a header as it can propagate to other files.

您永远不应该using namespace std在标头中使用,因为它可以传播到其他文件。

回答by Alexandre Hamez

You forgot to put std:: in front of each occurrence of ostream. Also, you should take Direction as a reference (while in this case, it won't hurt):

您忘记将 std:: 放在每次出现的ostream. 此外,您应该将方向作为参考(在这种情况下,它不会受到伤害):

inline std::ostream& operator<<(std::ostream& os,const Direction& d)

回答by JBC--

you are missing a semicolon

你少了一个分号

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";
  }
}`