C2447在模板化类中使用大括号进行模板化基类初始化时 [英] C2447 when using braces for templated base-class initialization in templated class

查看:84
本文介绍了C2447在模板化类中使用大括号进行模板化基类初始化时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下MWE中所示的特殊情况,在我看来,MSVC错误地生成错误。在这个特定的模板化情况下,它无法识别调用基类构造函数的大括号。


 #include< ;&的iostream GT; 

模板< typename T>
class A
{
public:
A(T x);
virtual~A()=默认值;
T getX();
private:
T x;
};

模板< typename T>
B类:公共A< T>
{
public:
B(T x);
};

int main(int argn,char ** argc)
{
A< int>一个(42);
B< int> B(42);
std :: cout<< "A:" << a.getX()<< std :: endl
<< "B:" << b.getX()<<的std :: ENDL;
返回0;
}

模板< typename T>
A< T> :: A(T x):x(x){}

模板< typename T>
T A< T> :: getX(){return x; }

模板< typename T>
B< T> :: B(T x):A< T> {x * x} {} //错误C2447:'{':缺少函数头(旧式正式列表?)

我找不到任何关于此的错误报告,因此在这里喊出来。

解决方案


模板< typename T> B< T> :: B(T x):A< T> {x * x} {}


你好,


你确定这是正确的语法吗?


我从未见过这种语法。我认为你应该写:

 template< typename T> B< T> :: B(T x):A< T>(x * x ){} 

问候,Guido


I came accross this particular case illustrated in the following MWE and it seems to me MSVC generates an error by mistake. In this particular templated case it doesn't recognize the braces to invoke the base-class constructor.

#include <iostream>

template <typename T>
class A
{
public:
    A(T x);
    virtual ~A() = default;
    T getX();
private:
    T x;
};

template <typename T>
class B : public A<T>
{
public:
    B(T x);
};

int main(int argn, char** argc)
{
    A<int> a(42);
    B<int> b(42);
    std::cout << "A: " << a.getX() << std::endl
        << "B: " << b.getX() << std::endl;
    return 0;
}

template<typename T>
A<T>::A(T x) : x(x) {}

template<typename T>
T A<T>::getX() { return x; }

template<typename T>
B<T>::B(T x) : A<T>{ x*x } {} // error C2447: '{': missing function header (old-style formal list?)

I couldn't find any bug-report on this, hence the shout-out here.

解决方案

template<typename T> B<T>::B(T x) : A<T>{ x*x } {}

Hello,

are you sure this is the correct syntax?

I've never seen this syntax. I think you should write:

template<typename T>B<T>::B(T x) : A<T>( x*x ) {}

Regards, Guido


这篇关于C2447在模板化类中使用大括号进行模板化基类初始化时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆