【问题标题】:link error with illegal call链接错误与非法调用
【发布时间】:2015-11-30 15:07:03
【问题描述】:
CUtil<char>::input(command);

我在“main.cpp”中编写了上面的代码 我为该代码制作了一个头文件,写在下面。

但我收到以下错误消息:

C2352: 'class::function' : 非法调用非静态成员函数。

有什么问题?

#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

【问题讨论】:

    标签: c++ templates generic-programming template-classes


    【解决方案1】:

    错误准确地说明了问题所在。如果你想调用CUtil&lt;char&gt;::input(command),你需要将input设为静态,或者有一个CUtil&lt;char&gt;的实例来调用input

    没有静态函数:

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

    使用静态函数:

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

    【讨论】:

      猜你喜欢
      • 2011-03-22
      • 1970-01-01
      • 2012-10-23
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      相关资源
      最近更新 更多