相关疑难解决方法(0)

我必须在何处以及为何要使用"模板"和"typename"关键字?

在模板,在那里,为什么我必须把typenametemplate上依赖的名字呢?究竟什么是依赖名称?我有以下代码:

template <typename T, typename Tail> // Tail will be a UnionNode too.
struct UnionNode : public Tail {
    // ...
    template<typename U> struct inUnion {
        // Q: where to add typename/template here?
        typedef Tail::inUnion<U> dummy; 
    };
    template< > struct inUnion<T> {
    };
};
template <typename T> // For the last node Tn.
struct UnionNode<T, void> {
    // ...
    template<typename U> struct inUnion {
        char fail[ -2 + (sizeof(U)%2) ]; // Cannot be instantiated for any …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++-faq dependent-name typename

1061
推荐指数
8
解决办法
15万
查看次数

什么时候需要"typename"关键字?

可能重复:
正式,什么是typename?
我必须在哪里以及为什么要放置模板和typename关键字?

考虑以下代码:

template<class K>
class C {
    struct P {};
    vector<P> vec;
    void f();
};

template<class K> void C<K>::f() {
    typename vector<P>::iterator p = vec.begin();
}
Run Code Online (Sandbox Code Playgroud)

为什么此示例中需要"typename"关键字?是否还有其他必须指定"typename"的情况?

c++ syntax templates typename

50
推荐指数
3
解决办法
3万
查看次数

模板类中的Typedef不起作用

我有以下代码的问题:

template <typename U>
class lamePtr
{
public:
    typedef U* ptr;
};

template <typename U>
class smarterPointer
{
    public:
    void funFun()
    {
        typedef lamePtr<U> someType;
        someType::ptr query;
    }
};
Run Code Online (Sandbox Code Playgroud)

如你所见,我在lamePtr中有一个typedef.在smarterPointer类里面我有一个函数funFun().我想做的是制作另一个typedef someType.直到该行,一切正常,直到我们到someType :: ptr查询行.

我想要发生的是"查询"将成为lamePtr <U> :: ptr(一个简单的值,而不是typedef;).但是,我得到编译错误(使用gcc 4.4.3):

temp.cpp: In member function ‘void smarterPointer<U>::funFun()’:
temp.cpp:15: error: expected ‘;’ before ‘query’
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

c++ templates typedef class typename

8
推荐指数
2
解决办法
1万
查看次数

C++ 中 typename 应该在 const 之前还是之后?

我应该写:

template<class T> class Foo {
    typename const T* x;
};
Run Code Online (Sandbox Code Playgroud)

或者:

template<class T> class Foo {
    const typename T* x;
};
Run Code Online (Sandbox Code Playgroud)

c++ oop class constants typename

6
推荐指数
2
解决办法
4781
查看次数

模板问题('typename'不是模板函数参数)

实际上我用intel编译器编译一些库时遇到了问题.

使用g ++正确编译了相同的库.

问题是由模板引起的.我想要理解的是 **typename**函数体内的模板函数参数和变量声明的声明

例:

void func(typename sometype){..
...
typename some_other_type;
..
}
Run Code Online (Sandbox Code Playgroud)

编译这种代码产生以下错误(英特尔),(gcc没有声称):我有以下错误

../../../libs/log/src/attribute_set.cpp(415): error: no operator "!=" matches these operands
            operand types are: boost::log_st::basic_attribute_set<wchar_t>::iter<'\000'> != boost::log_st::basic_attribute_set<wchar_t>::iter<'\000'>
      while (begin != end)
                   ^
          detected during instantiation of "void boost::log_st::basic_attribute_set<CharT>::erase(boost::log_st::basic_attribute_set<CharT>::iter<'\000'>, boost::log_st::basic_attribute_set<CharT>::iter<'\000'>) [with CharT=wchar_t]" at line 438

../../../boost/log/attributes/attribute_set.hpp(115): error: no operator "!=" matches these operands
            operand types are: boost::log_st::basic_attribute_set<wchar_t>::iter<'\000'> != boost::log_st::basic_attribute_set<wchar_t>::iter<'\000'>
              if (it != m_pContainer->end())
Run Code Online (Sandbox Code Playgroud)

我想要理解的是在函数体内使用typename,参数声明.

例:

template< typename CharT >
struct basic_attribute_values_view< CharT >::implementation
{

public:
..
..
void …
Run Code Online (Sandbox Code Playgroud)

c++ boost generic-programming

4
推荐指数
2
解决办法
1994
查看次数

消除结构中关联类型的歧义

我正在尝试运行这个

use std::collections::BTreeSet;

pub struct IntoIter<T> {
    iter: BTreeSet<T>::IntoIter,
}

fn main() {}
Run Code Online (Sandbox Code Playgroud)

操场

这失败了

use std::collections::BTreeSet;

pub struct IntoIter<T> {
    iter: BTreeSet<T>::IntoIter,
}

fn main() {}
Run Code Online (Sandbox Code Playgroud)

为什么关联类型不明确?

rust

4
推荐指数
1
解决办法
986
查看次数

C++:返回指向模板子类的指针

做这样的事情的语法是什么?当我尝试编译下面的代码时,它告诉我a ';' was expected before '*',指向函数的返回类型,ResourceManager<T>::ResourceWrapper*.

template<class T>
class ResourceManager
{
  private:
    struct ResourceWrapper;
    ResourceWrapper* pushNewResource(const std::string& file);
};

// (Definition of ResourceWrapper not shown.)

template <class T>
ResourceManager<T>::ResourceWrapper* ResourceManager<T>::pushNewResource(
    const std::string& file)
{
    // (Irrelevant details)
}
Run Code Online (Sandbox Code Playgroud)

c++ templates

3
推荐指数
1
解决办法
1522
查看次数

C ++中“typename”的目的是什么

可能的重复:
正式地说,typename 是什么?

当我使用

template <typename TMap>
typename TMap::referent_type * func(TMap & map, typename TMap::key_type key) 
{ ... }
Run Code Online (Sandbox Code Playgroud)

第二行的两个“typename”的目的是什么?

它似乎触发了编译时警告(VS2008:C4346),但它只是“您声称这是一种类型”吗?

即在TMap::referent_type模板被实例化时进行实际检查是否实际类型,但似乎仍然有 C++ 标准的要求,但代码被正确解析。

有没有实际需要类型名来解决歧义的例子?或者还有更多吗?

c++ templates typename

2
推荐指数
1
解决办法
431
查看次数

模板类型参数的模板实参必须是类型

我正在开发一个template Subject class将用作某些事件驱动编程的基类。目前,我正在进行回调注册,并且我Template argument for template type parameter must be a type; did you forget 'typename'?在 行收到了“” std::map

我觉得答案与此链接相关,但我无法形成它来解决我的问题。

这是类的标题Subject

#ifndef Subject_hpp
#define Subject_hpp

#include "Observer.hpp"

#include <list>
#include <map>

template<class T>
class Subject
{
public:
    Subject();
    virtual ~Subject();

    virtual void attach( Observer* const object, void(Observer::* const func)(T) );
    virtual void detach(const int pObserverId);
    virtual void notify(T& pEvent);

    // Clear the subject: Clear the attached observer list
    void clear();

private:


    std::list< …
Run Code Online (Sandbox Code Playgroud)

c++ templates stdmap

2
推荐指数
1
解决办法
1万
查看次数

将迭代器传递给函数

查看二叉树的源代码,我找到以下函数:

//definition of BTR,in case you'd want to know
template< class Type>
struct BTR 
{
    // The item saved to that specifiec position into the tree
    Type  value;    

    // Points to the left leaf
    BTR<Type>*  left;

    // Points to the right leaf
    BTR<Type>*  right;  
};

//why typename?
template< class Type>
BTR<Type>* CreateEx(typename const std::vector<Type>::iterator start,typename const std::vector<Type>::iterator end) 
{
    //definition
}
Run Code Online (Sandbox Code Playgroud)

现在,让我对这个功能感到困惑的是它的参数.为什么需要关键字typename?因为如果我删除这两个类型名称,我的编译器开始抱怨并说我应该在标识符'start'之前加上')'.如果我更改了参数以便函数使用两个向量而不是两个迭代器并删除了类型名称,我的编译器就会停止抱怨(当然,该函数不再起作用).

// perfectly acceptable!
template< class Type>
BTR<Type>* CreateEx( const std::vector<Type> start, const std::vector<Type> end)
Run Code Online (Sandbox Code Playgroud)

所以我似乎需要关键字,因为该函数需要两个迭代器.但是为什么在这样的情况下这个关键字是必要的呢?

c++ templates iterator vector typename

1
推荐指数
1
解决办法
2634
查看次数

C++类模板的奇怪错误

任何人都可以建议为什么这不编译?我想我错过了一些重要的东西.编译器是g ++ 4.2.1(在OS X上),错误是"预期的`;" 在'it'之前,在声明迭代器的行上.

#include <vector>

template <class T>
class A {
public:
    struct SomeStruct {
        T* ptr;
        int i;
    };

    typedef std::vector<SomeStruct> MyList;

    void Func()
    {
        MyList::iterator it;
    }
};
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
196
查看次数

模板化类嵌套类型typename

我要声明的Itertypename,但没有typedef.
然后,我不能Iter在我的template class声明中使用=>令人沮丧:(请教
我如何使用Iter,或者解释我为什么C++如此讨厌 ......

template <typename T>
struct MyContainer
{
  typedef  std::vector<T>  Vec;
  typename Vec::iterator   Iter;

  void Fo (typename std::vector<T>::iterator);  //ok
  typename std::vector<T>::iterator Ba();       //ok

  void Foo (Iter);   //error: 'Iter' is not a type
  Iter Bar ();       //error: 'Iter' does not name a type
};                   //(gcc 4.1.2)
Run Code Online (Sandbox Code Playgroud)
  1. 为什么typedef不能用来申报Iter?为什么不?
  2. 如何使用Itertemplate class?(例如作为函数参数类型)

编辑
在指令中typename Vec::iterator Iter;,关键字typename表示 …

c++ templates typedef nested-class typename

1
推荐指数
1
解决办法
1177
查看次数