You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++调用Date类dateFormat_1()报operator<<匹配错误求助

问题分析与修复

错误信息

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

问题描述

我不确定该如何修复这个问题。我是C新手,目前正在学习高级C课程,类体系的创建让我感到困惑。成员函数dateFormat_2和dateFormat_3没有引发任何问题,编译器也没有提示错误,所以我认为它们是正确的。希望能得到帮助。

代码片段

main.cpp

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

Date.h

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

问题根源与修复方法

1. 核心错误:void类型无法用于std::cout <<

你的dateFormat_1、dateFormat_2、dateFormat_3都是void返回类型的函数——它们直接在函数内部调用std::cout输出内容,不会返回任何值。但你在main里却试图把void类型的函数返回值传给std::cout <<,这不符合语法规则,所以编译器报错。

修复方式二选一:

方式A:直接调用函数,去掉多余的std::cout <<

因为函数本身已经负责输出,所以修改main中的代码:

std::cout << "Date format 1: ";
myDate.dateFormat_1();
std::cout << std::endl;

std::cout << "Date format 2: ";
myDate.dateFormat_2();
std::cout << std::endl;

std::cout << "Date format 3: ";
myDate.dateFormat_3();
std::cout << std::endl;

方式B:修改函数返回std::string,让std::cout <<接收字符串

这种方式更符合C++的设计习惯(将数据生成和输出分离):
修改Date.h中的三个格式函数:

std::string dateFormat_1() const {
    return std::to_string(day) + "/" + std::to_string(month) + "/" + std::to_string(year);
}

std::string dateFormat_2() const {
    std::string day_s = (day < 10) ? "0" + std::to_string(day) : std::to_string(day);
    std::string month_s = (month < 10) ? "0" + std::to_string(month) : std::to_string(month);
    return day_s + "/" + month_s + "/" + std::to_string(year);
}

std::string dateFormat_3() const {
    std::string day_s = (day < 10) ? "0" + std::to_string(day) : std::to_string(day);
    std::string month_s = (month < 10) ? "0" + std::to_string(month) : std::to_string(month);
    return std::to_string(year) + month_s + day_s;
}

这样main里原来的代码就可以正常运行了。

2. 其他隐藏错误

  • Date myDate初始化错误:你的Date类只有一个带三个参数的构造函数,没有默认构造函数,所以main里的Date myDate;会编译报错,必须改成:
    Date myDate(d, m, y);
    
  • std::cin的运算符错误:main里的std::cin << d << m << y;是笔误,输入应该用>>,改成std::cin >> d >> m >> y;
  • dateFormat_2和dateFormat_3中的类型错误:else分支里的day_s = day;是把int直接赋值给std::string,这会导致编译错误,必须改成day_s = std::to_string(day);,同理month_s = month;也要改成month_s = std::to_string(month);
  • dateFormat_3缺少const修饰:这个函数不修改类成员变量,应该加上const,和前两个函数保持一致。

修复后的完整代码示例

修复后的main.cpp

#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(d, m, y);
    std::cout << "\nDay: " << myDate.getDay() << "\nMonth: " << myDate.getMonth() << "\nYear: " << myDate.getYear() << std::endl;
    
    std::cout << "Date format 1: ";
    myDate.dateFormat_1();
    std::cout << std::endl;
    
    std::cout << "Date format 2: ";
    myDate.dateFormat_2();
    std::cout << std::endl;
    
    std::cout << "Date format 3: ";
    myDate.dateFormat_3();
    std::cout << std::endl;
}

修复后的Date.h

#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() const { return day; }

    void setMonth(int m) { month = m; }
    int getMonth() const { return month; }

    void setYear(int y) { year = y; }
    int getYear() const { 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 = std::to_string(day);
        }
        if (month < 10) {
            month_s = "0" + std::to_string(month);
        } else {
            month_s = std::to_string(month);
        }
        std::cout << day_s << "/" << month_s << "/" << year;
    }

    void dateFormat_3() const {
        std::string day_s;
        std::string month_s;
        if (day < 10) {
            day_s = "0" + std::to_string(day);
        } else {
            day_s = std::to_string(day);
        }
        if (month < 10) {
            month_s = "0" + std::to_string(month);
        } else {
            month_s = std::to_string(month);
        }
        std::cout << year << month_s << day_s;
    }

private:
    int day, month, year;
};

内容的提问来源于stack exchange,提问作者nicocormier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 10:23:09