【问题标题】:c++ "no appropriate default constructor available" error using template class data member使用模板类数据成员的c ++“没有适当的默认构造函数可用”错误
【发布时间】:2013-04-15 18:11:18
【问题描述】:

我做了一个模板类Grid(我在头文件中说过T的默认值是float),我引用了源文件的一部分:

#include"Grid.h"

template <class T>
Grid<T>::Grid(unsigned int rows=1, unsigned int columns=1)
:Rows(rows),Columns(columns)
{
reset();
}

template<class T>
Grid<T>::~Grid(){}

template <class T>
void Grid<T>::reset()
{
vector<T> vec(Rows * Columns,T());
matrix = vec;
}

并且其他成员函数可以读取/更改矩阵的值或计算它。

Grid.h:

template<typename T=float> class Grid{

public:
        Grid(unsigned int, unsigned int);
        ~Grid();
        T getValue(unsigned int, unsigned int);
        void setValue(unsigned int, unsigned int, T);
        void reset();
        void write();

private:
        unsigned int Rows;
        unsigned int Columns;
        vector<T> matrix;
};

我在互联网上发现,为了使用模板类,我需要#include Grid.cpp 和 Grid.h,这样做我可以在 main() 中使用 Grid 类及其成员函数。我还在 Grid.cpp 周围放置了一个预处理器包装器。

现在,当我尝试构建一个新类 PDEProblem 时,没有继承但使用 Grid 类型的成员,我得到了错误:

    Error   2   error C2512: 'Grid<>' : no appropriate default constructor available      c:\users\...  15  

    Error   3   error C2512: 'Grid<T>' : no appropriate default constructor available   c:\users\...    15  
4   IntelliSense: no default constructor exists for class "Grid<float>" c:\Users\...    15

PDEProblem.h:

#include"grid.h"
#include"grid.cpp"

class PDEProblem: Grid<>
{
public:
PDEProblem(unsigned int,unsigned int);
~PDEProblem();
//some more other data members

private:
Grid<char> gridFlags;
Grid<> grid;
unsigned int Rows;
unsigned int Columns;
void conPot(unsigned int, unsigned int);
void conFlag(unsigned int, unsigned int);
};

PDEProblem.cpp:

#include"grid.h"
#include"grid.cpp"
#include "PDEProblem.h"

PDEProblem::PDEProblem(unsigned int rows=1,unsigned int columns=1)
    :Rows(rows), Columns(columns)
{
    conPot(rows, columns);
    conFlag(rows,columns);
}

PDEProblem::~PDEProblem(){}

void PDEProblem::conPot(unsigned int rows, unsigned int columns)
{
    grid=Grid<>(rows,columns);
}

void PDEProblem::conFlag(unsigned int rows, unsigned int columns)
 {gridFlags=Grid<char>(rows,columns);
    // some stuff with a few if and for loops which sets some elements of gridFlags to 1 and the others to 0
}

我该如何解决这个问题?在我看来,我对所有相关内容都有默认设置? 谢谢

【问题讨论】:

  • templates 如果您将它们的方法直接内联到头文件中,则更易于使用。先试试那个。您可以将实现分离到单独编译的源文件中,但随后需要显式实例化。
  • @user315052,只是复制粘贴头文件中的实现?如果可能的话,在大多数情况下使用两个单独的文件的原因是什么?
  • 你的代码完全没问题。 Live code 您在包含 .h.cpp 文件时遇到问题
  • 尝试在 PDEProblem 构造函数中显式初始化 Grid 网格。或者为 Grid 创建一个默认构造函数。
  • @Wouter:您通常不希望将源文件转换为包含文件。如果你把它们作为源文件保存,那就是你需要显式实例化的时候。

标签: c++ default-constructor template-classes


【解决方案1】:

使用我的编译器 (Visual Studio 2010) 和您的代码,我可以通过将默认参数值从函数定义移动到函数原型来消除您的错误。具体来说:

网格.h

template<typename T=float> class Grid{

public:
    Grid(unsigned int rows = 1, unsigned int columns = 1);
...
};

Grid.cpp

template <class T>
Grid<T>::Grid(unsigned int rows, unsigned int columns)
:Rows(rows),Columns(columns)
{
reset();
}

【讨论】:

    【解决方案2】:

    您的问题是您的主类继承自 Grid,同时包含其他两个 Grid 实例。除了糟糕的设计之外,您的两个 Grid 实例没有任何显式构造函数,这就是您遇到错误的原因。 设置默认值不是正确的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      • 2014-05-26
      • 2023-03-26
      相关资源
      最近更新 更多