Not able to push_back a class object to a vector

Hello all, I am trying to create a vector of object Course and then fill the vector with a temp value. A loop will cycle through entries that come from a text file to create the temp values. My issue is with the snippet below, but I have included the entire program so far. Yes, there are other things commented out, but I am trying to make sure that separate parts are working before combined. The following loop yields the forthcoming errors.

while (getline(inStream, lines)){
cout << lines << endl;
Course *temp = new Course(lines);
Course.push_back(temp);
}

main.cpp: In function 'int main()':
main.cpp:21:11: error: 'temp' was not declared in this scope
Course *temp = new Course(lines);
^~~~
main.cpp:21:22: error: expected type-specifier before 'Course'
Course *temp = new Course(lines);
^~~~~~





main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <vector>  
#include <fstream>
#include <iostream>
#include "course.hpp"
  using namespace std;


int main() {
        int menuChoice;
        string fileName;
        string lines;
        int maxEntries= 0;
        ifstream inStream;
        vector<Course> Course;
        cout << "Please enter the file that contains othe course data: ";
        cin >> fileName;
        inStream.open(fileName);
        cout << "TEST:  Filename is: " << fileName << endl;
        while (getline(inStream, lines)){
                cout << lines << endl;
                Course *temp = new Course(lines);
                Course.push_back(temp);
        }
        cout << "Welcome to Banner NE, short for Never Existed!" << endl;
        do {
        cout << "1 - List all available classes" << endl;
        cout << "2 - Search for a course" << endl;
        cout << "3 - View your current enrolled courses" << endl;
        cout << "4 - Enroll in course" << endl;
        cout << "5 - Exit" << endl;
        cin >> menuChoice;
        cout << "TEST:  menuChoice: " << menuChoice << endl;
        if (menuChoice == 5){
                cout << "Thank you for using Banner NE and have a nice day!" << endl;
                return 0;
        }
        if (menuChoice == 2) {
                cout << endl;
                cout << "Would you like to search by" << endl;
                cout << "1 - Course prefix" << endl;
                cout << "2 - Available seats remaining" << endl;
                cout << "Make your selection: ";
                cin >> menuChoice;
        }
/*      if (menuChoice == 1) {
                for (int x = 0; x < Course.size(); x++)
                        Course.at(x).printCourse();
        }
*/
         } while (menuChoice != 5);
        return 0;
}


course.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "course.hpp"

using namespace std;

Course::Course(){
        coursePrefix = "";
        courseNumber = 0;
        seatsAvailable = 0;
        maxSeats = 0;
}

Course::Course(string courseDetails){
        stringstream inString(courseDetails);
        string instructorName;
        string classDays;
        string classTimes;
        inString >> coursePrefix >> courseNumber >> instructorName >> maxSeats >> seatsAvailable >> classDays >> classTimes;
}


/*
bool course::MatchesCourseNumberSearch(int courseNumber){
        return (this -> courseNumber == courseNumber);
}

bool course::Enroll(){
        if (seatsAvailable > 0){
                seatsAvailable -= 1;
                return true;
        }
        else return false;
}

friend void printCourse(){
        cout << coursePrefix << " " << courseNumber << endl;
        printf("%0.5f\n");
        cout << seatsAvailable << " seat(s) available out of " << maxSeats << "." << endl;
        printf("%0.5f\n");
        cout << "Instructor: " << instructorName << endl;
        printf("%0.5f\n");
        cout << "Class is from " << classTimes << " on " << classDays << endl;
}
*/


course.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;



class Course {
        private:
                string coursePrefix;
                int courseNumber;
                // add in instructor instructor;
                int seatsAvailable;
                int maxSeats;
        //      ClassTime meetingTime;
                string classDays;
                string instructorName;
                string classTimes;
        public:
                Course();
                Course(string courseDetails);
                bool MatchesCourseNumberSearch(int courseNumber);
                bool Enroll();
                void printCourse();
};
Last edited on
You have a vector named Course, and a class named Course. This is causing you trouble. Don't name variables the same as classes.

Also, you made a vector of Course objects, and then you tried to push_back onto it a pointer-to-Course. You can't do that. If you make a vector of Course objects, you may only put Course objects into that vector.

Also, don't use new. Don't use new. DON'T USE NEW.
Last edited on
At first I didn't use a pointer. I tried using a temporary variable to hold the value of the class object that I wanted to push_back to the vector, but that also produced an error.

error message
1
2
3
4
main.cpp: In function 'int main()':
main.cpp:23:34: error: no match for call to '(Course) (std::__cxx11::string&)'
   thisCourse.push_back(temp(lines));
                                  ^


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <vector>
#include <fstream>
#include <iostream>
#include "course.hpp"
using namespace std;


int main() {
        int menuChoice;
        string fileName;
        string lines;
        int maxEntries= 0;
        ifstream inStream;
        vector<Course> thisCourse;
        Course temp;
        cout << "Please enter the file that contains othe course data: ";
        cin >> fileName;
        inStream.open(fileName);
        cout << "TEST:  Filename is: " << fileName << endl;
        while (getline(inStream, lines)){
                cout << lines << endl;
                thisCourse.push_back(temp(lines));
        }
        cout << "Welcome to Banner NE, short for Never Existed!" << endl;
        do {
        cout << "1 - List all available classes" << endl;
        cout << "2 - Search for a course" << endl;
        cout << "3 - View your current enrolled courses" << endl;
        cout << "4 - Enroll in course" << endl;
        cout << "5 - Exit" << endl;
        cin >> menuChoice;
        cout << "TEST:  menuChoice: " << menuChoice << endl;
        if (menuChoice == 5){
                cout << "Thank you for using Banner NE and have a nice day!" << endl;
                return 0;
        }
        if (menuChoice == 2) {
                cout << endl;
                cout << "Would you like to search by" << endl;
                cout << "1 - Course prefix" << endl;
                cout << "2 - Available seats remaining" << endl;
                cout << "Make your selection: ";
                cin >> menuChoice;
                }
         } while (menuChoice != 5);
        return 0;
}
Objects are constructed when they are defined. It's too late to say temp(lines) on line 22 and expect the constructor to run.

Instead, create an unnamed temporary Course object, as in
thisCourse.push_back(Course(lines));
or let the standard library do the exact same thing for you with emplace_back, as in
thisCourse.emplace_back(lines);.

Your naming leaves some things to be desired. thisCourse is actually a collection of courses; lines is actually just one line.

Last edited on
At risk of repeating mbozzi;

Course temp; - this creates a Course object, named temp, using the default constructor.
Course temp(lines); - this creates a Course object, named temp, using the constructor that accepts a single parameter of type string.
temp(lines) - this appears to be an attempt to call a function (that doesn't exist) named temp, that accepts a single parameter of type string.
Last edited on
Topic archived. No new replies allowed.