小编Aga*_*nju的帖子

编译器报告'已删除'operator =,但它就在那里

我遇到了一个讨厌的问题,编译器声称它operator=被删除了,但它就在那里.经过几个小时的尝试,我制作了一个最小的解决方案来重现这个问题.我正在使用MSVC Community Edition 2017 15.7.5(截至今天的最新版本,2018-07-20),并将其设置为'C++ 17'

代码不是微不足道的; 概念是模板类TT用于强制foo在一组类中存在静态成员函数Fn.这与工厂模式非常相似,只是此解决方案不会创建类实例,而是报告有关类的静态详细信息.

在最后一行的赋值中报告错误,并读取(底部的完整错误列表):

"错误C2280:'C&C :: operator =(const C&)':尝试引用已删除的函数"

但第5行定义了这个运算符,右边是那些装饰器?

失败的赋值尝试将返回的值分配给const std::vector<C>&类成员变量.
我认为这const是产生问题,但删除const每个函数中的所有函数没有任何区别; 同一行中的错误相同.

问题:为什么编译器报告这个,以及可能的修复方法是什么?
我认为这一定是我想念的傻事,但我找不到它.

#include <vector>

class C
{
public:
  C(int ii) : i(ii) {}
  C& operator=(const C&) = default;    /// HERE is the assignment operator
  const int i;
};
typedef std::vector<C> CVec;   // shorthand for a vector of C's

template <class T>   // this template forces classes F1, …
Run Code Online (Sandbox Code Playgroud)

c++ templates factory-method assignment-operator c++17

1
推荐指数
1
解决办法
79
查看次数

如何通过构造函数确定私有std :: array成员的大小?

我有一个class B应该包含一个private std::array<A*>,并且大小将通过构造函数发送

class B
{
  public:
    B(size_t s) : vv(s,nullptr), aa(s) {}
    // ... more stuff
  private:
    std::vector<A*>   vv;   // vector works fine
    std::array<A*,??> aa;   // but how to initialize an array??
}
Run Code Online (Sandbox Code Playgroud)

使用std::vector可以很好地工作,但是似乎无法使用它std::array

添加另一个成员const size_t ss并在std::array声明中使用它也不起作用,即使它是static const size_t ss-编译器(VS2019 16.1.5,设置为C ++ 17)声称“ ...期望的编译时常量...”

class B  // another try with a static const size_t
{
  public:
    B(size_t s) : ss(s), aa(s) {}
    // …
Run Code Online (Sandbox Code Playgroud)

c++ arrays constructor

-1
推荐指数
1
解决办法
51
查看次数