0

I am fairly new to C++ so if this is a silly question I apologize.

I am trying to create a graph algorithm and I have been stuck on a problem for quite sometime.

I am trying to set my head pointer (adjacent) to a new dynamically node (of type ListNode).

I am confused because when dynamically allocating memory for each vertex I dont have a problem. But when trying to access them individually through a subscript I am receiving errors.

Please see my code and errors below:

#include "stdafx.h"
#include <iostream>

//node for the linked list
struct ListNode
{
    int vertexNumber;
    ListNode *next;
};

struct Graph
{
    int vertex;
    int edge;
    ListNode *adjacent; //array of head pointers to linked list
};

Graph *adjListOfGraph();

int main()
{
    return 0;
}

Graph *adjListOfGraph()
{
    int i, x, y;
    ListNode *temp;

    //graph created
    Graph *g = new Graph; 
    if (!g)
    {
        std::cout << "Memory Error" << std::endl;
        return 0;
    }

    std::cout << "Enter number of vertices: " << std::endl;
    std::cin >> g->vertex;
    std::cout << "Enter number of edges: " << std::endl;
    std::cin >> g->edge;
    //dynamically allocate memory for each vertex
    g->adjacent = new ListNode[g->vertex]; 


    for (int i = 0; i < g->vertex; i++)
    {
        g->adjacent[i] = new ListNode; //error; no operator "=" matches these operands
        g->adjacent[i].vertexNumber = i;
        g->adjacent[i].next = g->adjacent[i]; //error; no suitable conversation 
    }

I know there are better ways to great a graph algorithm and I plan to create one through smart pointers and vectors after. But I am trying to follow a book (which is written in C) and I am having trouble duplicating the code.

Any help or advice would be much appreciated.

2501
  • 25,460
  • 4
  • 47
  • 87
JoeG
  • 512
  • 8
  • 19
  • If it helps, there's more to the error. *no known conversion from 'ListNode *' to 'const ListNode'*. Any time you get an error, you should include the full error text in the question. It won't look exactly like mine because it's a different compiler, but I know that isn't the complete message. – chris Feb 18 '17 at 15:22
  • why are you trying to learn C++ with a C book? Please, please, please dont do this. While most of the C example may compile on a C++ compiler, it still is not C++ – 463035818_is_not_an_ai Feb 18 '17 at 15:26
  • @chris thank you for the advice. – JoeG Feb 19 '17 at 02:07
  • @tobi303 I went through a online tutorial and have been using _C++ Primer_ to learn C++ so far. But the Data Structures and Algorithms book I am using is written in C. I am having trouble converting this particular algorithm. If you have any suggestions for good books in the topic Data Structures and Algorithms whose code is written in C++ they would be much appreciated. – JoeG Feb 19 '17 at 02:14
  • [Here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) is a list of books. not sure if there is something on data structures and algorithms, but especially when it is about data structures and algorithms there is a huge difference between C and C++ – 463035818_is_not_an_ai Feb 19 '17 at 18:45

0 Answers0