这是一个非常无聊的错误,但我不知道这里发生了什么.
有大量的pimpl例子,但我不明白为什么这不起作用(这或多或少的例子之一,但我没有看到差异).
我有一个非常简单的Pimpl示例,但它不会工作.
// Foo.hpp
#include <boost/scoped_ptr.hpp>
class Foo
{
struct Bar;
//boost::scoped_ptr<Bar> pImpl;
Bar* pImpl;
public:
Foo();
~Foo() {}
int returnValue();
private:
};
Run Code Online (Sandbox Code Playgroud)
和
// Foo.cpp
#include "foo.hpp"
struct Foo::Bar
{
Bar() {}
~Bar() {}
int value;
};
Foo::Foo() : pImpl(new Bar())
{
pImpl->value = 7;
}
int Foo::returnValue() {
return *pImpl->value;
}
Run Code Online (Sandbox Code Playgroud)
编译这个给了我错误.C2100:非法间接.
谢谢.
GMa*_*ckG 10
int returnValue() 应该是一个成员函数:
// vvvvv
int Foo::returnValue() {
return pImpl->value; // no need to dereference, value isn't a pointer
}
Run Code Online (Sandbox Code Playgroud)
在定义实现类之后,您需要定义构造函数,复制构造函数,复制赋值运算符和析构函数.(否则隐式析构函数很危险,scoped_ptr不会让你这样做):
// Foo.hpp
#include <boost/scoped_ptr.hpp>
class Foo
{
struct Bar;
boost::scoped_ptr<Bar> pImpl;
public:
Foo();
~Foo();
int returnValue(); // could be const (so should be)
private:
// just disable copying, like scoped_ptr
Foo(const Foo&); // not defined
Foo& operator=(const Foo&); // not defined
};
Run Code Online (Sandbox Code Playgroud)
和:
// Foo.cpp
#include "foo.hpp"
struct Foo::Bar
{
int value;
};
Foo::Foo() :
pImpl(new Bar())
{
pImpl->value = 7;
}
Foo::~Foo()
{
// okay, Bar defined at this point; scoped_ptr can work
}
int Foo::returnValue()
{
return pImpl->value;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
699 次 |
| 最近记录: |