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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:44:01+00:00 2026-05-25T17:44:01+00:00

I am working on Graph Template Class. Here is what I written till now.

  • 0

I am working on Graph Template Class. Here is what I written till now.

#ifndef __GRAPH_H__
#define __GRAPH_H__

#include <map>
#include <list>

template <typename _Ty>
class Graph {
private:
    template <typename _Ty>
    class Vertex {
    public:
        Vertex(_Ty in) : m_Label(in) {
        }
        ~Vertex() {
        }
    private:
        _Ty m_Label;
    protected:
    };

public:
    typedef Vertex<_Ty>                       VertexType;
    typedef std::list<VertexType>             AdjListType;
    typedef std::map<VertexType,AdjListType>  GraphType;

public:
    Graph(bool bType = false) : m_Type(bType) {
    }
    ~Graph() {
    }
    void AddEdge(VertexType vLevt, VertexType vRight) {
    }
private:
    // true if bidirectional
    // false if unidirectional.
    bool m_Type; 
    GraphType m_Graph;
protected:
};

#endif

Here is how I am using this class.

#include "Graph.h"
#include <string>

int main(int argc, char **argv) {
    Graph<int> myGraph;
    myGraph.AddEdge(1,2);

    Graph<char *> myGraph2;
    myGraph2.AddEdge("A","B");

    Graph<std::string> myGraph3;
    myGraph3.AddEdge("A","B");

}

myGraph3 is giving me compilation error.
error C2664: 'Graph<_Ty>::AddEdge' : cannot convert parameter 1 from 'const char [2]' to 'Graph<_Ty>::Vertex<_Ty>'

Why this is error , if std::string test = "ABC"; works.

  • 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-25T17:44:02+00:00Added an answer on May 25, 2026 at 5:44 pm

    std::string test = "ABC"; does implicit casting, but it is not happening while calling the function. Try myGraph3.AddEdge(std::string("A"),std::string("B"));.

    Function call overloading by defining another function as in

    void AddEdge(_Ty vLevt, _Ty vRight) {
            this->AddEdge((VertexType) vLevt, (VertexType) vRight);
        }
    

    helps.

    The other issue with your code (at least for gcc) is that you are using the same parameter _Ty in two nested template declarations. The complete, correct code, that works for me is:

    #include <map>
    #include <list>
    
    template <typename _Ty>
    class Graph {
    private:
        template <typename _Tyv>
        class Vertex {
        public:
            Vertex(_Tyv in) : m_Label(in) {
            }
            ~Vertex() {
            }
        private:
            _Tyv m_Label;
        protected:
        };
    
    public:
        typedef Vertex<_Ty>                       VertexType;
        typedef std::list<VertexType>             AdjListType;
        typedef std::map<VertexType,AdjListType>  GraphType;
    
    public:
        Graph(bool bType = false) : m_Type(bType) {
        }
        ~Graph() {
        }
        void AddEdge(VertexType vLevt, VertexType vRight) {
        }
        void AddEdge(_Ty vLevt, _Ty vRight) {
            this->AddEdge((VertexType) vLevt, (VertexType) vRight);
        }
    private:
        // true if bidirectional
        // false if unidirectional.
        bool m_Type;
        GraphType m_Graph;
    protected:
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on Graphs and I created a Graph class and added #include
I'm working in a proyect based on Netron (A graph editing library written in
Short story, I'm implementing a graph and now I'm working on the Kruskal, I
I am working on a graph in WPF, in each section there are thousands
Just as the title states, I'm looking to display a blank template graph (by
I'm working on a site that requires me to display a graph of the
Im working on rendering a graph in app. Working with the data (drawing the
I am working on to create a undirected graph with force layout. In addition,
Am working on a program that will allow a graph of nodes to be
I'm working on a program that's supposed to represent a graph. My issue is

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.