C++类定义编译问题
我已经查看了所有内容,但找不到与我的问题相关的任何内容。我试图为多边形类编写一个类定义,该多边形类基本上有一个保存指向点的指针的向量。当我尝试编译时,我不断收到以下错误...
错误 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
除了
std::vector
问题之外,您还错误地拼写了setHasIntersection
方法中的myHasIntersection
变量。Besides the
std::vector
problem, you have also mispelled themyHasIntersection
variable in thesetHasIntersection
method.您正在使用
std
命名空间中的vector
,但没有对其进行限定。您要么必须
using namespace std;
,要么using std::vector
,或者使用std声明所有
命名空间,例如vector
对象std::vector
。You are using
vector
from thestd
namespace without qualifying it.You either have to do
using namespace std;
, orusing std::vector
, or declaring all yourvector
objects with thestd
namespace likestd::vector
.矢量位于 std:: 命名空间中。所以向量在示例代码中未定义
两种可能的解决方案:
或:(在所有情况下通过引用向量的代码,声明和引用)
等。
第三种解决方案如下:
从编码风格的角度来看,最后 一个解决方案是我觉得不太受欢迎。原因是它将 std 命名空间中的所有内容导入到默认命名空间中。相比之下,我发现最好显式导入我正在使用的部分,因为它允许我跟踪为什么我需要标题。在这种情况下这没有意义(当然我需要
,我正在使用 std::vectors)。在这样的情况下,它更相关:哦,是的,这就是为什么我把它包括在内......
vector is in the std:: namespace. so vector is undefined in your example code
Two possible solutions:
or: (in all cases through the code where you reference vector, both declaration and reference)
etc.
A third solution is the following:
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:Oh yeah, that's why I included that...
如果您没有明确声明您正在使用 std 命名空间,则应该引用属于哪个命名空间向量。
If you're not explicitly declaring that you're using the std namespace you should reference which namespace vector belongs to.