When testing my code, using cout for my class operator results in an operator error

162 Views Asked by At

I am receiving this error:

main.cpp: In function ‘int main()’:
main.cpp:14:36: error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘void’)
   14 |     std::cout << "Date format 1: " << myDate.dateFormat_1();
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^~ ~~~~~~~~~~~~~~~~~~~~~
      |               |                                          |
      |               std::basic_ostream<char>                   void

I am unsure on how to fix this. I am new to c++, currently in a master c++ course, and creating the class systems has been confusing time. The member functions dateFormat_2 and 3 have not resulted in any issues nor prompt errors within the compiler so I am somewhat sure they are correct. Any help would be appreciated.

This is my main

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

int main()
{
    int d;
    int m;
    int y;
    std::cout << "Input three integers for day, month, year: ";
    std::cin << d << m << y;
    Date myDate
    std::cout << "\nDay: " << myDate.getDay() << "\nMonth: " << myDate.getMonth() << "\nYear: " << myDate.getYear() << std::endl;
    std::cout << "Date format 1: " << myDate.dateFormat_1() << std::endl;
    std::cout << "Date format 2: " << myDate.dateFormat_2() << std::endl;
    std::cout << "Date format 3: " << myDate.dateFormat_3() << std::endl;
}

this is my header

#include <iostream>
#include <string>

class Date
{
    public:
    Date(int d, int m, int y)
    {
        day = d;
        month = m;
        year = y;
    }
    void setDay(int d)
    {
        day = d;
    }
    int getDay()
    {
        return day;
    }
    void setMonth(int m)
    {
        month = m;
    }
    int getMonth()
    {
        return month;
    }
    void setYear(int y)
    {
        year = y;
    }
    int getYear()
    {
        return year;
    }
    void dateFormat_1()
    const{
        std::cout << day << "/" << month << "/" << year;
    }
    void dateFormat_2()
    const{
       std::string day_s;
        std::string month_s;
        if(day<10)
        {
            day_s = '0' + std::to_string(day);
        }
        else
        {
            day_s = day;
        }
        if(month<10)
        {
            month_s = '0' + std::to_string(month);
        }
        else
        {
            month_s = month;
        }
        std::cout << day_s << "/" << month_s << "/" << year;
    }
    
    void dateFormat_3()
    {
        std::string day_s;
        std::string month_s;
        if(day<10)
        {
            day_s = '0' + std::to_string(day);
        }
        else day_s = day;
        
        if(month<10)
        {
            month_s = '0' + std::to_string(month);
        }
        else month_s = month;
        
        std::cout << year << month_s << day_s;
    }
    
    private:
    int day, month, year;
};

2

There are 2 best solutions below

0
Some programmer dude On BEST ANSWER

If we make it simpler, you're essentially doing

std::cout << myDate.dateFormat_1();

This calls the function myDate.dateFormat_1() and will attempt to output the value it returns. But myDate.dateFormat_1() doesn't return anything, so you get the error.

What you want is just a plain function call:

std::cout << "Date format 1: ";  // Output heder text
myDate.dateFormat_1();           // Call function, and it will write its own output
std::cout << '\n';               // Output a trailing newline

Similarly with the other two functions, call them separately.

0
Marek R On

Some programmer dude gave you a quick fix which is fine, but IMO problem you should think about changing API of your Date.

Solution depends on skill level, that is why Some programmer dude provided minimal solution, but I'd like to move you one step forward, by introducing std::string as a return type and C++20 std::format which is more handy and easy to use:

class Date {
....
    std::string dateFormat_1() const
    {
        return std::format("{}/{}/{}", day, month, year);
    }

    std::string dateFormat_2() const
    {
        return std::format("{:02}/{:02}/{:02}", day, month, year);
    }

    std::string dateFormat_3()
    {
        return std::format("{:02}/{:02}/{:02}", year, month, day);
    }
....
};

Live demo (I've fixed more mistakes in your code to make it compile).

This can be also achieved in old C++ standard by using std::ostringstream, but its API is more complex and harder to use.

Depending on compiler (and it is version) you are using you have enable C++20 it respective way.