相关疑难解决方法(0)

C++模板:'不是从类型'派生的'

为什么这段代码无效?

#include <vector>

template <typename T>
class A {
  public:
    A() { v.clear(); }

    std::vector<A<T> *>::const_iterator begin() {
      return v.begin();
    }

  private:
    std::vector<A<T> *> v;
};
Run Code Online (Sandbox Code Playgroud)

GCC报告以下错误:

test.cpp:8: error: type 'std::vector<A<T>*, std::allocator<A<T>*> >' is not derived from type 'A<T>'
test.cpp:8: error: expected ';' before 'begin'
test.cpp:12: error: expected `;' before 'private'
Run Code Online (Sandbox Code Playgroud)

怎么了?怎么解决?

c++ templates compilation

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

使用boost图库的模板化typedef汤

我正在尝试创建一个扩展boost图库行为的类.我希望我的类成为一个模板,用户提供一个类型(类),用于存储每个顶点的属性.那只是背景.我正在努力创建一个更简洁的typedef来用于定义我的新类.

基于其他职位喜欢这个这个,我决定来定义一个struct将包含模板的typedef.

我将展示两种密切相关的方法.我无法弄清楚为什么GraphType的第一个typedef似乎正在工作,而VertexType的第二个类型失败.

#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>

template <class VP>
struct GraphTypes
{
    typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VP > GraphType;
    typedef boost::graph_traits< GraphType >::vertex_descriptor VertexType;
};

int main()
{
    GraphTypes<int>::GraphType aGraphInstance;
    GraphTypes<int>::VertexType aVertexInstance;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译器输出:

$ g++ -I/Developer/boost graph_typedef.cpp 
graph_typedef.cpp:8: error: type ‘boost::graph_traits<boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS, VP, boost::no_property, boost::no_property, boost::listS> >’ is not derived from type ‘GraphTypes<VP>’
graph_typedef.cpp:8: error: expected ‘;’ before ‘VertexType’
graph_typedef.cpp: In function ‘int main()’:
graph_typedef.cpp:14: error: ‘VertexType’ is not a member …
Run Code Online (Sandbox Code Playgroud)

c++ templates boost typedef

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

标签 统计

c++ ×2

templates ×2

boost ×1

compilation ×1

typedef ×1