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_Visual C++ 6 - Fatal编程技术网

C++ 成员函数模板出错

C++ 成员函数模板出错,c++,templates,visual-c++-6,C++,Templates,Visual C++ 6,我在下面的代码中得到编译错误 class A { public: A() { } ~A() { } void func() { cout <<"Ha ha ha \n"; } }; class C { public: C() { } ~C() { } template<typename type> func() { type t; t.fun

我在下面的代码中得到编译错误

class A
{
public:
    A()
    {
    }
    ~A()
    {
    }
 void func()
 {
     cout <<"Ha ha ha \n";
 }

};

class C
{
public:
    C()
    {
    }
    ~C()
    {
    }
template<typename type> func()
{
    type t;
    t.func();
}
void callme()
{
    func<A>();
}
};
A类
{
公众:
()
{
}
~A()
{
}
void func()
{

Cuth< P> >代码>函数()/<代码>的定义没有指定返回类型,它在C++中无效。 应该是:

template<typename type> void func()
{
    type t;
    t.func();
}
template void func()
{
t型;
t、 func();
}

VC6不支持成员函数模板。您实际上有几种解决方案:

func()
移出类定义

template<typename type> void func()
{
    type t;
    t.func();
}
或者使用类模板

class C
{
public:
   template<class T> struct func
   {
      void operator()()
      {
         T t;
         t.func();
      }
   };

   void callme()
   {
      func<A>()();
   }
};
C类
{
公众:
模板结构函数
{
void运算符()()
{
T;
t、 func();
}
};
void callme()
{
func();
}
};

error C2275:“A”:非法使用此类型作为表达式您如何调用
callme()
?但这不是实际问题。@sat,g++编译代码时,如果我添加返回类型,则不会出错。Visual Studio编译器是否仍会给出相同的错误?我正在使用旧的VC6编译器…这意味着VC6是罪魁祸首。非常感谢您的立即回复
class C
{
public:
   template<class T> struct func
   {
      void operator()()
      {
         T t;
         t.func();
      }
   };

   void callme()
   {
      func<A>()();
   }
};