gpt4 book ai didi

c++ - 动态数组错误

转载 作者:行者123 更新时间:2023-11-28 02:26:35 24 4
gpt4 key购买 nike

我正在尝试创建一个类,其中包含一个大小在运行时确定的数组。但是,当我尝试访问“addToSet”函数中的数组时,出现“未声明的标识符错误”。任何帮助,将不胜感激。我是 C++ 新手。

头文件:

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

public:
//Variables

int size;


//Constructor
ShortestPathSet(int numVertices);

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

};

类文件:

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

最佳答案

你在这里遗漏了类名:

void addToSet(int vertice, int distance)

你的意思是:

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

按原样,您正在声明和定义一个完全不相关的函数,并且在该函数的范围内没有这样的变量 pathSet - 因此没有声明标识符。

旁注,您可能不想使 size 成为公共(public)成员变量。

关于c++ - 动态数组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30429942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com