模板参数 2 和 1 无效 - 声明向量

Template argument 2 and 1 is invalid - declaring vector

我有一个 txt 文件,其中包含许多 post- 中学毕业生的数据。记录格式如下:年省毕业性别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's是毕业变量

在我的 ReportGenerator class 中,我试图声明一个按年份组织的部分集合,一个按省组织的集合,一个按 graduation/degree 组织的集合。每个部分集合应该被定义为 属性 对象指针的集合,并且每个 属性 对象存储一个集合,该集合是主数据集合中记录的子集。

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

year是一个整数,province和graduation都是Record.ccclass中的字符串。我试图在下面的 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 个向量提供了错误。我真的很感激一些帮助或推动正确的方向来修复这些错误。

Record.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

Property.h

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

template <class T>

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

我还应该提到 属性 是一个 class 模板 ,所以不像其他 classes 没有 Property.cc 文件。只是 Property.h

我注意到“属性”是一个模板class,而“记录”不是。

并且需要在ReportGenerator.h

中完整声明

使用

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

而不是使用

static vector<Property*>

编译器报错,我认为这是针对 std::vector

元素类型的模板参数 1

分配器类型的模板参数 2

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