简体   繁体   中英

Error : Type 'struct a` does not provide a call operator

I am writing the following code and creating an object of the struct 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

i declared a vector of struct objects like:

std::vector<CpuTimeState> m_entries;

I want to call it in a function like this:

  {
    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

        }
      }
    }

Its throwing this error on compilation: Type 'struct CpuTimeState does not provide a call operator.

On the line

m_entries.emplace_back(cpu_info_obj());

cpu_info_obj is a variable, not a type. You are trying to invoke an operator() on a variable that does not implement that operator. That is what the compiler is complaining about.

To construct a new instance of your struct, you need to call the struct's constructor instead:

m_entries.emplace_back(CpuTimeState());

However, by passing in an argument, you are telling emplace_back() to invoke the struct's copy constructor. A better option is to simply omit the argument altogether and let emplace_back() invoke the struct's default constructor for you:

m_entries.emplace_back();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM