简体   繁体   中英

Eclipse CDT can not resolve method in template class

I have an issue with Eclipse CDT indexer/code analyzer. When I write the following code in the editor, it shows me an error on const int* p = xf(); - Invalid arguments Candidates are: int * f() . So for some reason it doesn't recognize method const T* f() const in class B.

template<typename T>
class A {
public:
    const T* f() const { return 0; }
};

template<typename T>
class B : A<T> {
public:
    using A<T>::f;
    T* f() { return 0; }
};

void main() {
    const B<int> x;
    const int* p = x.f();
}

Any ideas why does it happen and how to solve the problem?

Why is happening?

Because the CDT C++ parser is failing to recognize that using A<T>::f declares in class B a member function that satisfies the call const int* p = xf() in main() . You can verify this by adding a const qualifier to T* B::f() . This satisfies the parser (but defeats the purpose of the member function).

How to solve the problem?

Well, it does seem peculiar to have const T* f() const in a base class and T* f() in a derived class. Unless there is some compelling reason for this, you could declare both member functions in either A or B (probably A ). Alternatively, but unattractively, you could:

  • Give them different names, maybe f and f_const , or
  • Remove using A<T>::f; and replace const int* p = x.(); with the explicitly qualified call const int* p = xA<int>::f();

You could also ignore the problem, since the parser's confusion doesn't stop your program building successfully in the IDE. (Nice to have a realtime C++ parser, except when it doesn't agree with your compiler).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM