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



我有一个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是一个省,学士是毕业变量

在我的ReportGenerator课程中,我试图申报一个按年份组织的部分集合,一个按省份组织的集合,以及一个按毕业/学位组织的集合。每个部分集合应该定义为Property对象指针的集合,每个Property对象存储一个集合,该集合是主数据集合中记录的子集。

这些部分集合中的每个元素都将保存Property的一个特定值的记录。例如:其中一个属性是year,因此ReportGenerator基类将包含按年份组织的部分集合,我在下面称之为"years"。该数据仅包含四个不同年份(2000年、2005年、2010年和2015年(的统计数据,因此部分收集的年份将恰好包含四个元素,每年一个;年份集合的第一个元素将包含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

我注意到"属性";是一个模板类;记录";不是。

您需要在ReportGenerator.h 中完全声明它


使用

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

而不是使用

static vector<Property*>

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

元素类型的模板参数1

分配器类型的模板参数2

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

最新更新