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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:13:39+00:00 2026-05-21T05:13:39+00:00

I have a vector of type template class and I am trying to print

  • 0

I have a vector of type template class and I am trying to print it, but getting a weird error.

Here is my class:

template <typename VertexType, typename EdgeType> class Vertex{
private:
    typedef std::vector<std::pair<int, EdgeType> > VertexList;
    std::vector<Vertex<VertexType, EdgeType>> Vertice;

public:
    void Add(Vertex);
};

Add Method and Print Statement:

template <typename VertexType, typename EdgeType> void Vertex<VertexType, EdgeType> ::Add(Vertex v)
{
    int count = 5;
    //std::vector<string>temp;

    for(int i=0; i<count; i++)
    Vertice.push_back(v);

    for(int i=0; i<Vertice.size(); i++)
        cout<< Vertice[i] <<endl;
}

Main() Method:

int main()
{
    Vertex<std::string, std::string> v1;

    v1.Add(v1);

    std::getchar();
}

Error am getting is:

error C2679: binary ‘<<‘ : no operator
found which takes a right-hand operand
of type ‘Vertex’
(or there is no acceptable conversion)

  • 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-21T05:13:40+00:00Added an answer on May 21, 2026 at 5:13 am

    You aren’t defining an operator << anywhere. You should define it like this out of your class :

    template <typename VertexType, typename EdgeType>
    std::ostream& operator << (std::ostream& out, const Vertex<VertexType,EdgeType>& v);
    // implementation
    template <typename VertexType, typename EdgeType>
    std::ostream& operator << (std::ostream& out, const Vertex<VertexType,EdgeType>& v) {
        // print whatever you want that represents your vertex
    
        // please don't forget to return this reference.
        return out;
    }
    

    Also, having a class with a vector of instances of it inside is a call for trouble. Remember that “vector<Vertice<VertexType,EdgeType> >” is an array of instances, not an array of references. If you want a array of ‘references’ to Vertex, use an array of pointers.

    And consider using boost’s graph library instead of redefining yet another one and coming to all pitfall associated with graphs (like memory management for instance). The boost library also have some useful algorithms that you could want to use..

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

Sidebar

Related Questions

trying to make a template, but I have an error in gcc4 but not
I have a template class template<typename T, typename R> . R is of type
I'm writing a simple maths library with a template vector type: template<typename T, size_t
I have a template class that I have some specializations for. But the next
I have a template base class like this: template<typename T, std::size_t Size> class VectorT
Given: template<typename T> class A { B b; std::vector<T> vec1; std::vector<T> vec2; } I'd
I keep getting the following error when trying to write a template function: main.cpp|17|error:
I'm trying to inherit from a template class, using a type defined in the
I currently have a vector using my template as its type: vector<hashData> myTable; hashData
I have a vector with pointers of type Vehicle. Vehicle is the base class

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.