gpt4 book ai didi

C++类定义编译问题

转载 作者:太空宇宙 更新时间:2023-11-03 10:21:01 25 4
gpt4 key购买 nike

我找遍了所有地方,但找不到与我的问题相关的任何内容。我正在尝试为一个多边形类编写一个类定义,该多边形类基本上具有一个包含指向某个点的指针的 vector 。当我尝试编译时,我不断收到以下错误...

错误 C2143:语法错误:缺少“;”在“<”之前错误 C4430:缺少类型说明符 - 假定为 int。错误 C2238:“;”之前的意外标记错误 C2061:语法错误:标识符“vector ”错误 C2065:“myPolygonPoints”:未声明的标识符错误 C2065:“点”:未声明的标识符错误 C2065:“myHasInstersection”:未声明的标识符错误 C2660:“Polygon::addSetOfPoints”:函数不接受 1 个参数

这是类的代码

#include "Point.h"
#include <vector>

class Point;

class Polygon
{
private:
vector<Point*> myPolygonPoints;
bool myHasIntersection;

public:
void addSetOfPoints(vector<Point*> points)
{
myPolygonPoints = points;
}

bool getHasIntersection()
{
return myHasIntersection;
}

void setHasIntersection(bool intersection)
{
myHasInstersection = intersection;
}

};

最佳答案

您正在使用 std 命名空间中的 vector 而未对其进行限定。

您要么必须using namespace std;,要么using std::vector,或者用std 命名空间,如 std::vector

#include "Point.h"
#include <vector>

class Point; // Assuming Point.h has this declared,
// you don't need this forward declaration, but in reality,
// you don't need to include Point.h
// since you're only declaring pointers to Points

class Polygon
{
private:
std::vector<Point*> myPolygonPoints;
bool myHasIntersection;

public:
void addSetOfPoints(std::vector<Point*> points)
{
myPolygonPoints = points;
}

bool getHasIntersection()
{
return myHasIntersection;
}

void setHasIntersection(bool intersection)
{
myHasInstersection = intersection;
}

};

关于C++类定义编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5130790/

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