5

我在我的 C++ 项目中使用Clang作为语法检查器。它是通过 Emacs 中的 Flycheck 调用的,我遇到了一个烦人的 use of undeclared identifier错误,下面的最小工作示例来说明这个问题:

在文件中testnamepace.cpp

#include "testnamespace.hpp"

int main() {
    const unsigned DIM = 3;
    testnamespace::A<DIM> a;
    a.f();
    a.g();
    return 0;
}

在文件中testnamespace.hpp

#ifndef testnamespace_h
#define testnamespace_h

#include <iostream>

namespace testnamespace {
    // My code uses lots of templates so this MWE uses a class template
    template <unsigned DIM> class A;
}

template <unsigned DIM>
class testnamespace::A{
public:
    static const unsigned dim = DIM;

    A() {std::cout << "A(): dim = " << dim << std::endl;}

    // in my case some functions are defined in a .hpp file...
    void f() {
        std::cout << "call f()" << std::endl;
    }

    // ...and others are defined in a .ipp file
    void g();
};

#include "testnamespace.ipp"
#endif

在文件中testnamespace.ipp

template <unsigned DIM>
void testnamespace::A<DIM>::g() {
//   ^^^^^^^^^^^^^ this results in the following error:
//   testnamespace.ipp:2:6:error: use of undeclared identifier 'testnamespace' (c/c++-clang)
    std::cout << "g()" << std::endl;
}

由于代码使用g++ -Wall testnamespace.cpp -o testnamespace(gcc 版本 4.7.2)编译时没有警告,我想知道这是否是我的编码错误,或者它只是使用 Clang 的“功能”。

4

0 回答 0