-2

I have multiple errors with my class, either "string", a variable, or the template class isnt declared. Not sure whats wrong with my header files or how I'm setting it up.

Now my program uses 2 classes, one is a templated class that uses dynamic arrays in the private member, and the other class uses that templated class in the private member.

In the .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);
}

In the .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

I'm getting templated argument 1 invalid or classRoom from the private member of Classroom isn't declared.

Also an error about I UniqueVector classRoom; with string not declared

I tried using std:: UniqueVector classRoom; is there way so that I dont have to include std:: in every instance of string?

  • 4
    `UniqueVector classRoom;` -> `UniqueVector classRoom;`. – R Sahu Oct 03 '16 at 17:36
  • C++ is a single-pass compiler, so it can't know things in advance. You will have to order your includes and definitions so that things are declared or defined ahead of their first usage. Also, don't do `using namespace std`. – kfsone Oct 03 '16 at 17:39
  • @kfsone At least the `using namespace std;` is in the source file, not the header, so it won't inadvertently affect any other files. – Justin Time - Reinstate Monica Oct 03 '16 at 18:38
  • When you have multiple errors in a large mass of code, you are writing code too fast and not testing it often enough. – Beta Oct 03 '16 at 22:58

1 Answers1

1

in your .h file. You should just #include <string>... there's no need to worry about compile-time problem doing that, even in production. Its a standard header, and thus will not change as much as your program code will.

Otherwise, attempting to forward declare std::string isn't a good idea.

WhiZTiM
  • 21,207
  • 4
  • 43
  • 68