模板:为什么这不能编译? [英] Templates: Why does this fail to compile?

查看:62
本文介绍了模板:为什么这不能编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

///

/// g ++ 3.4.1和comeau 4.3.3报告" i undefined"

///

#include< iostream>


模板< typename T>

类基地

{

protected:

int i;

};

模板< typename T>

类派生:公开基< T>

{

public:

void f(){

i = 0;

std :: cout<< i<< std :: endl;

}

};

int main()

{

派生< int> d;

df();

返回0;

}

///
/// Both g++ 3.4.1 and comeau 4.3.3 report "i undefined"
///
#include <iostream>

template<typename T>
class Base
{
protected:
int i;
};
template<typename T>
class Derived : public Base< T >
{
public:
void f() {
i = 0;
std::cout << i << std::endl;
}
};
int main()
{
Derived< int > d;
d.f();
return 0;
}

推荐答案

* A Moore:
* A Moore:
///
/// g ++ 3.4.1和comeau 4.3.3报告我未定义
///
#include< iostream>

模板< typename T>
类基地
{
受保护:
int i;
};

模板< typename T>
类派生:public Base< T>
公开:
void f(){
i = 0;
std :: cout<< i<< std :: endl;
}
};
///
/// Both g++ 3.4.1 and comeau 4.3.3 report "i undefined"
///
#include <iostream>

template<typename T>
class Base
{
protected:
int i;
};
template<typename T>
class Derived : public Base< T >
{
public:
void f() {
i = 0;
std::cout << i << std::endl;
}
};




这里''我'可以想象一个全局变量。


首选解决方案:''使用Base< T> :: i;''。


替代解决方案:''this-> i''。

-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这是一件坏事?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?



Here ''i'' could conceivably refer to a global variable.

Preferred solution: ''using Base<T>::i;''.

Alternative solution: ''this->i''.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

A Moore写道:
A Moore wrote:
#include< iostream>

模板< typename T>
类基地
{
受保护:
int i;
};

模板< typename T>
类派生:public Base< T>
{
公开:
void f(){
i = 0;


在此上下文中,非限定名称''i'不是从属名称。

非依赖名称会立即按照惯例查找

查找规则,除了从这个

进程中排除依赖基类。这就是找不到我的原因而代码无法编译。如果
在命名空间范围内声明一个''i''变量,代码将编译,但

''我'将引用命名空间变量,而不是基类成员。


如果你想让''我'在基类中引用''我'',你必须

明确地将其转换为依赖名称。这可以通过使用


this-> i = 0;


或使用合格的名字来完成


基础< T> :: i = 0;

std :: cout<< i<< std :: endl;
}
};
#include <iostream>

template<typename T>
class Base
{
protected:
int i;
};
template<typename T>
class Derived : public Base< T >
{
public:
void f() {
i = 0;
In this context non-qualified name ''i'' is not a dependent name.
Non-dependent names are looked up immediately in accordance with usual
look-up rules, except that dependent base classes are excluded from this
process. That''s why ''i'' is not found and the code fails to compile. If
declare an ''i'' variable at namespace scope, the code will compile, but
that ''i'' will refer to the namespace variable, not to the base class member.

If you want your ''i'' to refer to the ''i'' in the base class, you have to
explicitly turn it into a dependent name. This can be done by either using

this->i = 0;

or by using a qualified name

Base<T>::i = 0;
std::cout << i << std::endl;
}
};




-

祝你好运,

Andrey Tarasevich



--
Best regards,
Andrey Tarasevich


这个 - >我似乎并不依赖我...

怎么来的?


Andrey Tarasevich写道:
Well this->i does not seem very dependent to me...
How come?

Andrey Tarasevich wrote:
A Moore写道:
A Moore wrote:
#include< iostream>
template< typename T>
class Base
{
protected:
int i;
};

template< typename T>
类派生:public Base< T>
{
公开:
void f(){
i = 0;
#include <iostream>

template<typename T>
class Base
{
protected:
int i;
};
template<typename T>
class Derived : public Base< T >
{
public:
void f() {
i = 0;



在此上下文中,非限定名称''我'不是依赖名称。
根据通常的查找规则立即查找非依赖名称,除非依赖基类被排除在此
过程之外。这就是找不到我的原因而代码无法编译。如果在命名空间范围内声明一个''i''变量,代码将编译,但是'我'将引用命名空间变量,而不是基类成员。

如果你希望你的我在基类中引用我,你必须明确地将它变成一个从属名称。这可以通过使用

this-> i = 0;

或使用合格的名字来完成

Base< T>: :i = 0;


In this context non-qualified name ''i'' is not a dependent name.
Non-dependent names are looked up immediately in accordance with usual
look-up rules, except that dependent base classes are excluded from this
process. That''s why ''i'' is not found and the code fails to compile. If
declare an ''i'' variable at namespace scope, the code will compile, but
that ''i'' will refer to the namespace variable, not to the base class member.

If you want your ''i'' to refer to the ''i'' in the base class, you have to
explicitly turn it into a dependent name. This can be done by either using

this->i = 0;

or by using a qualified name

Base<T>::i = 0;

std :: cout<< i<< std :: endl;
}
};
std::cout << i << std::endl;
}
};




这篇关于模板:为什么这不能编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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