我的类有多个错误,“字符串”、变量或未声明模板类。不确定我的头文件有什么问题或我如何设置它。
现在我的程序使用了 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/