【问题标题】:Array definition in template classes模板类中的数组定义
【发布时间】:2017-04-26 12:05:50
【问题描述】:

我已经生成了带有数据数组(一些 const 静态,一些可变)和访问器方法的配置文件。问题是现在其中一种数据类型将被模板化,但我根本无法让编译器接受它。

模板化类型是非 pod,但默认可构造。

bo 使用的定义在 cpp 文件中,但由于在生成代码时我不知道模板类型,所以我不能再这样做了。

即我想要类似以下的内容(如果可以,但在标题之外定义更好)

template<typename T>
class LargeConfig :
{
public:
    // methods
private:
    static const POD1 POD_ONES[];
    T ManyTs[];
};

template<typename T>
static const POD1 LargeConfig<T>::POD_ONES[] =
{
   { 0U, 1U}, // instance 1
   { 1U, 1U}, // instance 2
   ...
};

template<typename T>
T LargeConfig<T>::ManyTs[] =
{
   T(), // instance 1
   T(), // instance 2
   ...
};

目前,对于 POD_ONES 定义,我得到“此处可能未指定存储类”,并且“可能未在其类之外定义非静态数据成员" 用于 ManyTs

但肯定有某种方法可以在 C++ 中的类中创建模板化的非平凡数组吗?到目前为止,我只找到了模板类型为整数类型的示例。

【问题讨论】:

  • POD1 在范围内吗?只要定义了错误,我就不会得到错误。
  • @NathanOliver:嗯。真的谢谢。在更改代码时,我将静态 POD 结构的一个成员设为 const。显然,这导致我的嵌入式编译器将其视为非 POD 并给出了上面的错误消息。没有 const 成员,POD_ONES 部分可以工作。

标签: c++ arrays templates


【解决方案1】:

首先,ManyTs 未声明为 static,因此出现了关于 nonstatic data member defined outside of class 的错误。

那么,当你定义你的static 成员时,不要使用关键字static

template<typename T>
class LargeConfig
{
public:
    // methods
private:
    static const POD1 POD_ONES[];
    static T ManyTs[];
};

template<typename T>
const POD1 LargeConfig<T>::POD_ONES[] =
{
   { 0U, 1U}, // instance 1
   { 1U, 1U}, // instance 2
};

template<typename T>
T LargeConfig<T>::ManyTs[] =
{
   T(), // instance 1
   T() // instance 2
};

我编译了一个sample demo(将您的static 数据成员设为公开以便快速访问它们)

【讨论】:

    【解决方案2】:

    如你所言,你有两个问题,让我从第二个开始。

    ManyTs 被定义为 LargeConfig 的常规成员,这意味着它应该在您的类的构造函数中初始化。一个例子:

    template<typename T>
    LargeConfig<T>::LargeConfig()
    : ManyTs
        { T(), // instance 1
          T(), // instance 2
          ...
        }
    {}
    

    第一个问题更难猜,因为我设法使用以下 POD1 定义编译它

    struct POD1
    {
        POD1(unsigned, unsigned);
        unsigned _1{};
        unsigned _2{};
    };
    

    我怀疑您要么不包含该课程,要么该课程出现其他问题,但是,由于我们看不到,很难说。

    【讨论】:

      【解决方案3】:

      尝试在此处编译您的代码 (g++ 5.4) 我在第 7 行遇到语法错误。更正我们得到:

      templates.cpp:16:44: error: ‘static’ may not be used when defining (as opposed to declaring) a static data member [-fpermissive]
       static const POD1 LargeConfig<T>::POD_ONES[] =
                                                  ^
      templates.cpp:23:26: error: ‘T LargeConfig<T>::ManyTs []’ is not a static data member of ‘class LargeConfig<T>’
       T LargeConfig<T>::ManyTs[] =
                                ^
      templates.cpp:23:26: error: template definition of non-template ‘T LargeConfig<T>::ManyTs []’
      

      第一条消息与this problem 相关。您不能使用静态来定义数据类型,只能用于声明。

      最后一个错误已通过简单地将ManyTs 成员设为静态而得到纠正。

      然后代码变成这样:

      class POD1 {
      
      };
      
      template<typename T>
      class LargeConfig
      {
      public:
          // methods
      private:
          static const POD1 POD_ONES[];
          static T ManyTs[];
          T ManyTsAgain[10];
      };
      
      template<typename T>
      const POD1 LargeConfig<T>::POD_ONES[] =
      {
         { 0U, 1U}, // instance 1
         { 1U, 1U}, // instance 2
      };
      
      template<typename T>
      T LargeConfig<T>::ManyTs[] =
      {
         T(), // instance 1
         T(), // instance 2
      };
      
      int main() {
          LargeConfig<int> obj1;
          LargeConfig<POD1> obj2;
      
          return 0;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-08-14
        • 1970-01-01
        • 1970-01-01
        • 2016-10-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多