Eclipse多态性使用C ++ 11 shared_ptr错误 [英] Eclipse polymorphism using C++11 shared_ptr error

查看:363
本文介绍了Eclipse多态性使用C ++ 11 shared_ptr错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下示例代码:

  #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表示行上有错误

  f(b); 

无效的参数候选项有:void f(std :: shared_ptr< A>)'

,因为已经通过了 shared_ptr< B> 。这个编译和运行,并有输出:

  3 
4



索引器和编译器指定了-std = c ++ 11。

编译器还具有定义的符号 __ GXX_EXPERIMENTAL_CXX0X __



有任何方法删除这个错误和它在Eclipse中的红色波纹(最好不修改源)?

解决方案

的CDT(我刚试过)。



您可以使用此存储库每晚构建一个CDT:
转到帮助,然后输入url http://download.eclipse.org/tools/cdt/builds/kepler/nightly



如果你不想使用nighty版本,你应该至少确保你有最新的版本(在这篇写作是8.1.1),使用 http://download.eclipse.org/tools/cdt/releases/juno/



我完全使用C ++ 11完成eclipse的设置:
http://scrupulousabstractions.tumblr.com/post/36441490955/eclipse-mingw-builds


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)?

解决方案

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

这篇关于Eclipse多态性使用C ++ 11 shared_ptr错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆