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/

相关文章:

android - while循环中的一个变量没有改变,ndk/android

c++ - 现代 C++ 函数返回函数指针的方式

c++ - 将 c/c++ 代码移植到 VxWorks 的问题

c++ - 为什么我的字符串转换为 float-conversion 不能用 istringstream 得到所有小数?

c++ - 为什么此代码在 Mac 和 Red Hat 中运行如此不同

c++ - 在 C++ 中使用 .find() 和 struct 作为映射中的键

c++ - 由于 "Type name is not allowed",使代码在 MSVC++ 2010 中工作的解决方法

c++ - 检查失败 : 1 == NumElements() (1 vs. 1792)在 Tensorflow C++ 中必须有一个元素张量

c++ - lambdas:这个捕获忽略常量(与 std::bind 相比)

c++ - 使用 JNIEnv::FindClass:我需要释放返回的 jclass 引用吗?