相关疑难解决方法(0)

是否需要std :: unique_ptr <T>才能知道T的完整定义?

我在头文件中有一些代码如下:

#include <memory>

class Thing;

class MyClass
{
    std::unique_ptr< Thing > my_thing;
};
Run Code Online (Sandbox Code Playgroud)

如果我有一个CPP这个头不包含的Thing类型定义,那么这并不在VS2010 SP1的编译:

1> C:\ Program Files(x86)\ Microsoft Visual Studio 10.0\VC\include\memory(2067):错误C2027:使用未定义类型'Thing'

替换std::unique_ptrstd::shared_ptr和编译.

所以,我猜这是当前VS2010 std::unique_ptr的实现,需要完整的定义,而且完全依赖于实现.

或者是吗?它的标准要求中是否有某些东西使得std::unique_ptr实施只能使用前向声明?感觉很奇怪,因为它应该只有一个指针Thing,不应该吗?

c++ stl visual-studio-2010 unique-ptr c++11

236
推荐指数
5
解决办法
4万
查看次数

具有不完整类型的std :: unique_ptr将无法编译

我正在使用pimpl-idiom std::unique_ptr:

class window {
  window(const rectangle& rect);

private:
  class window_impl; // defined elsewhere
  std::unique_ptr<window_impl> impl_; // won't compile
};
Run Code Online (Sandbox Code Playgroud)

但是,我在第304行的第304行收到有关使用不完整类型的编译错误<memory>:

' sizeof'到不完整类型' uixx::window::window_impl的应用无效' '

据我所知,std::unique_ptr应该可以使用不完整的类型.这是libc ++中的错误还是我在这里做错了什么?

c++ unique-ptr incomplete-type libc++

179
推荐指数
5
解决办法
5万
查看次数

包含一个以 std::unique_ptr&lt;T&gt; 作为字段的类,而“T”是不完整类型

std::unique<B>我为incomplete type创建了一个小测试用例B

测试.h

#pragma once
#include <memory>
class B;   //<--- compile error here
class Test{
    std::unique_ptr<B> bPtr;   
    //#1 need to move destructor's implementation to .cpp
    public: ~Test();
};
Run Code Online (Sandbox Code Playgroud)

测试.cpp

#include "Test.h"
class B{};
Test::~Test(){}  //move here because it need complete type of B
Run Code Online (Sandbox Code Playgroud)

主程序

#include <iostream>
#include "Test.h"
using namespace std;
int main(){
   Test test;
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:-

/usr/include/c++/4.8.2/bits/unique_ptr.h:65:22:错误:“sizeof”对不完整类型“B”的无效应用

据我了解,编译器告诉我这 B是一个不完整的类型(in main.cpp),因此它无法B正确删除。

但是,在我的设计中,我不想main.cpp拥有完整B.
粗略地说,这是一个粉刺。

有没有好的解决方法?

这里有一些类似的问题,但没有一个提出干净的解决方法。 …

c++ pimpl-idiom unique-ptr incomplete-type c++11

5
推荐指数
0
解决办法
184
查看次数