Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#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;
};

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading