错误:隐式删除,因为默认定义格式不正确(结构向量)
error: implicitly deleted because the default definition would be ill-formed (vector of structs)
我在编译C++程序时遇到问题。非常感谢此错误的一些帮助。在头文件中,我有这个:
struct workerT{
workerT() : status(true), threadSem(0){}
bool status;
std::function<void(void)> func;
semaphore threadSem;
};
std::vector<workerT> workers;
在我的 .cc 文件中,我正在尝试像这样初始化该向量:
fill(workers.begin(), workers.end(), workerT());
此操作失败并显示错误:错误:"TP::workerT&TP::workerT::operator=(const TP::workerT&)"被隐式删除,因为默认定义格式不正确:它指向 semaphore.h 文件。Semaphore.h 的定义如下:
public:
semaphore(int value = 0);
....
private:
int value;
....
semaphore(const semaphore& orig) = delete;
const semaphore& operator=(const semaphore& rhs) const = delete;
如果我删除"填充"行,程序会编译,但我真的需要它,因为我想初始化向量。当我制作虚拟结构并尝试push_back向量时,我收到相同的错误消息。
更新:谢谢@DyP!我仍然需要帮助编译。将"填充"行替换为:
std::generate(workers.begin(), workers.end(), free_func);
将这个精确地添加到我的标题中:
workerT free_func(){
return {};
}
收到以下错误:
thread-pool.cc:在构造函数'ThreadPool::ThreadPool(size_t)'中:thread-pool.cc:33:58:错误:类型为"ThreadPool::workerT (ThreadPool::)()"的参数与"ThreadPool::workerT (ThreadPool::*)()"不匹配在/usr/include/c++/4.6/algorithm:63:0 包含的文件中, 从 thread-pool.cc:15:/usr/include/c++/4.6/bits/stl_algo.h: 在函数 'void std::generate(_FIter, _FIter, _Generator) [with _FIter = __gnu_cxx::__normal_iterator>, _Generator = ThreadPool::workerT (ThreadPool::*)()]':thread-pool.cc:33:58:从这里实例化/usr/include/c++/4.6/bits/stl_algo.h:5013:2:错误:必须使用".' 或 '->' 以调用 '__gen (...)' 中的指向成员的指针函数,例如 '(... ->* __gen) (...)'make: * [thread-pool.o] 错误 1
更新 -- 在我的 .cc 文件中:
using namespace std;
static workerT free_func(){
return {};
}
ThreadPool(...args...){
std::generate(workers.begin(), workers.end(), free_func);
}
错误:
thread-pool.cc:19:10: error: ‘workerT’ does not name a type
thread-pool.cc: In constructor ‘ThreadPool::ThreadPool(size_t)’:
thread-pool.cc:39:49: error: ‘free_func’ was not declared in this scope
make: *** [thread-pool.o] Error 1
再次更新:
static ThreadPool::workerT free_func(){
return {};
}
ThreadPool(...args...){
std::generate(workers.begin(), workers.end(), free_func);
}
在 thread-pool.h 中:
struct workerT{
workerT() : status(true), threadSem(0){}
bool status;
std::function<void(void)> func;
semaphore threadSem;
};
正如0x499602d2正确指出的那样,fill
需要从第三个参数复制赋值。由于您的类型隐式不可复制,因此不能使用 fill
。
但是,您可以使用generate
来填充载体:
#include <vector>
#include <algorithm>
struct noncopyable
{
noncopyable() = default;
// make it noncopyable
noncopyable(noncopyable const&) = delete;
noncopyable& operator=(noncopyable const&) = delete;
// make it movable (thanks, gx_)
noncopyable(noncopyable&&) = default;
noncopyable& operator=(noncopyable&&) = default;
};
int main()
{
std::vector<noncopyable> vec(10);
std::generate(begin(vec), end(vec), []()->noncopyable{return {};});
}
注意:这仅在noncopyable
具有未删除的、可访问的移动构造函数时才有效。但是,如果它没有这样的 ctor,您将无法使用大部分向量(resize
需要 MoveInsertable
,这需要复制或移动 ctor)。
对于 g++4.8,要使用 generate
,您需要一个自由函数。我认为这是一个错误。
#include <vector>
#include <algorithm>
struct noncopyable
{
noncopyable() = default;
noncopyable(noncopyable const&) = delete;
};
noncopyable free_func()
{ return {}; }
int main()
{
std::vector<noncopyable> vec;
std::generate(begin(vec), end(vec), free_func);
}
另一个问题是你是否可以像这样初始化你的向量。我会说不。 fill
和 generate
不构造元素,而是覆盖(分配)。也就是说,您已经需要具有包含多个元素的向量才能使用它们。
N 个默认构造元素初始化向量的最简单版本是使用构造函数:
std::vector<noncopyable> vec(10);
创建包含 10 个默认构造元素的vector
。唯一的要求是noncopyable
是 DefaultConstructable(本质上,它必须有一个默认构造函数)。
如果类型不可复制且不可移动,则不能直接使用它(或作为数据成员)将其存储在vector
(*) 中。要使类C
可移动的,其中包含不可复制、不可移动的类型X
,您需要将X
存储为指针:
(*) 嗯,你可以,但你不能调整矢量的大小,你不能插入等。
struct nocopies_nomoves
{
nocopies_nomoves() = default;
nocopies_nomoves(nocopies_nomoves const&) = delete;
nocopies_nomoves& operator=(nocopies_nomoves const&) = delete;
// not required to be explicitly deleted:
nocopies_nomoves(nocopies_nomoves&&) = delete;
nocopies_nomoves& operator=(nocopies_nomoves&&) = delete;
};
#include <utility>
#include <memory>
class C
{
public:
C() : ptr( new nocopies_nomoves() ) {} // make_unique in C++1y
// I don't think you need to explicitly define those as defaulted;
// at least not if you don't declare ANY of the copy/move ctors, assignment ops
// and dtor
C(C&& rhs) = default;
C& operator=(C&& rhs) = default;
~C() = default;
// not required to be explicitly deleted:
C(C const&) = delete;
C& operator=(C const&) = delete;
private:
std::unique_ptr<nocopies_nomoves> ptr;
};
现在您可以创建一个vector<C>
并使用它(例如 resize
, insert
, ...
#include <vector>
#include <algorithm>
static C generate_C()
{
return {};
}
int main()
{
std::vector<C> vec(10);
// note: futile statement below; overwrites the 10 default-constructed
// elements
std::generate(begin(vec), end(vec), generate_C);
}
- 非静态成员函数的 decltype 格式不正确吗?
- HTTP 响应格式不正确?
- 实例化具有不完整类型的类模板格式不正确(如果该类型是在之后定义的)
- 注意:"Entity_c::Entity_c(const Entity_c&)"被隐式删除,因为默认定义格式不正确:
- C++ std::vector 中的外部类(定义格式不正确)
- 输出使用 cout 转换为 ASCII 的文本时输出格式不正确,C++
- 为什么参考简历的格式不正确?
- 被隐式删除,因为默认定义格式不正确:
- 为什么 (int&)0 格式不正确?
- 下面的代码是格式不正确的 NDR 还是格式正确的代码
- 尝试访问类的私有成员以消除重载是否格式不正确
- 不正确的printf格式指定器编译
- 使用派生自 'std::tuple' 的元素在 'std::tuple' 上调用 'std::get' - 格式不正确
- 尝试使用 boost::asio 连接到 SOCKS 5 服务器,但我的连接请求格式不正确
- “在成员函数之外封闭类的定义中需要默认成员初始值设定项” - 我的代码格式不正确
- 为什么以下模板声明格式不正确
- C++构造函数被隐式删除,因为默认定义的格式不正确
- 错误:隐式删除,因为默认定义格式不正确(结构向量)
- 提升日志时间戳格式不正确
- 在 C++11 标准中哪里说 char* p = "abc" ;格式不正确?