【问题标题】:Template argument 2 and 1 is invalid - declaring vector模板参数 2 和 1 无效 - 声明向量
【发布时间】:2021-03-16 04:56:14
【问题描述】:

我有一个 txt 文件,其中包含许多大专毕业生的数据。记录格式如下:年份省份毕业性别numEmployed numGrads。这是文件的一部分:

2000 AB Bachelor's All 6900 7500
2005 AB Bachelor's All 9300 10000
2015 PE Bachelor's All 440 500
2000 PE College All 728 800
2005 AB Bachelor's Females 5642 6200
2010 AB Bachelor's Females 5369 5900
2015 BC Doctorate Females 188 200
2010 BC Doctorate Males 285 300
2005 CAN Bachelor's Males 33396 36300
2000 CAN College Males 28569 32100

所以第一行2000是一年,AB是省,Bachelor是毕业变量

在我的 ReportGenerator 类中,我试图声明一个按年份组织的部分集合,一个按省组织的集合,一个按毕业/学位组织的集合。每个部分集合都应该定义为 Property 对象指针的集合,并且每个 Property 对象都存储一个集合,该集合是主数据集合中记录的子集。

这些部分集合中的每个元素都将保存属性的一个特定值的记录。例如:其中一个属性是年份,因此 ReportGenerator 基类将包含按年份组织的部分集合,我在下面将其称为“年份”。数据仅包含四个不同年份(2000、2005、2010 和 2015)的统计数据,因此年份部分集合将恰好包含四个元素,每个元素对应一个年份; year 集合的第一个元素将包含 2000 年的所有记录;第二个元素将包含 2005 年的元素,以此类推 2010 年和 2015 年。

年份是整数,省和毕业都是字符串,如 Record.cc 类所示。我试图在 ReportGenerator.h 文件中声明这 3 个部分集合,但我不断收到错误消息:

ReportGenerator.h:25:27: error: template argument 1 is invalid
    static vector<Property*> years; 
                           ^
ReportGenerator.h:25:27: error: template argument 2 is invalid
ReportGenerator.h:26:27: error: template argument 1 is invalid
    static vector<Property*> provinces; 
                           ^
ReportGenerator.h:26:27: error: template argument 2 is invalid
ReportGenerator.h:27:27: error: template argument 1 is invalid
    static vector<Property*> graduation;
                           ^
ReportGenerator.h:27:27: error: template argument 2 is invalid

我基本上对记录集合做了完全相同的事情,它基本上只是一个记录指针的 STL 向量,但由于某种原因,它并没有给我这个向量的错误,但它对其他 3 个。我会非常感谢您在修复这些错误方面的帮助或朝着正确的方向前进。

记录.h

#ifndef RECORD_H
#define RECORD_H

#include <iostream>
#include <string>

class Record{
  public:
    Record(int = 0, string = "Unknown", string = "Unknown");
    ~Record();
    
    private:
        int year;
        string province;
        string graduation;
};
#endif

ReportGenerator.h

#ifndef REPORTGENERATOR_H
#define REPORTGENERATOR_H

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

#include "Record.h"
#include "Property.h"

class ReportGenerator{
  public:
    ReportGenerator();
    static void populate();
  
  protected:
    static vector<Record*> records;
    
    static vector<Property*> years; 
    static vector<Property*> provinces; 
    static vector<Property*> graduation;        
  
};
#endif

属性.h

#include <iostream>
using namespace std;
#include <cstdlib>

template <class T>

class Property{
    public:
         Property& operator+=(const int);
         Property& operator[](int);
    private:
};

我还要提一下,Property 是一个类模板,因此与其他类不同,它没有 Property.cc 文件。这只是 Property.h

【问题讨论】:

  • 另外请花点时间阅读Why is “using namespace std;” considered bad practice? 在头文件中这样做是双重不好的做法。
  • 额外的#endif 是这篇文章的错字,不在原始文件中,对此我感到抱歉
  • Property 不是一个类,它是一个模板。您需要 Property 的模板参数才能成为一个类,如 Property&lt;int&gt;。你不能有一个模板容器,只有类实例。
  • 我的指令说将其定义为属性对象指针的集合。那么它会不会像“静态向量 Property* 年;”或者其他的东西?我还是有点困惑。
  • 您确定Property 应该是模板吗?也许你应该花点时间和你的老师或教授谈谈?

标签: c++ templates vector compiler-errors stdvector


【解决方案1】:

我注意到“Property”是一个模板类,而“Record”不是。

而且你需要在 ReportGenerator.h 中完全声明它


使用

static std::vector<Property< TYPENAME >*>

而不是使用

static vector<Property*>

并且错误编译器抱怨,我认为这是针对 std::vector

元素类型的模板参数 1

分配器类型的模板参数 2

可以参考https://en.cppreference.com/w/cpp/container/vector

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多