像“类型限定符'std'这样的错误必须是C ++中的结构或类名

时间:2013-12-11 11:24:02

标签: c++

当我编译下面的代码时,我得到了类型限定符' std'之类的错误。必须是结构或类名。请找到下面的代码 -

#include <iostream>

int foo(int i)
{
  return 2;
}

double foo(double d)
{
  return 4.0;
}

struct Computer
{
  int foo(int i)
  {
    return 8; 
  }
};

struct Gateway : public Computer
{
  double foo(double d)
  {
    return 16.0; 
  }
};

int main(int argc, char** argv)
{
  Gateway g;

  std::cout << foo(1) + foo(1.0) + g.foo(1) + g.foo(1.0) << std::endl;

  return 0;
}

请检查并建议如何解决。

1 个答案:

答案 0 :(得分:2)

Your code compiles and runs fine.

您收到此错误是因为您的编译器不符合the C++ standard

Turbo C++可怕的过时了。

现在是时候获得一个新的,免费的,符合标准的编译器。例如Clang


在回答第二个问题时,在评论中,Gateway::foo 隐藏了 Computer::foo,这就是为Gateway::foo调用int的原因和double参数。如果这不符合您的意图,您可以改变struct,如此:

struct Gateway : public Computer
{
    using Computer::foo;

    double foo(double d)
    {
        return 16.0;
    }
};

See it run!