C++类 - 如何从函数返回自定义类型的向量

C++ classes - how return vector of custom type from a function?

本文关键字:自定义 返回 类型 向量 函数 C++      更新时间:2023-10-16

当我想让函数返回我刚刚定义的类型 struct 的向量时,我在类中设置函数时遇到问题。编译器给出"使用未声明的标识符"错误。

在 .h 文件中:(未给出错误)

struct workingPoint;
public:
vector<workingPoint>calculateWorkingPointCloud();

在.cpp文件中:

struct DeltaKinematics::workingPoint {
    int x, y, z;
    //more stuff to come
};
vector<workingPoint> DeltaKinematics::calculateWorkingPointCloud(){ //error here is "Use of undeclared identifier 'workingPoint'
}

编译器似乎不知道workingPoint是什么,尽管它是在函数之前声明的?

这只是查找的问题。您需要完全限定名称,例如
vector<DeltaKinematics::workingPoint> DeltaKinematics::calculateWorkingPointCloud(){...

我在这里问了一个关于这个问题的类似问题。也许这对你来说也很有趣。

您定义了一个结构DeltaKinematics::workingPoint,然后尝试返回一个结构workingPoint。您需要明确的限定条件。