it's my first question here so I apologize for eventual formal mistakes you may found in my post.
I'm coding a simple class for "Undirected Connected Weighted Graphs", which must use Adjacency Lists based on vectors.
The issue is that when I run the program from Eclipse, MS Windows says it "stops working" and after debugging I get an "Unhandled exception at 0x00AE251A .... Access violation writing location..." message. Looking around I found that this issue may be caused by a missing pointer destruction or pointer initialization (?). I switched from the standard pointer to the shared_ptr to troubleshoot this issue but the error is the same...
Can anybody enlighten me on this? I lost almost a whole day trying to find the cause without success.
class UndirectedGraph
{
private:
int V;
std::vector<std::shared_ptr<std::pair<int,int>>>* adj;
public:
UndirectedGraph(int V)
{
this->V = V;
this->adj = new std::vector<std::shared_ptr<std::pair<int,int>>>;
}
void addEdge(int v, int w, int weight)
{
auto sp = std::make_shared<std::pair<int,int>>(std::make_pair(v,weight));
adj[v].push_back(sp);
}
int main()
{
UndirectedGraph G1(7);//Ok
G1.addEdge(0,1,9);//Ok
G1.addEdge(1,2,5);//Ok
G1.addEdge(2,0,8);//EXCEPTION RAISED HERE (if line is commented all run fine)
return 0;
}