[英]Error : Type 'struct a` does not provide a call operator
我正在編寫以下代碼並創建 struct CPuTimeState 的CPuTimeState
:
struct CpuTimeState
{
///< Field for time spent in user mode
int64_t CS_USER = {};
///< Field for time spent in user mode with low priority ("nice")
int64_t CPU_TIME_STATES_NUM = 10;
std::string cpu_label;
auto sum() const {
return CS_USER + CS_NICE + CS_SYSTEM + CS_IDLE + CS_IOWAIT + CS_IRQ + CS_SOFTIRQ + CS_STEAL;
}
} cpu_info_obj; // struct CpuTimeState
我聲明了一個結構對象向量,例如:
std::vector<CpuTimeState> m_entries;
我想在 function 中這樣稱呼它:
{
std::string line;
const std::string cpu_string("cpu");
const std::size_t cpu_string_len = cpu_string.size();
while (std::getline(proc_stat_file, line)) {
// cpu stats line found
if (!line.compare(0, cpu_string_len, cpu_string)) {
std::istringstream ss(line);
// store entry
m_entries.emplace_back(cpu_info_obj());
cpu_info_obj &entry = m_entries.back();
// read cpu label
ss >> entry.cpu_label;
// count the number of cpu cores
if (entry.cpu_label.size() > cpu_string_len) {
++m_cpu_cores;
}
// read times
//for (uint8_t i = 0U; i < CpuTimeState::CPU_TIME_STATES_NUM; ++i) {
ss >> entry
}
}
}
它在編譯時拋出此錯誤: Type 'struct CpuTimeState
不提供調用運算符。
在線上
m_entries.emplace_back(cpu_info_obj());
cpu_info_obj
是一個變量,而不是一個類型。 您試圖在未實現該運算符的變量上調用operator()
。 這就是編譯器所抱怨的。
要構造結構的新實例,您需要調用結構的構造函數:
m_entries.emplace_back(CpuTimeState());
但是,通過傳入一個參數,您是在告訴emplace_back()
調用該結構的復制構造函數。 更好的選擇是完全省略參數並讓emplace_back()
為您調用結構的默認構造函數:
m_entries.emplace_back();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.