2016-09-12 2 views
3

У меня возникают проблемы с получением компилятором для распознавания типа возвращаемого значения для шаблона функции в файле .tem (используется для разделения реализации из файла .h).Невозможно распознать тип typedef, определенный в файле .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
  1. Добавить квалифицированное имя для них (с typename ключевого слова см Where and why do I have to put the “template” and “typename” keywords?), как:

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

  2. В Graph.h, #include "Graph.tem" находится внутри определения пространство имен gdwg, а в Graph.tem функция определена в ins ide другое пространство имен gdwg, то они будут определены в пространстве имен gdwg::gdwg. Вы можете переместить #include "Graph.tem" из определения пространства имен в Graph.h или удалить определение пространства имен в Graph.tem.

+0

Предполагается, что в соответствии с моими спецификациями оно должно быть внутри. Хорошо, что ошибка ушла, но я получил это: tests/Graph.tem: 28: 75: error: определение 'const N & gdwg :: Node_Iterator :: operator *() const' не находится в пространстве имен, в котором содержится 'gdwg :: Node_Iterator '[-fpermissive] typename Node_Iterator :: ссылка Node_Iterator :: operator *() const { – iteong

+0

@iteong В этом проблема. '#include" Graph.tem "' внутри определения пространства имен 'gdwg', а внутри' Graph.tem' определено другое пространство имен 'gdwg', наконец станет sth как' namespace gdwg {namespace gdwg {... ' , Если вам нужно «#include» Graph.tem «внутри определения пространства имен», удалите определение пространства имен в «Graph.tem». – songyuanyao

+0

О, хорошо, поэтому я удаляю gdwg в ​​.tem-файле? – iteong