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 7162827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:47:58+00:00 2026-05-28T13:47:58+00:00

I have a defined a simple template class. In this class I define a

  • 0

I have a defined a simple template class. In this class I define a structure (struct NodeData) which is used for nodes of a graph. For the first code I give bellow, there is no compilation error even if I do an error on purpose in the method test (I do g[nId].anything = "something" even if struct NodeData dont have a variable called anything).

To understand where is the problem, in the second code I give bellow I have put my structs definitions and typedefs outside of MyClass. I have put template<typename T1, typename T2> on top of the definition of struct NodeData because this struct need to store 2 variables of abstract type T1 and T2. I also removed the keyword typename from the typedefs, and I have put NodeData<int, int> instead of NodeData in the first typedef (even if I don’t want to do that actually), otherways it will give some errors at this line like: expected a type, got 'NodeData'. When I compile, it gives the following expected error (which is totally normal actually): 'struct NodeData<int, int>' has no member named 'anything', while for the first code I didn’t get this error !

What is the difference between this two codes ? How can I do for the second code to not be obliged to specify NodeData for the first typedef (because members var1 and var2 of struct NodeData are not necessarily of type int) ? Or how can I do for the first code to work correctly and detect the error that NodeData has no member named anything ?

First code:

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

using namespace std;
using namespace boost;

template<typename T1, typename T2>
class MyClass
{
    public:
        MyClass();
        virtual ~MyClass();
        void test(T1 p, T2 s);

    protected:
        struct NodeData
        {
            T1 var1;
            T2 var2;
            int var3;
        };

        struct EdgeData
        {
            int var;
        };

        typedef adjacency_list<setS, setS, undirectedS, NodeData, EdgeData> Graph;
        typedef typename Graph::vertex_descriptor NodeDataID;
        typedef typename Graph::edge_descriptor EdgeDataID;
        typedef typename graph_traits<Graph>::vertex_iterator VertexIterator;

        Graph g;
};

template<typename T1, typename T2>
void MyClass<T1, T2>::test(T1 arg1, T2 arg2)
{
    NodeDataID nId = add_vertex(g);
    g[nId].anything = "but anything is not in struct NodeData !";
    g[nId].var1 = arg1;
    g[nId].var2 = arg2;
    g[nId].var3 = 55;
}

template<typename T1, typename T2>
MyClass<T1, T2>::MyClass()
{
    // ...
}

template<typename T1, typename T2>
MyClass<T1, T2>::~MyClass()
{
    // ...
}

Second code:

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

using namespace std;
using namespace boost;

template<typename T1, typename T2>
struct NodeData
{
    T1 var1;
    T2 var2;
    int var3;
};

struct EdgeData
{
    int var;
};

typedef adjacency_list<setS, setS, undirectedS, NodeData<int, int>, EdgeData> Graph;
typedef Graph::vertex_descriptor NodeDataID;
typedef Graph::edge_descriptor EdgeDataID;
typedef graph_traits<Graph>::vertex_iterator VertexIterator;

template<typename T1, typename T2>
class MyClass
{
    public:
        MyClass();
        virtual ~MyClass();
        void test(T1 p, T2 s);

    protected:
        Graph g;
};

template<typename T1, typename T2>
void MyClass<T1, T2>::test(T1 arg1, T2 arg2)
{
    NodeDataID nId = add_vertex(g);
    g[nId].anything = "but anything is not in struct NodeData !";
    g[nId].var1 = arg1;
    g[nId].var2 = arg2;
    g[nId].var3 = 55;
}

template<typename T1, typename T2>
MyClass<T1, T2>::MyClass()
{
    // ...
}

template<typename T1, typename T2>
MyClass<T1, T2>::~MyClass()
{
    // ...
}
  • 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-05-28T13:47:59+00:00Added an answer on May 28, 2026 at 1:47 pm

    If there is no instantiation of the template then there is no code generated so the compilation errors aren’t present. This is why it’s important to usually have some test code which makes a few likely instantiations of your templates and uses them to ensure you didn’t make any silly mistakes.

    Edit: Added an example using some of your code in case it wasn’t clear what I was saying.

    #include <iostream>
    
    template<typename T1, typename T2>
    class MyClass
    {
    public:
        void Test(T1 p, T2 s);
    
    protected:
        struct NodeData
        {
            T1 var1;
            T2 var2;
            int var3;
        };
    
    private:
        NodeData m_g;
    };
    
    template<typename T1, typename T2>
    void MyClass<T1, T2>::Test(T1 arg1, T2 arg2)
    {
        // error C2039: 'anything' is not a member of 'MyClass<T1,T2>::NodeData'
        m_g.anything = "but anything is not in struct NodeData !";
        m_g.var1 = arg1;
        m_g.var2 = arg2;
        m_g.var3 = 55;
    }
    
    int main() {
        // if you comment out the lines using it the template will never be compiled
        MyClass<int, double> test; // instantiation of template with T1 = int and T2 = double
        test.Test(42, 3.14); // calling Test function
    
        std::cin.get();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this very simple wrapper template: template<class T> struct wrapper { inline operator
I have a simple container : template <class nodeType> list { public: struct node
I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
I have defined a 'vertex' class which, when combined, form a graph. these vertices
I have a class defined like this: template< class TBoundingBox > class BoundingBoxPlaneCalculator {
I have a template class btree and have defined a struct Node in the
Well, the question is kinda simple. I have a object defined as: public class
I have a VERY simple 3D space defined in WPF, which defines a 3D
I have a class template where some methods are defined as virtual to give
I have a simple class as defined below: public class Person { int _id;

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.