无法识别.h文件中定义的typedef类型

时间:2016-09-12 07:04:09

标签: c++ templates operators typedef typename

我遇到问题让编译器识别.tem文件中函数模板的返回类型(用于将实现与.h文件分开)。

In file included from tests/Graph.h:57:0,
                 from tests/test1.cpp:3:
tests/Graph.tem:28:2: error: ‘reference’ does not name a type
  reference Node_Iterator<N, E>::operator*() const {
  ^~~~~~~~~
tests/Graph.tem:33:2: error: ‘pointer’ does not name a type
  pointer Node_Iterator<N, E>::operator->() const {

不确定我做错了什么。我使用typedef来定义返回类型的类型。

/* 
 * File: Graph.h
 */

#ifndef _Graph_h
#define _Graph_h

#include <vector>
#include <algorithm>
#include <string>
#include <memory>
#include <iostream>
#include <exception>
#include <map>
#include <set>
#include <typeinfo>

namespace gdwg {

    template <typename N, typename E> class Graph; // function prototype for Graph class

//-----------------------------------------------------------
// Iterators for Graph with Nodes N and Edges E
//-----------------------------------------------------------

    // Iterator class for Node N
    template <typename N, typename E> class Node_Iterator {

    public:
            typedef typename Graph<N, E>::Node Node;

            typedef std::ptrdiff_t                     difference_type;
            typedef std::forward_iterator_tag          iterator_category;
            const typedef N                            value_type;
            const typedef N*                           pointer;
            const typedef N&                           reference;

            // operators for value and reference types
            reference operator*() const;
            pointer operator->() const;



    private:
            bool end;
            typename std::vector< std::shared_ptr<Node> >::iterator it;
            typename std::vector< std::shared_ptr<Node> > mynodes_;

    };


    #include "Graph.tem" // definition/implementation of Graph, Node_Iterator and Edge_Iterator classes

}

#endif

这是.tem文件,它是Graph.h文件的实现,我将其包含在.h文件的底部。

#include <vector>
#include <algorithm>
#include <string>
#include <memory>
#include <iostream>
#include <exception>
#include <map>
#include <set>
#include <typeinfo>

namespace gdwg {

    //-----------------------------------------------------------
    // Implementation of Iterator class for Node N
    //-----------------------------------------------------------

    // operators for value and reference types
    template <typename N, typename E> 
    reference Node_Iterator<N, E>::operator*() const {
        return (*it)->val_;
    }

    template <typename N, typename E> 
    pointer Node_Iterator<N, E>::operator->() const {
        return &(operator*());
    }




}

1 个答案:

答案 0 :(得分:1)

  1. 为他们添加限定名称(使用 var jsonFileArr = JSON.parse(data); var log = loginData.log; var matchingItems = jsonFileArr.filter(function (obj) { return obj.log == log; }); if (matchingItems.length > 0) { // Was at least 1 found? var pass = matchingItems[0].pass; // The 'pass' data that comes with the first match } else console.log("error"); // no matches 关键字,请参阅.filter()),如:

    typename
    typename Node_Iterator<N, E>::reference ...

  2. typename Node_Iterator<N, E>::pointer...中,Graph.h位于命名空间#include "Graph.tem"的定义内,而在gdwg中,该函数在另一个命名空间{{1}内定义然后,它们将在命名空间Graph.tem中定义。您可以将gdwg移出gdwg::gdwg中的命名空间定义,或删除#include "Graph.tem"中的命名空间定义。