'Use of undeclared identifier' error using Clang in a C++ project

7.3k views Asked by At

I am using Clang as a syntax checker in my C++ project. It's called through Flycheck in Emacs, and I am getting an annoying use of undeclared identifier error, the following Minimal Working Example to illustrates the problem:

In file testnamepace.cpp:

#include "testnamespace.hpp"

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

In file 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

In file 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;
}

Since the code compiles with no warning using g++ -Wall testnamespace.cpp -o testnamespace (gcc version 4.7.2) I am wondering if this is an error in my coding or if it's just a 'feature' of using Clang.

0

There are 0 answers