【问题标题】:Error: some class is not a template错误:某些类不是模板
【发布时间】:2015-05-21 08:04:22
【问题描述】:

我有一个头文件Algo.h。它有以下内容:

#include <iostream>
#include <fstream>
#include <math.h>
#include <float.h>
#include <string.h>
#include <stdlib.h>

using namespace std;

//some static functions
// ...

template <class Type> class Algo{
    int

    public:
        Algo(int size, int num, int plth, int theN, float** theAg, int theLN,
            float* theIn, float theeEps = 1E-3, float theEpsilonLR = 1E-3,
            int theCycle = 30, bool DebInf = false, int theT = -1, int** theX = 0,
            const char* theFileName = 0, const char* theFileNameChar = 0);
        ~Algo();

        //some methods
        //...
};

//Constructor
template <class Type> Algo<Type>::Algo(int size, int num, int plth, int theN, float** theAg, int theLN,
                                        float* theIn, float theeEps = 1E-3, float theEpsilonLR = 1E-3,
                                        int theCycle = 30, bool DebInf = false, int theT = -1, int** theX = 0,
                                        const char* theFileName = 0, const char* theFileNameChar = 0){
    //...
}
// ...

那我想用在main.cpp

#include "Algo.h"
#include <float.h>
#include <time.h>
#include <stdlib.h>
#include <string>

#include <iostream>

using namespace std;

Algo<int>* construct1(const int & rt, float** & rm); //error: Algo is not a template
Algo<int>* construct2(const int & rte, float** & rm, Algo<int>* & the1, const bool & rob1); //error: Algo is not a template

//...

int main(){
    //...
    return 0;
}

似乎一切正常,但我总是收到此错误:

算法不是模板。

你有什么解决办法吗?

【问题讨论】:

  • 一开始,您使用的参数太多。这不是一个好的做法,因为程序不容易阅读。在此之后您应该使用Algo&lt;int&gt;* construct1 = new Algo&lt;int&gt;(...)Algo&lt;int&gt; construct1(...)
  • 真的有必要在变量名前加上the..吗?
  • 未使用模板参数。可能它被优化掉了,因为没有必要,然后Algo 真的不是模板。

标签: c++ templates compiler-errors


【解决方案1】:
  1. 你的代码中不应该有“int”。请删除它。

    template <class Type> class Algo{
      int // here should be deleted
    
      public:
      ...
    
  2. Algo 的构造函数有很多默认参数,但是当你定义这个函数时,这些默认参数不应该在 param-list 中设置值。您可以将构造函数定义如下:

    template <class Type> Algo<Type>::Algo(int size, int num, int plth, int theN, float** theAg, int theLN, float* theIn, float theeEps, float theEpsilonLR, int theCycle, bool DebInf, int theT, int** theX, const char* theFileName, const char* theFileNameChar)
    {
    //...
    }
    
  3. 做这两个修复,它会工作的。(我在我的电脑上试过了~)

【讨论】:

    【解决方案2】:

    我认为这不是唯一的问题,但请注意您尝试使用它的方式。

    构造函数:

     Algo(int size, int num, int plth, int theN, float** theAg, int theLN,
                float* theIn, float theeEps = 1E-3, float theEpsilonLR = 1E-3,
                int theCycle = 30, bool DebInf = false, int theT = -1, int** theX = 0,
                const char* theFileName = 0, const char* theFileNameChar = 0);
    

    有很多参数。前 7 个是必需的,其余的具有默认值并且是可选的。但是,当您尝试实例化实例时:

    Algo<int>* construct1(const int & rt, float** & rm); //error: Algo is not a template
    Algo<int>* construct2(const int & rte, float** & rm, Algo<int>* & the1, const bool & rob1); //error: Algo is not a template
    

    您要传递 2 个或 4 个参数。没有匹配的重载。 您至少需要提供前 7 个参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 2012-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-26
      相关资源
      最近更新 更多