I'm currently trying to extend my student class with a course class & by extending the driver.cpp to enrol 3 students into the course.
However, i've gotten some errors & can't seem to figure whats wrong with them, i might have missed out something. I was hoping to get some help on these issues & to point out what mistakes I might be made.

Thanks.

here are my codes

Student.cpp

#include <iostream>
#include <string>
#include "Student.h"

using namespace std;

// constructor for your class
Student::Student() 
{
}
Student::Student(int newID, string newName)
{
    ID = newID;
    Name = newName;
}
// destructor for your class
Student::~Student() 
{

}
//Accessor Functions
int Student::getStudentID()
{
    return ID;
}
string Student::getStudentName()
{
    return Name;
}   
//Sets the student name attribute data/Mutator
void Student::setStudentName(string newName) 
{
    Name = newName;
}
//Sets the student ID attribute data/Mutator
void Student::setStudentID(int newID)
{
    ID = newID;
}
//displays the output
void Student::display()     
{
    cout << "Name: "<< Name << endl; 
    cout << "ID: " << ID << endl; 
}

Student.h

#include <iostream>
#include <string>
using namespace std;


#ifndef STUDENT_H
#define STUDENT_H



class Student
{
    public:
    // constructor
    Student();
    Student(int, string);

    // destructor
    ~Student();

    //Accessors
    string getStudentName();
    int getStudentID();

    //declaration of private methods
    void setStudentName(string newName);
    void setStudentID(int newID);
    void display();

    private:

    int ID;     //Student ID
    string Name;    //Student Name

}; 
#endif  

Course.cpp

#include <iostream>
#include <vector>
#include <string>
#include "Course.h"

using namespace std;

Course::Course(string a) 
{
    CourseName=a;
}


string Course::getCourseName() 
{
    return CourseName;
}


bool Course::isEnrolled(string StudentID) 
{
    for (int i=0; i<students.size(); i++) 
    {
        if (students[i]==StudentID) 
        {
            return true;
            break;
        } 
        else 
        {
            return false;
            break;
        }    
    }

}

void Course::enrolStudents(Student) 
{
    int i;
    students.push_back(); 
}

void Course::display()
{
    cout << "Course Name: "<< CourseName << endl;
}

Course.h

#include <iostream>
#include <string>
#include <vector>

#ifndef COURSE_H
#define COURSE_H


class Course
{
    public:
    // constructor
    Course();
    Course(std::string a);

    // destructor
    ~Course();

    //Accessors
    std::string getCourseName();

    //declaration of private methods
    bool isEnrolled(std::string StudentID);
    void enrolStudents(Student);
    void display();

    private:

    std::string CourseName;     //Course Name
    vector<Student> students;    //vector to store students

}; 
#endif  // end of class definition

driver.cpp

#include "Student.h"
#include "Course.h"

int main()
{
    Student student;
    Course course;
    string temp = "student1";
    student.setStudentID(23456);
    student.setStudentName("Tom");
    temp = "student2";
    student.setStudentID(24680);
    student.setStudentName("John");
    temp = "student3";
    student.setStudentID(13579);
    student.setStudentName("Jason");

    student.display();
    course.display();

    return 0;
}

these are the errors, i am getting:
(course.h)error C2061: syntax error : identifier 'Student'
(course.h)error C2143: syntax error : missing ';' before '<'
(course.h)error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
(course.h)error C2238: unexpected token(s) preceding ';'
(course.cpp)error C2065: 'students' : undeclared identifier
(course.cpp)error C2228: left of '.size' must have class/struct/union type is ''unknown-type''
(course.cpp)error C2065: 'students' : undeclared identifier
(course.cpp)error C2065: 'Student' : undeclared identifier
(course.cpp)error C2448: 'Course::enrolStudents' : function-style initializer appears to be a function definition

Recommended Answers

All 7 Replies

Since you want to use Student objects in Course, you need to tell Course about them by including the header file for the Student class in the Course class. It's similare to what you did for the vector class.

I've editted my course.h file to look like this

#include <iostream>
#include <string>
#include <vector>
#include "Student.h"

#ifndef COURSE_H
#define COURSE_H


class Course
{
    public:
    // constructor
    Course();
    Course(std::string a);

    // destructor
    ~Course();

    //Accessors
    std::string getCourseName();

    //declaration of private methods
    bool isEnrolled(std::string StudentID);
    void enrolStudents(Student);
    void display();

    private:

    std::string CourseName;     //Course Name
    std::vector<Student> students;   //vector to store students

}; 
#endif  // end of class definition

now i have some new errors that i'm unable to identify, hope some1 could help me with them.

the errors:
warning C4018: '<' : signed/unsigned mismatch
error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'Student'
see declaration of 'std::operator =='
error C2784: 'bool std::operator ==(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'Student'
see declaration of 'std::operator =='
error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'Student'
see declaration of 'std::operator =='
error C2784: 'bool std::operator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'Student'
see declaration of 'std::operator =='
error C2784: 'bool std::operator ==(const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &,const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &)' : could not deduce template argument for 'const std::istream_iterator<_Ty,_Elem,_Traits,_Diff> &' from 'Student'
see declaration of 'std::operator =='
error C2784: 'bool std::operator ==(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)' : could not deduce template argument for 'const std::istreambuf_iterator<_Elem,_Traits> &' from 'Student'
see declaration of 'std::operator =='

and many other similar errors

so i've managed to run my program.

however, I am trying to enrol 3 students into the course class, but the program only recognises the 1st student & not the other 2.

Hope someone couldhelp me with this problem.

here are my updated codes

Course.cpp

#include <iostream>
#include <vector>
#include <string>
#include "Course.h"

using namespace std;

//Constructor
Course::Course()
{

}

Course::Course(string a)
{
    CourseName=a;
}

//destructor
Course::~Course()
{

}
//checks student enrolled with ID
bool Course::isEnrolled(string StudentID)
{
    for (int i=0; i<students.size(); i++)
        {
                if (students[i].getStudentID() == StudentID)
                {
                        return true;

                }
                else
                {
                        return false;

                }
        }

}
//stores student in vector
void Course::enrolStudents(Student student)
{
    students.push_back(student);
}

Course.h

#include <iostream>
#include <string>
#include <vector>
#include "Student.h"

#ifndef COURSE_H
#define COURSE_H


class Course
{
    public:
    // constructor
    Course();
    Course(std::string a);

    // destructor
    ~Course();

    //declaration of private methods
    bool isEnrolled(std::string StudentID);
    void enrolStudents(Student student);

    private:

    std::string CourseName;     //Course Name
    std::vector<Student> students;   //vector to store students

}; 
#endif  // end of class definition

Student.cpp

#include <iostream>
#include <string>
#include "Student.h"

using namespace std;

// constructor for your class
Student::Student()
{

}
Student::Student(string newID, string newName)
{
        ID = newID;
        Name = newName;
}
// destructor for your class
Student::~Student()
{

}
//Accessor Functions
string Student::getStudentID()
{
        return ID;
}
string Student::getStudentName()
{
        return Name;
}
//Sets the student name attribute data/Mutator
void Student::setStudentName(string newName)
{
        Name = newName;
}
//Sets the student ID attribute data/Mutator
void Student::setStudentID(string newID)
{
        ID = newID;
}
//displays the output
void Student::display()
{
        cout << "Name: "<< Name << endl;
        cout << "ID: " << ID << endl;
}

Student.h

#include <iostream>
#include <string>
using namespace std;


#ifndef STUDENT_H
#define STUDENT_H



class Student
{
    public:
    // constructor
    Student();
    Student(string, string);

    // destructor
    ~Student();

    //Accessors
    string getStudentName();
    string getStudentID();

    //declaration of private methods
    void setStudentName(string newName);
    void setStudentID(string newID);
    void display();

    private:

    string ID;  //Student ID
    string Name;    //Student Name

}; 
#endif

driver.cpp

#include "Student.h"
#include "Course.h"

int main()
{
        Student student;

        student.setStudentID("123");
        student.setStudentName("abc");
        student.display();

        Course course;
        Student a("Tom" , "1");
        course.enrolStudents(a);
        cout<<course.isEnrolled("Tom");
        Student b("Jas" , "2");
        course.enrolStudents(b);
        cout<<course.isEnrolled("Jas");
        course.enrolStudents(Student("Kio" , "3"));
        cout<<course.isEnrolled("Kio");

        cout << "check" << endl;
        string check;
        cin >> check;
        if(course.isEnrolled(check))
        cout << "Students Enrolled" << endl;
        Else
        cout << "Students not Enrolled" << endl;



        return 0;
}

Try to put this includes:

include <iostream>
include <string>
include <vector>
include "Student.h"

after this ones in your .h files:

ifndef COURSE_H
define COURSE_H

oh, thanks everyone for your help, I've managed to solve my problems and got my program working

I'm trying to write another driver.cpp to enrol students into the course by reading off a txt file, I cant seem to get it to work right.
the students cant seem to be enrolled into the course.

here is my driver.cpp & .txt file

#include<iostream>
#include<fstream>
#include<string>
#include "Course.h"
#include "Student.h"

using namespace std;

int main()
{
    Course course;
    string Name,temp; 
    string ID; 
    ifstream myfilestream;
    myfilestream.open("input.txt");
    if (myfilestream.fail())
    {
        cout<<"Invalid Input Textfile!" << endl;
        return -1;
    }

    do
    {
        myfilestream>>temp;
        myfilestream>>Name;
        Name+=temp;
        myfilestream>>ID;
        course.enrolStudents(Student(Name,ID));

    }
    while(myfilestream.good());
    myfilestream.close();
    cout<<"Enter to check if student is enrolled\n";
    string check;
    cin>>check;
    if(course.isEnrolled(check))
        cout<<"Student " << check << "is currently enrolled" << endl;
    else
        cout<<"Student not enrolled" << endl;

    return 0;
}

input.txt

Tom 123
Jas 456
Kio 789

someone please help me on this problem

How do you know? Did you print out the list of students without trying to search for one?

Remember the non default Student() constructor takes ID before Name but you are sending Name before ID and since no name will ever match an ID it will look like you didn't add the Student to course, but you may well have, just not in the form you thought because of the glitch in sending data to the constructor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.