0
CUtil<char>::input(command);

I wrote the code above in "main.cpp" and I made a header file for that code, which is written below.

But I received the following error message:

C2352: 'class::function' : illegal call of non-static member function.

What's the problem?

#ifndef CUTIL_H
#define CUTIL_H

template <typename T>

class CUtil {
public:
    void input(T& command) {
        std::cin >> command;
        if (std::cin.fail()) {
            std::cin.clear();
            std::cin.ignore(100, '\n');
        }
    }
};

#endif
AndyG
  • 39,700
  • 8
  • 109
  • 143
윤성필
  • 85
  • 1
  • 1
  • 7

1 Answers1

0

The error says exactly what is wrong. If you want to call CUtil<char>::input(command) you need to make input static, or alternatively have an instance of CUtil<char> to call input on:

Without a static function:

CUtil<char> myUtil;
myUtil.input(command);

With a static function:

template <typename T>
class CUtil {
public:
    static void input(T& command) {
       // ...
    }
};

// ...
CUtil<char>::input(command);
AndyG
  • 39,700
  • 8
  • 109
  • 143