我需要在頭檔案中使用幾個函式std::cout來表示日期,但我不知道如何從頭檔案中訪問它們。如果我在班級里,我可以這樣說:
void DisplayStandard()
{
cout << month << "/" << day << "/" << year << endl;
}
然而,由于我訪問一個月,日和年(這是士兵在我的.cpp file),我不知道如何從這些修改void functions在實作檔案。這是所有的代碼:
日期類.h
#ifndef DATECLASS_H
#define DATECLASS_H
class dateclass
{
private:
int month;
int day;
int year;
public:
//default constructor
dateclass()
{
month = 1;
day = 1;
year = 1;
}
//value constructor
dateclass(int month, int day, int year)
{
this->month = month;
this->day = day;
this->year = year;
}
void DisplayStandard();
};
#endif
日期類.cpp
#include "dateclass.h"
#include <iostream>
using namespace std;
void DisplayStandard()
{
cout << "Date: " << month << "/" << day << "/" << year << endl;
}
我還沒有設定主要功能,盡管我認為沒有必要這樣做。
uj5u.com熱心網友回復:
您可以通過更改為如下所示來解決此問題void DisplayStandard()void dateclass::DisplayStandard()
void dateclass::DisplayStandard()//note the dateclass:: added infront of DisplayStandard
{
cout << "Date: " << month << "/" << day << "/" << year << endl;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/371203.html
