我想<ctime>在 C 類的范圍內使用。但是,每當我嘗試編譯以下類時,都會收到錯誤訊息:
error: 'time' cannot be used as a function
time_t now = time(0);
我認為這可能與我試圖在Session類內部呼叫該函式有關,因為我已經設法在time()函式內部呼叫該main()函式。
我究竟做錯了什么?
Session.h
#ifndef _SESSION_H_
#define _SESSION_H_
#include <string>
#include <ctime>
class Session
{
private:
std::string language;
time_t date;
time_t time;
public:
Session(std::string language, time_t date = NULL, time_t time = NULL);
~Session();
};
#endif
Session.cpp
#include <ctime>
#include "Session.h"
Session::Session(std::string language, time_t date, time_t time) : language{language}, date{date}, time{time}
{
if (date == NULL)
{
time_t now = time(0);
tm *ltm = localtime(&now);
}
}
Session::~Session() {}
uj5u.com熱心網友回復:
您宣告了一個與time函式名稱同名的建構式引數:
Session::Session(std::string language, time_t date, time_t time)
^
在建構式塊范圍內,引數隱藏了同名函式(以??及類定義中宣告的同名資料成員)。
因此,使用該函式的限定名稱:
time_t now = ::time(0);
要訪問資料成員,請使用帶有指標的運算式this,例如this->time.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/421141.html
標籤:
上一篇:如何比較資料框中的列
