Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 链接错误与非法呼叫_C++_Templates_Generic Programming_Template Classes - Fatal编程技术网

C++ 链接错误与非法呼叫

C++ 链接错误与非法呼叫,c++,templates,generic-programming,template-classes,C++,Templates,Generic Programming,Template Classes,我在main.cpp中编写了上面的代码 我为代码创建了一个头文件,如下所示 但我收到了以下错误消息: C2352:“类::函数”:非法调用非静态成员函数 有什么问题吗 CUtil<char>::input(command); 错误正好说明了问题所在。如果要调用CUtil::inputcommand,则需要将输入设置为静态,或者使用一个CUtil实例来调用输入: 没有静态功能: #ifndef CUTIL_H #define CUTIL_H template <typenam

我在main.cpp中编写了上面的代码 我为代码创建了一个头文件,如下所示

但我收到了以下错误消息:

C2352:“类::函数”:非法调用非静态成员函数

有什么问题吗

CUtil<char>::input(command);

错误正好说明了问题所在。如果要调用CUtil::inputcommand,则需要将输入设置为静态,或者使用一个CUtil实例来调用输入:

没有静态功能:

#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
CUtil<char> myUtil;
myUtil.input(command);
具有静态功能:

#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
CUtil<char> myUtil;
myUtil.input(command);