Forward declaration of class and still Error: Variable has incomplete type

Forward declaration of class and still Error: Variable has incomplete type

我定义了两个 classes :(1) class Point_CCS_xy 和 (2) random_Point_CCS_xy_generator。我想在 Point_CCS_xy class 中定义一个函数,它利用 random_Point_CCS_xy_generator class 在 Min 范围内的笛卡尔坐标系上生成随机点,并且Max,两者都被定义为long double。我已经 forward declared class random_Point_CCS_xy_generator,但是我在这一行 random_Point_CCS_xy_generator rpg(Min, Max) 上收到 Variable has incomplete type random_Point_CCS_xy_generator 错误;还有这个错误 Member reference base type 'vector<Point_CCS_xy> (size_t)' (aka 'vector<Point_CCS_xy> (unsigned long long)') is not a structure or union,在这一行 Point_CCS_xy_vec.push_back(p);.

MWE

#include<iostream>
#include<fstream>
#include<functional>
#include<algorithm>
#include<vector>
#include<deque>
#include<queue>
#include<set>
#include<list>
#include<limits>
#include<string>
#include<memory>

using namespace std;
typedef long double ld;

class random_Point_CCS_xy_generator;

class Point_CCS_xy{
private:
    long double x_coordinate_ {0.0};
    long double y_coordinate_ {0.0};
public:
    Point_CCS_xy () = default;
    Point_CCS_xy(long double x, long double y): x_coordinate_(x), y_coordinate_(y) {}
    ~Point_CCS_xy() = default;

    // copy constructor through construction delegation method
    Point_CCS_xy(const Point_CCS_xy& cpyObj) : Point_CCS_xy(cpyObj.x_coordinate_, cpyObj.y_coordinate_)
    {

    }



 // Copy and Mover Assignment Operators, 
 // also setters and getters, and a few utility functions come here inlcuding:
friend ostream& operator<<(ostream& os, const Point_CCS_xy& pnt);


// Problematic Function -> source of ERRORS
vector<Point_CCS_xy> Random_Point_CCS_xy(long double Min, long double Max, size_t n) {
        random_Point_CCS_xy_generator rpg(Min, Max);
        vector<Point_CCS_xy> Point_CCS_xy_vec(size_t);
        for (size_t i = 0; i < n; ++i){
            Point_CCS_xy p = Point_CCS_xy(rpg());
            Point_CCS_xy_vec.push_back(p);
        }
        return Point_CCS_xy_vec;
    }
};

ostream& operator<<(ostream& os, const Point_CCS_xy& pnt) {
    os << "X: " << setw(10)<< left << pnt.x_coordinate_ << " Y: " << setw(10)<< left << pnt.y_coordinate_;
    return os;
}

class random_Point_CCS_xy_generator {
public:
    random_Point_CCS_xy_generator(long double min, long double max)
            : engine_(std::random_device()()), distribution_(min, max) {}

    Point_CCS_xy operator()() {
        long double x = distribution_(engine_);
        long double y = distribution_(engine_);
        return Point_CCS_xy(x, y);
    }

    std::mt19937 engine_;
    std::uniform_real_distribution<double> distribution_;
};

由于我是编程新手,任何想法将不胜感激。

您必须先完成 class random_Point_CCS_xy_generator 的定义,然后才能在实际函数定义中使用它。可能您必须将这些方法移出 .h 文件并移至实现文件中。

类型必须在您实际使用它时完成(例如构造一个值)。前向声明只会帮助您指定指针或引用类型,而不是值类型。

这里的主要问题是您在 class 定义中内联定义函数。取而代之的是,将它们移出:

class Point_CCS_xy {
private:
    long double x_coordinate_{ 0.0 };
    long double y_coordinate_{ 0.0 };
public:
    Point_CCS_xy() = default;
    Point_CCS_xy(long double x, long double y) : x_coordinate_(x), y_coordinate_(y) {}
    ~Point_CCS_xy() = default;

    Point_CCS_xy(const Point_CCS_xy& cpyObj);
    vector<Point_CCS_xy> Random_Point_CCS_xy(long double Min, long double Max, size_t n);

    friend ostream& operator<<(ostream& os, const Point_CCS_xy& pnt);
};

class random_Point_CCS_xy_generator {
public:
    random_Point_CCS_xy_generator(long double min, long double max)
        : engine_(std::random_device()()), distribution_(min, max) {}

    Point_CCS_xy operator()() {
        long double x = distribution_(engine_);
        long double y = distribution_(engine_);
        return Point_CCS_xy(x, y);
    }

    std::mt19937 engine_;
    std::uniform_real_distribution<double> distribution_;
};

那么你可以定义后面的函数:

Point_CCS_xy::Point_CCS_xy(const Point_CCS_xy& cpyObj)
    : Point_CCS_xy(cpyObj.x_coordinate_, cpyObj.y_coordinate_)
{
}

vector<Point_CCS_xy> Point_CCS_xy::Random_Point_CCS_xy(long double Min, long double Max, size_t n)
{
    random_Point_CCS_xy_generator rpg(Min, Max);
    vector<Point_CCS_xy> Point_CCS_xy_vec;
    Point_CCS_xy_vec.reserve(n);
    for (size_t i = 0; i < n; ++i) {
        Point_CCS_xy p = Point_CCS_xy(rpg());
        Point_CCS_xy_vec.push_back(p);
    }
    return Point_CCS_xy_vec;
}

ostream& operator<<(ostream& os, const Point_CCS_xy& pnt)
{
    os << "X: " << setw(10) << left << pnt.x_coordinate_ << " Y: " << setw(10) << left << pnt.y_coordinate_;
    return os;
}

请注意,我修复了另一个错误,您使用 size_t 而不是 n 作为向量构造函数的参数。正如评论中所指出的那样,由于您填充向量的方式,这实际上是不正确的。相反,我将其更改为在推送元素之前保留所需的大小。

此外,您还遗漏了一些 headers 并使用了大量其他内容。您真正需要的是:

#include <iostream>
#include <iomanip>
#include <vector>
#include <random>