c++ - 在 C++ 中打印带有类的字符串?

标签 c++

#ifndef DATE_H
#define DATE_H

#include <iostream>
#include <iomanip>
using namespace std;

class Date{
private:
    unsigned int day;
    unsigned int month;
    string monthName;
    unsigned int year;
public:
    Date();
    void printNumeric() const;
    void printAlpha() const;
};
#endif

我的头文件

#include "Date.h"
#include <string>
using namespace std;


Date::Date(){
    month = 1;
    monthName = "January";
    day = 1;
    year = 1970;
}

void Date::printNumeric() const{
    cout << month << "/" << day << "/" << year;
}

void Date::printAlpha() const{
    cout << Date::monthName << " " << day << ", " << year;
}

和实际代码。根据测试平台,我的 printNumeric 函数工作正常,但我的 printalpha 没有生成字符串月份名称。我是否应该对 monthName 做些什么,以便它会生成月份名称的用户输入?

最佳答案

删除 Date:: 它应该可以工作

关于c++ - 在 C++ 中打印带有类的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21485047/

相关文章:

c++ - enable_shared_from_this 中的空弱指针

c++ - 从 C/C++ 中的 .csv 文件中读取汉字

c++ - 学习 C++ : error: use of deleted function

c++ - 关于元编程的几个问题?

c++ - 为什么 omnet++ 4.6 模拟在运行时停止?

c++ - 如何在 C++ 中显式实例化模板 constexpr 变量?

c++ - 查找两个 3D 点之间距离的有效方法

C++基本整数混淆

c++ - 如何正确地将 void * 值转换为 CString

c++ - in_addr 结构(IPv4 地址)的 Mac/Windows 不同格式