来自不同标题的模板类的类不会编译

时间:2016-10-03 17:33:52

标签: c++ class templates makefile compiler-errors

我的班级有多处错误,"字符串",变量或模板类未声明。不确定我的标题文件或我如何设置它的错误。

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

在.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的私有成员中的classRoom未被声明。

关于I UniqueVector classRoom的错误;字符串未声明

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

1 个答案:

答案 0 :(得分:1)

在你的.h文件中

。你应该#include <string> ......即使在生产中也没有必要担心编译时问题。它是一个标准的标题,因此不会像程序代码那样改变。

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