2016-10-03 3 views
-2

私のクラスでは、 "string"、変数、またはテンプレートクラスが宣言されていない複数のエラーがあります。私のヘッダーファイルに何が間違っているのか、どうやって設定しているのかは分かりません。異なるヘッダーのテンプレートクラスのクラスはコンパイルされません

私のプログラムは2つのクラスを使用します.1つはプライベートメンバーでダイナミックアレイを使用するテンプレートクラスで、もう1つはプライベートメンバーでそのテンプレートクラスを使用します。 .hファイルには.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); 
} 

#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が無効または教室テンプレート取得しています。

また、I UniqueVector classRoomに関するエラー。文字列が宣言されていません

私はstd :: UniqueVectorクラスルームを使用しようとしました。私は文字列のすべてのインスタンスにstd ::を含める必要はありませんので、方法はありますか?

+4

'UniqueVector classRoom;' - > 'UniqueVector classRoom;'。 –

+0

C++はシングルパスコンパイラであるため、事前に事柄を知ることはできません。インクルードと定義を順序付けして、最初の使用に先立って宣言または定義されるようにする必要があります。また、 'using namespace std'を使わないでください。 – kfsone

+0

@kfsone少なくとも 'using namespace std;'はヘッダーではなくソースファイルにあるので、他のファイルに悪影響を及ぼすことはありません。 –

答えて

1

.hファイル。 #include <string> ...ちょうどそれは、生産中であっても、コンパイル時の問題を心配する必要はありません。これは標準ヘッダーなので、プログラムコードと同じくらい変更されることはありません。

それ以外の場合は、std::stringと宣言してみることはお勧めできません。

関連する問題