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

  • Home
  • SEARCH
  • 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 6815199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:46:43+00:00 2026-05-26T20:46:43+00:00

I declared a template for a class and I am trying to use that

  • 0

I declared a template for a class and I am trying to use that class as a vector in the subsequent class.
Here is my code:

#include <iostream>
#include <vector>
#include <utility>

using namespace std;

template <typename L>

class AdjacencyListVertex {

public:
    L data;

    vector<int> edges;

    AdjacencyListVertex() {
        data = -1;  
    }

    AdjacencyListVertex(L _data) {
        data = _data;
    }

    void add_edge(int other) {
        edges.push_back(other) ;
    }

    void print(int i) {
        cout << "  vertex[" << i << "] '" << data << "' connected to:";
        for (int j = 0; j < edges.size(); j++) {
            cout << " " << edges.at(j);
        }
        cout << endl;
    }
};

template <typename L>
class AdjacencyListGraph {
  public:
      vector<typedef AdjacencyListVertex*> vertices;

    AdjacencyListGraph(int size) {
        vertices = vector<typedef AdjacencyListVertex*> (size, AdjacencyListVertex<L>());
    }

    void add_vertex(int position, L data) {
        vertices.at(position) = AdjacencyListVertex<L>(data);
    }


    void add_edge(int vertex1, int vertex2) {
        vertices.at(vertex1).add_edge(vertex2);
    }

    void print() {
        cout << "AdjacencyListGraph:" << endl;
        for (int i = 0; i < vertices.size(); i++) {
            if (vertices.at(i).data != -1) vertices.at(i).print(i);
        }
    }
};



int main(int argc, char** argv) {


    AdjacencyListGraph<string> alg_str = AdjacencyListGraph<string>(5);

    alg_str.add_vertex(0, "alg_str vertex 0");
    alg_str.add_vertex(1, "alg_str vertex 1");
    alg_str.add_vertex(2, "alg_str vertex 2");
    alg_str.add_vertex(3, "alg_str vertex 3");
    alg_str.add_vertex(4, "alg_str vertex 4");

    alg_str.add_edge(0, 1);
    alg_str.add_edge(1, 0);
    alg_str.add_edge(1, 3);
    alg_str.add_edge(3, 4);


}

I keep getting the errors:

graphs.cpp:38: error: template argument 1 is invalid
graphs.cpp:38: error: template argument 2 is invalid
graphs.cpp: In constructor ‘AdjacencyListGraph<L>::AdjacencyListGraph(int)’:
graphs.cpp:41: error: template argument 1 is invalid
graphs.cpp:41: error: template argument 2 is invalid
graphs.cpp: In member function ‘void AdjacencyListGraph<L>::add_vertex(int, L)’:
graphs.cpp:48: error: request for member ‘at’ in ‘((AdjacencyListGraph<L>*)this)-    >AdjacencyListGraph<L>::vertices’, which is of non-class type ‘int’
graphs.cpp: In member function ‘void AdjacencyListGraph<L>::add_edge(int, int)’:
graphs.cpp:55: error: request for member ‘at’ in ‘((AdjacencyListGraph<L>*)this)->AdjacencyListGraph<L>::vertices’, which is of non-class type ‘int’
graphs.cpp: In member function ‘void AdjacencyListGraph<L>::print()’:

Its an error in the line:
vector vertices;

What mistake am I doing in the definition of the vector?

  • 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-26T20:46:44+00:00Added an answer on May 26, 2026 at 8:46 pm

    You got the syntax wrong. Were you just guessing some keywords?

    You need something like this:

    std::vector<AdjacencyListVertex<L>*> vertices;
    

    Maybe what you meant was to typedef the list type:

    typedef AdjacencyListVertex<L> ListType;
    
    std::vector<ListType*> vertices;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; template <typename T> class test { T y; public:
I'm trying to make the following bit of code to work... #include <list> template
I'm trying to use inheritance among classes defined inside a class template (inner classes).
I'm trying to encapsulate the C++ Standard Library's vector class using templates but I
I have a template class where I want to use objects of that class
#include <iostream> #include <string> using namespace std; void printstr( const string & s )
I'm trying to use the C++ standard library's find algorithm like this: template<class T>
I am trying to use template meta-programming to determine the base class. Is there
I have a problem in calling a template class I have. I declared a
I declared a private variable vector<SomeClass> theVector; someplace inside my SomeClass class. Why can't

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.