0

This is my third time for creating a graph using adjacency list in c++. It's important to use OOP. I feel like the answer to this problem is really simple, but I can't manage to fix and improve my code.

There is it:

#include <iostream>
#include <algorithm>
#include <fstream> 
#include <vector>


using namespace std;
struct Edge
{
    int begin;
    int end;
};

class Graph
{
private:
    int numOfNodes;
    vector<vector<int>> baseVec;

public:
    Graph(int numOfNodes)
    {   

        //baseVec->resize(numOfNodes, vector<int>(numOfNodes));
        for (int i = 0; i < numOfNodes; i++)
        {
            vector<Edge> subVec;
            baseVec.emplace_back(subVec);
        }
    }

    void newEdge(Edge edge)
    {
        if (edge.begin >= numOfNodes && edge.end >= numOfNodes)
        {
            cout << "Invalid edge!\n";
        }
        baseVec[edge.begin].emplace_back(edge.end);
        baseVec[edge.end].emplace_back(edge.begin);
    }
    void display()
    {
        cout << baseVec.size();
        for (int i = 0; i < baseVec.size(); i++)
        {
            cout << "\n Adjacency list of vertex " << i << "\n head ";
            for (int j = 0; j < baseVec[i].size(); j++)
            {
                cout << baseVec[i][j];
                cout << endl;
            }
        }
    }

};

int main()
{
    int vertex, numberOfEdges, begin, end;
    cout << "Enter number of nodes: ";
    cin >> vertex;
    numberOfEdges = vertex * (vertex - 1);

    Edge edge;
    Graph g1(vertex);
    for (int i = 0; i < numberOfEdges; i++)
    {
        cout << "Enter edge ex.1 2 (-1 -1 to exit): \n";
        cin >> edge.begin >> edge.end;
        if ((begin == -1) && (end == -1))
        {
            break;
        }
        g1.newEdge(edge);
    }
    g1.display();
    return 0;
}

So now in Visual Studio I have an error:

'std::vector>::vector(const std::vector<_Ty,std::allocator<_Ty>> &)': cannot convert argument 1 from 'std::vector>' to 'const _Alloc &'

Also that in display() method there is signed/unsigned mismatch. I don't know If there is something wrong with my methods but I'm stuck here.

halfer
  • 19,824
  • 17
  • 99
  • 186
Top4o
  • 547
  • 6
  • 19
  • semi-offtopic: here: `{ vector subVec; baseVec.emplace_back(subVec); }` why are you using `emplace_back` ? `emplace_back` allows you to create a element in place instead of first creating it and then copying into the container. First creating the element and then calling `emplace_back` has it backwards. If you want to add n default constructed elements, you can simply call `baseVec.resize(baseVec.size()+n)` instead of writing the loop. – 463035818_is_not_an_ai Nov 15 '18 at 20:45
  • Yes, definitely your way is much better. I'm going to change that and several more things. But now I'm wondering how to display the edges... – Top4o Nov 15 '18 at 20:51
  • https://stackoverflow.com/questions/2981836/how-can-i-use-cout-myclass – 463035818_is_not_an_ai Nov 15 '18 at 20:52
  • So I think I overloaded the operator like that : std::ostream &operator<<(std::ostream &os, Edge const &m) { return os << m.begin << m.end; } But still I get the error: binary '<<': no operator found which takes a right-hand operand of type '_Ty' (or there is no acceptable conversion) . Probably I'm asking stupid questions, sorry about that. – Top4o Nov 15 '18 at 21:10
  • your have the out-of-bounds check wrong: `if (edge.begin >= numOfNodes && edge.end >= numOfNodes)` the last valid index is `numOfNodes-1` not `numOfNodes` – 463035818_is_not_an_ai Nov 15 '18 at 21:14
  • and you dont check the lower bounds. Passing user input and using it to index into the vector is dangerous – 463035818_is_not_an_ai Nov 15 '18 at 21:15
  • I chaged it to `if (edge.begin >= numOfNodes-1 || edge.end >= numOfNodes-1 || edge.begin < 0 || edge.end < 0)`, but still got the error message and this one **'<': signed/unsigned mismatch** on display() for loops. – Top4o Nov 15 '18 at 21:25
  • if you still have question open a new question. Sorry but comments are one of the worst places to discuss code. – 463035818_is_not_an_ai Nov 15 '18 at 21:26
  • I opened a new question, hope that finally, the code is going to work. – Top4o Nov 15 '18 at 21:44

1 Answers1

0
vector<vector<int>> baseVec;

accepts vector<int>s

vector<Edge> subVec;
baseVec.emplace_back(subVec);

Attempts to feed it vector<Edge>s. This does not make sense.

vector<vector<Edge>> baseVec;

Makes better sense.

Note that this change will set up another blast of errors when

cout << baseVec[i][j];

tries to print an edge and does not know how. Either make an operator<< overload to handle edge or change what you're outputting.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Thanks! That was helpful. You were right about cout errors, I'm going to try and fix them. – Top4o Nov 15 '18 at 20:22
  • Do you think that that code it's going to do the task of creating a Graph with edges? And if not what also should i change, I'm not sure about newEdge method. – Top4o Nov 15 '18 at 20:31
  • If you're not sure the code works, test it. Mind you, even if you're sure the code works you should test it just in case you get a nasty surprise. Ask a new question, if necessary, based on the results of your testing. – user4581301 Nov 15 '18 at 20:35