动态数组错误
Error with Dynamic Array
我正在尝试创建一个 class,其中包含一个大小在运行时确定的数组。但是,当我尝试访问 "addToSet" 函数中的数组时,我得到一个 "undeclared identifier error"。任何帮助,将不胜感激。我是 C++ 新手。
头文件:
class ShortestPathSet
{
private:
//Variables
int *pathSet;
public:
//Variables
int size;
//Constructor
ShortestPathSet(int numVertices);
//Functions
void addToSet(int vertice, int distance);
};
Class 文件:
#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;
}
您在这里遗漏了 class 名称:
void addToSet(int vertice, int distance)
你的意思是:
void ShortestPathSet::addToSet(int vertice, int distance)
^^^^^^^^^^^^^^^^^
按原样,您正在声明和定义一个完全不相关的函数,并且在该函数的范围内没有这样的变量 pathSet
- 因此未声明标识符。
旁注,您可能不想让 size
成为 public 成员变量。
我正在尝试创建一个 class,其中包含一个大小在运行时确定的数组。但是,当我尝试访问 "addToSet" 函数中的数组时,我得到一个 "undeclared identifier error"。任何帮助,将不胜感激。我是 C++ 新手。
头文件:
class ShortestPathSet
{
private:
//Variables
int *pathSet;
public:
//Variables
int size;
//Constructor
ShortestPathSet(int numVertices);
//Functions
void addToSet(int vertice, int distance);
};
Class 文件:
#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;
}
您在这里遗漏了 class 名称:
void addToSet(int vertice, int distance)
你的意思是:
void ShortestPathSet::addToSet(int vertice, int distance)
^^^^^^^^^^^^^^^^^
按原样,您正在声明和定义一个完全不相关的函数,并且在该函数的范围内没有这样的变量 pathSet
- 因此未声明标识符。
旁注,您可能不想让 size
成为 public 成员变量。