C++类定义编译问题

发布于 2024-10-19 12:59:56 字数 837 浏览 3 评论 0原文

我已经查看了所有内容,但找不到与我的问题相关的任何内容。我试图为多边形类编写一个类定义,该多边形类基本上有一个保存指向点的指针的向量。当我尝试编译时,我不断收到以下错误...

错误 C2143:语法错误:缺少 ';'在“<”之前 错误 C4430:缺少类型说明符 - 假定为 int。 错误 C2238:“;”之前出​​现意外标记 错误 C2061:语法错误:标识符“向量” 错误 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;
    }

};

Ive looked all over and I cant find anything relating to my problem. Im trying to write a class definition for a polygon class that basicly has a vector that holds pointers to a point. When I try to compile i keep geting the folllowing errors...

error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed.
error C2238: unexpected token(s) preceding ';'
error C2061: syntax error : identifier 'vector'
error C2065: 'myPolygonPoints' : undeclared identifier
error C2065: 'points' : undeclared identifier
error C2065: 'myHasInstersection' : undeclared identifier
error C2660: 'Polygon::addSetOfPoints' : function does not take 1 arguments

Here is the code to the class

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

};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

意中人 2024-10-26 12:59:57

除了 std::vector 问题之外,您还错误地拼写了 setHasIntersection 方法中的 myHasIntersection 变量。

Besides the std::vector problem, you have also mispelled the myHasIntersection variable in the setHasIntersection method.

我一向站在原地 2024-10-26 12:59:56

您正在使用 std 命名空间中的 vector,但没有对其进行限定。

您要么必须using namespace std;,要么using std::vector,或者使用std声明所有vector对象 命名空间,例如 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;
    }

};

You are using vector from the std namespace without qualifying it.

You either have to do using namespace std;, or using std::vector, or declaring all your vector objects with the std namespace like 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;
    }

};
拒绝两难 2024-10-26 12:59:56

矢量位于 std:: 命名空间中。所以向量在示例代码中未定义

两种可能的解决方案:

#include <vector>
using std::vector;

或:(在所有情况下通过引用向量的代码,声明和引用)

private:
  std::vector<Point*> myPolygonPoints;
public:
  void addSetOfPoints(std::vector<Point*> points)

等。


第三种解决方案如下:

#include <vector>
using namespace std;

从编码风格的角度来看,最后 一个解决方案是我觉得不太受欢迎。原因是它将 std 命名空间中的所有内容导入到默认命名空间中。相比之下,我发现最好显式导入我正在使用的部分,因为它允许我跟踪为什么我需要标题。在这种情况下这没有意义(当然我需要 ,我正在使用 std::vectors)。在这样的情况下,它更相关:

#include <algorithm>
using std::adjacent_find;

哦,是的,这就是为什么我把它包括在内......

vector is in the std:: namespace. so vector is undefined in your example code

Two possible solutions:

#include <vector>
using std::vector;

or: (in all cases through the code where you reference vector, both declaration and reference)

private:
  std::vector<Point*> myPolygonPoints;
public:
  void addSetOfPoints(std::vector<Point*> points)

etc.


A third solution is the following:

#include <vector>
using namespace std;

This last one, from a coding style perspective, I find less preferred. The reason is that it imports absolutely everything in the std namespace into the default namespace. By contrast, I find it preferrable to explicitly import the pieces I'm using, becuase it allows me to keep track of why I need a header. This doesn't make a sense in this case (of course I need <Vector>, I'm using std::vectors). It's much more relevant in a case like this:

#include <algorithm>
using std::adjacent_find;

Oh yeah, that's why I included that...

狂之美人 2024-10-26 12:59:56

如果您没有明确声明您正在使用 std 命名空间,则应该引用属于哪个命名空间向量。

If you're not explicitly declaring that you're using the std namespace you should reference which namespace vector belongs to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文