开发者

C++ class definition compile problems

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;
    bo开发者_StackOverflow社区ol myHasIntersection;

public:
    void addSetOfPoints(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;
    }

};


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...


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜