Eclipse polymorphism using C++11 shared_ptr error

别来无恙 提交于 2019-12-11 02:36:58

问题


Given the following sample code:

#include <iostream>
#include <memory>
using namespace std;

struct A {
public:
    A(int aa) : a(aa) {}
    int a;
    virtual ~A() {}
};
struct B : A {
public:
    B(int aa, int bb) : A(aa), b(bb) {}
    int b;
};

void f(shared_ptr<A> a){
    shared_ptr<B> b = dynamic_pointer_cast<B>(a);
    if (b) {
        cout << b->b << endl;
    } else {
        cout << a->a << endl;
    }
}

int main() {
    auto a = make_shared<A>(3);
    auto b = make_shared<B>(7, 4);
    f(a);
    f(b);
    return 0;
}

Eclipse indicates that there is an error on the line

f(b);

saying Invalid arguments ' Candidates are: void f(std::shared_ptr<A>) '
because a shared_ptr<B> has been passed. This compiles and runs, and has output:

3
4

as expected.

The indexer and compiler have -std=c++11 specified.
The compiler also has the symbol __GXX_EXPERIMENTAL_CXX0X__ defined.

Is there any way to get rid of this error and its red squiggles in Eclipse (preferably without modifying the source)?


回答1:


This has been fixed in later versions of CDT (I just tried it).

You can use a nightly build of CDT using this repository: Go to Help, and enter the url http://download.eclipse.org/tools/cdt/builds/kepler/nightly

If you don't feel like using a nighty build, you should at least make sure you have the latest released version (at this writing it is 8.1.1), using http://download.eclipse.org/tools/cdt/releases/juno/

My full setup of eclipse with C++11 is found here: http://scrupulousabstractions.tumblr.com/post/36441490955/eclipse-mingw-builds




回答2:


I recommend you disable the Static Code Analysis plugin Codan in the settings it probably does not support C++11 yet.



来源:https://stackoverflow.com/questions/13905283/eclipse-polymorphism-using-c11-shared-ptr-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!