c++ - 具有来自不同 header 的模板类的类将无法编译

标签 c++ class templates makefile compiler-errors

我的类有多个错误,“字符串”、变量或未声明模板类。不确定我的头文件有什么问题或我如何设置它。

现在我的程序使用了 2 个类,一个是在私有(private)成员中使用动态数组的模板类,另一个类在私有(private)成员中使用该模板类。

在.cpp

#include <iostream>
#include <ostream>
#include <string> 

#include "Classroom.h"
using namespace std;

bool Classroom::addStudent(const string& name){ 
      return (classRoom.insert(name));
}
bool Classroom::removeStudent(const string& name)
{ 
     return (classRoom.remove(name));
}
bool Classroom::containsStudent(const string& name)
{ 
     return (classRoom.contains(name));
}
string Classroom::listAllStudents()
{ 
    string name;
    string data;
    unsigned int i;
    for (i=0;i<classRoom.size()-1; i++){  
        if (classRoom.at(i,data)){ 
                name += data;
                name += ", ";
           }      
    }
    if (i<classRoom.size()){
       if (classRoom.at(i,data)){ 
                name += data;
           }    
    }
    return (name);
}

在.h
#ifndef CLASSROOM_H
#define CLASSROOM_H

#include "UniqueVector.h"

class Classroom {
public:
    /*— If a student named name is not already on the
    classroom roster, adds a new student named name to the classroom roster and returns true;
    otherwise, returns false.*/
    bool addStudent(const std::string& name); 
    /* — If a student named name is on the classroom
    roster, removes the student named name from the classroom roster and returns true; otherwise,
    returns false.*/
    bool removeStudent(const std::string& name);
    /* — If a student named name is on the classroom
    roster, returns true; otherwise, returns false. */
    bool containsStudent(const std::string& name);
    /*— Returns a string containing the names of the students in the
    classroom, separated by commas.*/
    std ::string listAllStudents();     
private:
    UniqueVector<string> classRoom;
};

#endif

我收到模板化参数 1 无效或来自 Classroom 私有(private)成员的 classRoom 未声明。

还有一个关于 I UniqueVector classRoom 的错误;未声明字符串

我尝试使用 std::UniqueVector classRoom;有没有办法让我不必在每个字符串实例中都包含 std::?

最佳答案

在您的 .h 文件中。你应该只是 #include <string> ...即使在生产中,也无需担心编译时问题。它是一个标准头文件,因此不会像您的程序代码那样改变。

否则,尝试转发声明 std::string不是一个好主意。

关于c++ - 具有来自不同 header 的模板类的类将无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39837260/

相关文章:

c++ - 静态指针 C++ 用法

javascript - 如何通过 jQuery 循环遍历某种类型的所有元素?

c++ - 单例模式的类成员初始化失败

c++ - 如何将 C++ 类的静态成员函数传递给模板并调用该函数

c++ - 如何实际使用表达式模板

c++ - 如何使用 eigen 库针对 matlab 加速这个 C++ 程序?

C++基数排序与按位运算

c++ - qt 音频输出示例未构建——缺少生成器类声明

python - 根据 PEP8 : uppercase or lowercase? 的常量类属性样式

c++ - 为不适用于输出流的模板矩阵重载 "+"