gpt4 book ai didi

C++ 错误 : expected initializer before ‘&’ token

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:54:21 25 4
gpt4 key购买 nike

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

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

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

这是指以下行:

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

native 使用的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)

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

谢谢

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’

最佳答案

作为 C++ 标准库中的所有内容,ostream 位于 std 命名空间中,因此它是 std::ostream

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

关于C++ 错误 : expected initializer before ‘&’ token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2506909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com