0

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.

1
  • 1
    I think the term you're looking for is member function. Commented Jan 13, 2014 at 23:54

1 Answer 1

3
 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.