0

Code is given below :

#include <cstdio>
#include <iostream>
#include <deque>

struct vertex {
    bool visited;
    int value;
    int distance;
};

int main() {

    int key_value;
    std::cin >> key_value;

    int num_vertices;
    std::cin >> num_vertices;

    int** matrix;
    matrix = new int*[num_vertices];
    for (int i = 0; i < num_vertices; i++) {
        matrix[i] = new int[num_vertices];
    }

    vertex* vertices;
    vertices = new vertex[num_vertices];

    std::deque<vertex> queue;

    int vertex, value;
    while(std::cin >> vertex >> value) {
        vertices[vertex].value = value;
        int num_edges;
        std::cin >> num_edges;
        for(int i = 0; i < num_edges; i++) {
            int edge_to;
            std::cin >> edge_to;
            matrix[vertex][edge_to] = 1;
        }
    }

    //BFS

    vertices[0].visited = true;
    vertices[0].distance = 0;
    queue.push_back(vertices[0]);

    while(!queue.empty()) {
        vertex cur_v;
        cur_v = queue.front();
        queue.pop_front();
        for(int i = 1; i < num_vertices; i++) {
            if(matrix[cur_v, i]) {
                if(!vertices[i].visited) {
                    vertices[i].visited = true;
                    queue.push_back(vertices[i]);
                }
            }
        }
    }

    return(0);
}

I am getting the following errors,

search_gilene_matt.cc: In function ‘int main()’: search_gilene_matt.cc:42: error: expected ‘;’ before ‘cur_v’ search_gilene_matt.cc:43: error: ‘cur_v’ was not declared in this scope

Can someone shed some light on what would be causing this?

I have looked for a missing semicolon but I don't have any idea of where it would be.

13
  • 1
    Does the code compile and run correctly? IntelliSense errors may be a bug in the IDE, they are different to program errors Commented Apr 17, 2015 at 4:24
  • 1
    And when you get compiler errors, what are those? When having compiler errors, always please include them, complete and unedited, in the question. Also mark out on which line in the shown code the errors are. Commented Apr 17, 2015 at 4:26
  • 4
    How about providing an SSCCE instead of a bunch of dots? Commented Apr 17, 2015 at 4:27
  • 2
    cur_v, i shouldn't use the comma operator... you want ][. Separately, compilers tend to do something really helpful - tell you the line number the error's on. How about putting a big // ERROR HERE! in your code listing...? Commented Apr 17, 2015 at 4:33
  • 4
    int vertex, value; shadows the type vertex Commented Apr 17, 2015 at 4:33

2 Answers 2

5

Looks like you declared int vertex which is hiding your struct vertex. Rename the int and you're good.

Sign up to request clarification or add additional context in comments.

Comments

3

When you write:

int vertex, value;

then vertex becomes the name of a variable so long as that variable is in scope.

Later you write:

vertex cur_v;

which fails because vertex is the name of a variable.

To fix this, either name your variable something different, or use struct vertex to refer to the type.

1 Comment

This answer is clear, explains the problem and the solution well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.