Karl Taht
Karl Taht

Reputation: 125

Error with Dynamic Array

I am trying to create a class with an array with a size that is determined at runtime. However, when I try to access the array in the "addToSet" function, I get an "undeclared identifier error". Any help would be appreciated. I am new to C++.

Header File:

class ShortestPathSet
{
private:
    //Variables
    int *pathSet;

public:
    //Variables

    int size;


    //Constructor
    ShortestPathSet(int numVertices);

    //Functions
    void addToSet(int vertice, int distance);

};

Class File:

#include "ShortestPathSet.h"

using namespace std;

ShortestPathSet::ShortestPathSet(int numVertices)   
{
    size = numVertices;
    pathSet = new int[numVertices];
}

void addToSet(int vertice, int distance)
{
    pathSet[vertice] = distance;
}

Upvotes: 0

Views: 169

Answers (1)

Barry
Barry

Reputation: 302738

You're missing the class name here:

void addToSet(int vertice, int distance)

You meant:

void ShortestPathSet::addToSet(int vertice, int distance)
     ^^^^^^^^^^^^^^^^^

As-is, you're declaring and defining a completely unrelated function, and within the scope of that function there is no such variable pathSet - hence undeclared identifier.

Side-note, you probably don't want to make size a public member variable.

Upvotes: 2

Related Questions