yyy中的错误xxx没有命名类型

时间:2016-12-04 14:50:34

标签: c++ c++11 compiler-errors g++ c++14

编译文件error_xxx_does_not_name_a_type.cpp,为什么我会收到错误
error_xxx_does_not_name_a_type.cpp:5:28: error: ‘A’ in ‘struct std::pair<bool, int>’ does not name a type std::pair<bool, int> ::A::B::C::D::get_i()

// error_xxx_does_not_name_a_type.h
#pragma once
#include <utility>

namespace A{ namespace B{ namespace C{
struct D
{   
    std::pair<bool, int> get_i();
    std::pair<bool, int> get_j();
    std::pair<bool, int> get_k();
    int get_l();
};  
}}}

// error_xxx_does_not_name_a_type.cpp
#include "error_xxx_does_not_name_a_type.h"

#if 1 // gives me the error
std::pair<bool, int> ::A::B::C::D::get_i()
{ return {true, 10}; }
#endif
// But none of the below do
// missing ::
std::pair<bool, int> A::B::C::D::get_j()
{ return {true, 10}; }
// trailing return type
auto ::A::B::C::D::get_k()->
    std::pair<bool, int>
{ return {true, 10}; }
// return type int
int ::A::B::C::D::get_l()
{ return 10; }

我已使用g++ -Wall -Wextra -std=c++14 -c error_xxx_does_not_name_a_type.cppg++ (Ubuntu 5.3.0-3ubuntu1~14.04) 5.3.0 20151204

编译

1 个答案:

答案 0 :(得分:1)

Here是一个复制的例子:

struct T{};
T A();
T ::A() { return T(); }

// error: no 'int T::A()' member function declared in class 'T'

(注意GCC尝试的自动 - int返回类型是C的保留!)

这是一个C ++的怪癖。你会混淆解析器,因为看起来你正试图用std::pair<bool, int> ::A::B::C::D做一些事情,这显然不存在。 (这里忽略了间距,尽管我们通常会这样写。)

请忽略::。通过功能定义,您无论如何都不需要它。

相关问题