c++ - 模板参数 2 和 1 无效 - 声明 vector

标签 c++ templates vector compiler-errors stdvector

我有一个 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 vector ,但由于某种原因,它并没有给我这个 vector 的错误,但它对其他 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
报告生成器.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.cc 文件。这只是 Property.h

最佳答案

我注意到“Property”是一个模板类,而“Record”不是。
您需要在 ReportGenerator.h 中完全声明它

使用

static std::vector<Property< TYPENAME >*>
而不是使用
static vector<Property*>

并且错误编译器提示,我认为这是针对 std::vector
元素类型的模板参数 1
分配器类型的模板参数 2
可引用https://en.cppreference.com/w/cpp/container/vector

关于c++ - 模板参数 2 和 1 无效 - 声明 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65138464/

相关文章:

c++ - 回收未使用的 vector

c# - 2d-bin-packing 将矩形放置在 x,y 位置的算法?

c++ - std::array 作为类的模板参数

c++ - 依赖类型 : Template argument deduction failed

c++ - 如何在 C++ 中编写一个通用函数,将整数和字符串作为参数并修改它们?

c++ - 带有 vector 作为键的 STL 映射

matlab - 如何在matlab中将元胞数组转换为向量?

c++ - 我可以在不查看每个元素的情况下将 std::vector<Animal*> 转换为 std::vector<Dog*> 吗?

c++ - 共享库、对象构造函数和 fork() 行为

c++ - 2 智能感知 : argument of type "const char *" is incompatible with parameter of type "LPCWSTR"