1

I'm new to C++ and I have written an example in CodeBlocks to see how this program works. Here is the program:

 #include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

class GradeBook
{
    public:
        GradeBook( string name )
        {
            setCourseName( name );
        }
        void setCourseName( string name )
        {
            courseName = name;
        }
        string getCourseName()
        {
            return courseName;
        }
        void displayMessage()
        {
            cout << "Welcome to the gradebook for \n" << getCourseName() << "!" << endl;
        }
    private:
        string courseName;
};

int main()
{
    GradeBook gradeBook1("Introduction to C++");
    cout << gradeBook1.displayMessage() << endl;
    return 0;

}

And as you can see I have called a displayMessage function at main and it basically should print out a statement based on the argument that I have called ealier in gradeBook1 object.

But the problem is, it does not start and I don't know why!

And here is the error log:

  ||=== Build: Debug in Youtube (compiler: GNU GCC Compiler) ===|
C:\Users\Pouya\Desktop\C++_Tutorials\Youtube\main.cpp||In function 'int main()':|
C:\Users\Pouya\Desktop\C++_Tutorials\Youtube\main.cpp|35|error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'void')|
1
  • Here's your problem: cout << gradeBook1.displayMessage() << endl; and you are calling void displayMessage(). Just invoke gradeBook1.displayMessage(); then it will work Commented Oct 29, 2017 at 7:18

1 Answer 1

1

gradeBook1.displayMessage() is a void function. Nothing to print in this line ->

cout << gradeBook1.displayMessage() << endl;

remove cout. Just type ->

gradeBook1.displayMessage();

Hope it helps. :)

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.