Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8538627
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:09:48+00:00 2026-06-11T11:09:48+00:00

I wrote these classes that represent a graph: class Node{ public: typedef std::vector<Node>::iterator iterator;

  • 0

I wrote these classes that represent a graph:

class Node{

  public:
    typedef std::vector<Node>::iterator iterator;
    iterator begin() {return neigh.begin();}
    iterator end() {return neigh.end();}

  public:
    Node(std::string n) : name(n) {}

    std::string getName() {return name; }

    void addNeigh(Node n){
      neigh.push_back(n);
    } 

  private:
    std::string name;
    std::vector<Node> neigh;
};

class Map{

  public:
    typedef std::vector<Node>::iterator iterator;
    iterator begin() {return nodes.begin();}
    iterator end() {return nodes.end();}

  public:
    Map(std::string n) : map_name(n) {}

    void addNode(Node n){
      nodes.push_back(n);
    }


    std::string getName() { return map_name; }
    int getNumNodes() {return nodes.size();}

  private:
    std::string map_name;
    std::vector<Node> nodes;

};

Then i want to write a function printGraph that prints some graph informations using a traits:

template<typename T>
struct GraphTraits{

};

template<>
struct GraphTraits<Map>{

  typedef Map::iterator node_iterator;

  static node_iterator node_begin(Map map){ return map.begin();}
  static node_iterator node_end(Map map) { return map.end();}

  static std::string getName(Map m) {return m.getName();}

};

template<typename T>
void printGraph(T t){
  std::cout << std::endl 
            << "--------------------------------------" << std::endl  
            << "|             printGraph             |" << std::endl  
            << "--------------------------------------" << std::endl;
  std::cout << "Graph name: " << GraphTraits<T>::getName(t) << std::endl;

  for(GraphTraits<T>::node_iterator it = GraphTraits<T>::node_begin(t), e = GraphTraits<T>::node_end(t);
        it != e; ++it) {
          std::cout<< (*it).getName() << std::endl; }
}

If i try to compile this code i get these errors:

grafo.cpp:75:37: error: expected ';' in 'for' statement specifier
  for(GraphTraits<T>::node_iterator it = GraphTraits<T>::node_begin(t), e = GraphTraits<T>::node_end(t);
                                    ^
grafo.cpp:75:37: error: use of undeclared identifier 'it'
grafo.cpp:75:73: error: use of undeclared identifier 'e'
  for(GraphTraits<T>::node_iterator it = GraphTraits<T>::node_begin(t), e = GraphTraits<T>::node_end(t);
                                                                        ^
grafo.cpp:76:9: error: use of undeclared identifier 'it'
        it != e; ++it) {
        ^
grafo.cpp:76:15: error: use of undeclared identifier 'e'
        it != e; ++it) {
              ^
grafo.cpp:76:16: error: expected ')'
        it != e; ++it) {
               ^
grafo.cpp:75:6: note: to match this '('
  for(GraphTraits<T>::node_iterator it = GraphTraits<T>::node_begin(t), e = GraphTraits<T>::node_end(t);
     ^
grafo.cpp:76:20: error: use of undeclared identifier 'it'
        it != e; ++it) {
                   ^
grafo.cpp:75:23: error: unexpected type name 'node_iterator': expected expression
  for(GraphTraits<T>::node_iterator it = GraphTraits<T>::node_begin(t), e = GraphTraits<T>::node_end(t);
                      ^
grafo.cpp:104:3: note: in instantiation of function template specialization 'printGraph<Map>' requested here
  printGraph(mappa);
  ^

I can solve and compile successfully substituting:

for(GraphTraits<T>::node_iterator....

with:

for(GraphTraits<Map>::node_iterator....

But this makes printGraph to loose it’s generality.

Can someone give me some hint to solve the problem?

Talking in generale, is it possible to use a Traits to force a data-type definition? I mean:

template<typename T>
struct ATraits{
 -> type "iterator" must be defined
};

such that every specialization has to define:

typedef ..something.. iterator;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:09:49+00:00Added an answer on June 11, 2026 at 11:09 am

    You forget to use the typename keyword.

    In GraphTraits<Map>::node_iterator, the compiler knows what specialization of GraphTraits is used, and can figure out that node_iterator is a typedef.

    In GraphTraits<T>::node_iterator, it has no idea what specialization will be used, because T isn’t known yet. So it assumes that node_iterator is a member variable, not a type. You have to correct this assumption. Say typename GraphTraits<T>::node_iterator

    To require every specialization of GraphTraits to provide a typedef, you’d need “concepts”, which were proposed for C++11 but had to be delayed until a future version. So it isn’t possible in C++ today. Whenever you actually USE printGraph (formally, during instantiation of each template function instance of the printGraph<T> function template), however, the compiler will double-check that GraphTraits<T>::node_iterator really is a type (since now it knows T and can perform overload resolution).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote these two classes that simple encrypt and decrypt strings: Encode.php class Encode
I have 2 classes that inherit from a common base class. Each of these
I wrote a class to represent vectors in Python (as an exercise) and I'm
How can I represent, in a class, something that represents a Value. This value
I wrote some classes that I use with many different projects. For example, I
I write hobby code from time to time. The thing is these tools, classes
When I write class templates, and need to fully-specialize members of those classes, Doxygen
I wrote these lines in My Application start event: var mongo = new Mongo();
I wrote these lines in my code: CFUUIDRef identifier = CFUUIDCreate(NULL); NSString *identifierString =
In serialization mechanism,we are wrote the object into stream using objectinputstream and object outputstream.These

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.