TinMan
TinMan

Reputation: 99

How to call a function inside a class

I am trying to call a function inside a class, when I try I get the error "no operator << matches these operands" right before instructor.displayMessage(). Also, am I calling instructor.displayMessage() correctly? I am new to c++

#include <iostream>
#include "GradeBook.h"
using namespace std;
int main()
{
    GradeBook gradeBook1("CS101 Introduction to C++ Programming");
    GradeBook gradeBook2("CS102 Data Structures in C++");
    GradeBook instructor("");

    instructor.setInstructorName();

    cout << "gradeBook1 created for course: \n" << gradeBook1.getCourseName() << instructor.displayMessage()
        << "\ngradeBook2 created for course: \n" << gradeBook2.getCourseName()
        << endl;
    cout << "\nPress any key to exit" << endl;
    getchar();
}

Header:

#include <string>
using namespace std;

class GradeBook{
public:
    GradeBook(string);
    void setCourseName(string);
    string getCourseName();
    void displayMessage();
    void setInstructorName();
    string getInstructorName();
private:
    string courseName;
    string instructorName;
};

I didnt include the functions because I dont think they are part of the problem.

Upvotes: 0

Views: 173

Answers (1)

user3072164
user3072164

Reputation:

 void displayMessage();

This function does not return anything, yet you try to print its return value here:

 cout << "gradeBook1 created for course: \n" << gradeBook1.getCourseName() << instructor.displayMessage()

If it actually should return something, then you have to declare it with the correct return type, for example

 string displayMessage();

However the name suggests that the function itself prints the output already. So maybe you simply want to call it, like this:

 instructor.displayMessage();

in a single line.

If you provide the implementation of displayMessage() I might give a more precise answer.

Upvotes: 3

Related Questions