【发布时间】:2012-12-04 00:35:51
【问题描述】:
给出以下示例代码:
#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);
说Invalid arguments ' Candidates are: void f(std::shared_ptr<A>) '
因为 shared_ptr<B> 已通过。这编译并运行,并有输出:
3
4
正如预期的那样。
索引器和编译器已指定 -std=c++11。
编译器还定义了符号__GXX_EXPERIMENTAL_CXX0X__。
有什么办法可以在 Eclipse 中消除这个错误及其红色曲线(最好不修改源代码)?
【问题讨论】:
标签: c++ eclipse c++11 polymorphism smart-pointers