English 中文(简体)
C++ 差错:在“和”之前预期的初始动力
原标题:C++ error: expected initializer before ‘&’ token
  • 时间:2010-03-24 10:36:58
  •  标签:
  • c++

下述C++代码是两年前用10.1%的含水层机器汇编的。

#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";
  }
}
...
...

现在,我正试图将其编入乌班图9.10,我犯了错误:

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

参见:

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

该机器使用的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) 

各位能否让我对这一错误表示某种担忧?

增 编

P.D. 如果是这样的话,我就犯了错误:

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 名称space, so it s std:ostream

我认为,如果用于汇编,那就是错误的。

问题回答

www.un.org/Depts/DGACM/index_french.htm 班级是C++标准游览图书馆的一部分,在名称空间上界定:<代码>std。

so you probably should add std:: before ostream
or

using namespace std;

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

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

页: 1

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

页: 1 此外,你应当把指示作为参考(尽管在这种情况下,它赢得了一定伤害):

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




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签