1
votes

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

};
4
Stylistic note: it's kinder to present the public section first, then the protected, and finally the private one. A user of the class is only interested by the first (and perhaps the second in case of inheritance) and could not care less about the private one, so no need to have it in her eyes-path.Matthieu M.
I can't speak for others, but I'm always interested in seeing private details.Johannes Schaub - litb

4 Answers

6
votes

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

};
2
votes

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

1
votes

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

0
votes

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