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_Inheritance_Scope_Name Lookup - Fatal编程技术网

C++ 对基类成员数据的派生模板类访问

C++ 对基类成员数据的派生模板类访问,c++,templates,inheritance,scope,name-lookup,C++,Templates,Inheritance,Scope,Name Lookup,这是一个进一步的问题中提出的 使用以下类定义: template <class T> class Foo { public: Foo (const foo_arg_t foo_arg) : _foo_arg(foo_arg) { /* do something for foo */ } T Foo_T; // either a TypeA or a TypeB - TBD foo_arg_t _foo_arg;

这是一个进一步的问题中提出的

使用以下类定义:

template <class T>
class Foo {

public:
    Foo (const foo_arg_t foo_arg) : _foo_arg(foo_arg)
    {
        /* do something for foo */
    }
    T Foo_T;        // either a TypeA or a TypeB - TBD
    foo_arg_t _foo_arg;
};

template <class T>
class Bar : public Foo<T> {
public:
    Bar (const foo_arg_t bar_arg, const a_arg_t a_arg)
    : Foo<T>(bar_arg)   // base-class initializer
    {

        Foo<T>::Foo_T = T(a_arg);
    }

    Bar (const foo_arg_t bar_arg, const b_arg_t b_arg)
    : Foo<T>(bar_arg)
    {
        Foo<T>::Foo_T = T(b_arg);
    }

    void BarFunc ();

};

template <class T>
void Bar<T>::BarFunc () {
    std::cout << _foo_arg << std::endl;   // This doesn't work - compiler error is: error: ‘_foo_arg’ was not declared in this scope
    std::cout << Bar<T>::_foo_arg << std::endl;   // This works!
}
模板
福班{
公众:
Foo(const Foo_arg_t Foo_arg):\u Foo_arg(Foo_arg)
{
/*为福做点什么*/
}
T Foo_T;//类型a或类型B-待定
富阿格富阿格;
};
模板
分类栏:公共食品{
公众:
Bar(常量foo_arg_t Bar_arg,常量a_arg_t a_arg)
:Foo(bar_arg)//基类初始值设定项
{
Foo::Foo_T=T(a_arg);
}
Bar(常数foo_arg_t Bar_arg,常数b_arg_t b_arg)
:Foo(bar_arg)
{
Foo::Foo_T=T(b_arg);
}
void-BarFunc();
};
模板
void Bar::BarFunc(){

Std::CuthScript。

在Visual C++ 2008中看起来很好。我为你提到的类型添加了一些虚拟定义,但没有给出任何源。其余的和你所说的一样。然后强制一个主函数强制<代码> BarFunc <代码>被实例化和调用。

#include <iostream>

class streamable {};
std::ostream &operator<<(std::ostream &os, streamable &s) { return os; }

class foo_arg_t : public streamable {};
class a_arg_t : public streamable {};
class b_arg_t : public streamable  {};

template <class T>
class Foo {

public:
    Foo (const foo_arg_t foo_arg) : _foo_arg(foo_arg)
    {
        /* do something for foo */
    }
    T Foo_T;        // either a TypeA or a TypeB - TBD
    foo_arg_t _foo_arg;
};

template <class T>
class Bar : public Foo<T> {
public:
    Bar (const foo_arg_t bar_arg, const a_arg_t a_arg)
    : Foo<T>(bar_arg)   // base-class initializer
    {

        Foo<T>::Foo_T = T(a_arg);
    }

    Bar (const foo_arg_t bar_arg, const b_arg_t b_arg)
    : Foo<T>(bar_arg)
    {
        Foo<T>::Foo_T = T(b_arg);
    }

    void BarFunc ();

};

template <class T>
void Bar<T>::BarFunc () {
    std::cout << _foo_arg << std::endl; 
    std::cout << Bar<T>::_foo_arg << std::endl;   
}

int main()
{
    Bar<a_arg_t> *b = new Bar<a_arg_t>(foo_arg_t(), a_arg_t());
    b->BarFunc();
}
#包括
类可流{};

std::ostream&operator您可以使用
this->
来明确您指的是类的一个成员:

void Bar<T>::BarFunc () {
    std::cout << this->_foo_arg << std::endl;
}

这向编译器表明,成员名称取决于模板参数,因此它会在正确的位置搜索该名称的定义。有关更多信息,请参阅。

此处,基类不是非依赖基类(这意味着一个完全类型可以在不知道模板参数的情况下被确定),和<代码> { FuuYOGARG< /COD> >是一个非依赖名称。标准C++表示在依赖基类中没有查找非依赖名称。 要更正代码,只需使名称
\u foo\u arg
依赖即可,因为依赖名称只能在实例化时查找,此时必须探究的确切基础专门化将已知。例如:

// solution#1
std::cout << this->_foo_arg << std::endl;

关于顶部的定义,g++发出了很多错误。但是,范围问题仍然存在:“错误:''u foo_arg'未在此范围内声明”,这是由于在BarFunc()中第一次调用了''u foo_arg'定义。你是说我的伪类型声明在gcc上会给你带来错误吗?是的,上面的伪类型,但范围错误仍然存在。我相信g++可能是正确的,关于:你最初的问题。IBM编译器也引起了同样的骚动,IIRC。对不起,我添加了一些测试代码-这会给我带来错误。你发布的代码如果使用t他在BarFunc()中使用了this->_foo_arg而不是_foo_arg。到常见问题解答的链接非常有用:它还显示了此问题在哪些地方可能导致不必要的行为。知道为什么会这样吗?(常见问题解答没有完全回答这个问题)
// solution#1
std::cout << this->_foo_arg << std::endl;
// solution#2
std::cout << Foo<T>::_foo_arg << std::endl;
// solution#3
template <class T>
class Bar : public Foo<T> {
public:
    ...
    void BarFunc ();
private:
    using Foo<T>::_foo_arg;
};

template <class T>
void Bar<T>::BarFunc () {
    std::cout << _foo_arg << std::endl;   // works
}