简体   繁体   English

Eclipse多态性使用C ++ 11 shared_ptr错误

[英]Eclipse polymorphism using C++11 shared_ptr error

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 Eclipse表示该行有错误

f(b);

saying Invalid arguments ' Candidates are: void f(std::shared_ptr<A>) ' Invalid arguments ' Candidates are: void f(std::shared_ptr<A>) '
because a shared_ptr<B> has been passed. 因为已经传递了shared_ptr<B> This compiles and runs, and has output: 这编译并运行,并输出:

3
4

as expected. 正如所料。

The indexer and compiler have -std=c++11 specified. 索引器和编译器指定了-std = c ++ 11。
The compiler also has the symbol __GXX_EXPERIMENTAL_CXX0X__ defined. 编译器还定义了符号__GXX_EXPERIMENTAL_CXX0X__

Is there any way to get rid of this error and its red squiggles in Eclipse (preferably without modifying the source)? 有没有办法摆脱这个错误及其在Eclipse中的红色波形(最好不修改源代码)?

我建议您在可能不支持C ++ 11的设置中禁用静态代码分析插件Codan。

This has been fixed in later versions of CDT (I just tried it). 这已在CDT的后续版本中修复(我刚刚尝试过)。

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 您可以使用此存储库使用每晚构建的CDT:转到帮助,然后输入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/ 如果你不想使用一个夜间版本,你应该至少确保你有最新发布的版本(在撰写本文时它是8.1.1),使用http://download.eclipse.org/tools/cdt/发布/朱诺/

My full setup of eclipse with C++11 is found here: http://scrupulousabstractions.tumblr.com/post/36441490955/eclipse-mingw-builds 我在这里找到了完整的使用C ++ 11进行eclipse的设置: http//scrupulousabstractions.tumblr.com/post/36441490955/eclipse-mingw-builds

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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