0

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;
}

1 Answer 1

2

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.

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

4 Comments

So, even when using "namespace std" i still have to include this? Also, thank you so much!!
@KarlTaht This has nothing to do with namespace std. In order to define a class member function outside of the class definition, you need to qualify it with the class name.
Should I not use a header file for the class then, to simplify things?
@KarlTaht No, just have to learn the correct syntax for writing your class definitions in a source file.

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.