0

I have a vertex class that has an id and adjacency list as private members. The adjacency list is stored as a map. When I instantiate an object of this class I want to create an empty map. I am switching over from python to C++ and its proving harder than I thought. Here's the code for the class:

#include "vertex.h"

class Vertex {
    char _id;
    std::map<char, int> _adjList;

public:
    void addNeighbor(Vertex, char neighbor, int weight);
    std::vector<char> getConnections(Vertex);
    char getId();
    int getWeight(Vertex, char neighbor);
    Vertex(char);
};

Vertex::Vertex(char id){
    std::map<char, int> adjList;
    _adjList = adjList;
    _id = id;

}

void Vertex::addNeighbor(Vertex v, char neighbor, int weight){
    v._adjList.insert(std::map<char, int>::value_type(neighbor, weight));
}

std::vector<char> Vertex::getConnections(Vertex v){
    std::vector<char> ids;
    for(std::map<char,int>::iterator it = v._adjList.begin(); it != v._adjList.end(); ++it) {
        ids.push_back(it->first);
    };
    return ids;
}

char Vertex::getId(){
    return _id;
}

int Vertex::getWeight(Vertex v, char neighbor){
    return v._adjList[neighbor];
}

Now when I instantiate this in main

Vertex V('a');

The compiler throws an error that the variable has incomplete type Vertex. Any help is greatly appreciated. Basically I want to construct an object with an id and empty map that will hold the id and path weight to the adjacent node. I am doing this for learning purposes

My vertex.h has:

#ifndef __graphs__vertex__
#define __graphs__vertex__

#include <stdio.h>
#include <map>
#include <vector>

class Vertex;

#endif

and then in my main I include vertex.h

Praetorian
  • 106,671
  • 19
  • 240
  • 328
Fizi
  • 1,749
  • 4
  • 29
  • 55
  • What does your source file with `main` in it include? – jxh Aug 05 '15 at 00:52
  • #include and #include "vertex.h" – Fizi Aug 05 '15 at 00:52
  • 1
    What has this question to do with `std::map`? – Kerrek SB Aug 05 '15 at 00:53
  • 2
    "incomplete type" means that the compiler has not seen the definition of `Vertex`. How do you have things divided into header and source? – aschepler Aug 05 '15 at 00:53
  • Did you include `` and ``? – yizzlez Aug 05 '15 at 00:53
  • `std::map adjList; _adjList = adjList;` is unnecessary, the `map` will be default constructed in any case. `vertex.h` needs to contain the definition of the `Vertex` class, not just a declaration, otherwise how is the compiler supposed to instantiate it within `main`? Please consider reading a [book](https://stackoverflow.com/q/388242/241631). – Praetorian Aug 05 '15 at 00:55
  • Please read a basic book; or just *any* source code of a real C++ project, to learn how to divide code into files. – Kerrek SB Aug 05 '15 at 00:56
  • @KerrekSB This is part of my learning process. I am reading a book and coding alongside it which helps me learn better. A site like this helps people like me to seek help if I am stuck. – Fizi Aug 05 '15 at 00:58
  • @Fizi: No, not really. Books, tutorials, lecture notes and reading existing code help people like you. This site helps people who can *not* be helped easily by means of existing literature, search engines or examples, and who need human intervention. – Kerrek SB Aug 05 '15 at 01:00
  • @KerrekSB I get your point. To be honest I have looked at the tutorials online and reading essential C++ but Included the definition in the cpp file and my line of thinking didn't really see it as the problem. Only then I asked the question. But I'll refrain from asking stupid questions in future – Fizi Aug 05 '15 at 01:03
  • http://stackoverflow.com/questions/1410563/what-is-the-difference-between-a-definition-and-a-declaration/1410632#1410632 – jxh Aug 05 '15 at 01:05
  • http://stackoverflow.com/questions/4757565/c-forward-declaration/4757718?s=12|0.3230#4757718 – jxh Aug 05 '15 at 01:06
  • Feel free to ask stupid questions. Just take care not to ask questions, stupid or otherwise, that are already answered at length. Odds are you'll have to ask a few anyway simply because you can't google up an answer if you don't know the the right terms. – user4581301 Aug 05 '15 at 01:15

1 Answers1

1

The definition of Vertex should be in header, not in cpp:

I fixed declaration of method to add const and remove parameter Vertax as you should use this value.

#ifndef graphs_vertex_h
#define graphs_vertex_h

#include <map>
#include <vector>

class Vertex {
    char _id;
    std::map<char, int> _adjList;

public:
    explicit Vertex(char);

    void addNeighbor(char neighbor, int weight);
    std::vector<char> getConnections();
    char getId() const;
    int getWeight(char neighbor) const;
};

#endif
Jarod42
  • 203,559
  • 14
  • 181
  • 302