开发者

Error for getters and setters

I have a vector of vertices and I wish to set vertice.setVisit as false or 0 initially. I defined some getters and setters for this, but getting some type error. Code is as per below:

//Vector:
std::vector<project3::Vertex<VertexType, EdgeType>*> Vertice1;

//Class containing methods:
template <class Ve开发者_如何学PythonrtexType, class EdgeType> class Vertex{
protected:
    //std::vector<std::pair<int, EdgeType>> VertexList;
    VertexType vertice;
    EdgeType edge;
    int num;
    int mark;
    //int status=0;

public:

    void setNum(int){ this.num = num; }
    int getNum() { return num; }
    int getMark(){ return mark; }
    void setVisit(int) { this.mark = mark; }
};

In some function I am assigning the values to it as :

for(int i=0; i<g1.Vertice1.size(); i++)
{
    g1.Vertice1.at(i)->setNum(0);
    g1.Vertice1.at(i)->setVisit(0);
}

Below is the error I am getting while compilation of the code for "this.mark=mark" and for "this.num=num" in the function definition in class.

Error: left of '.mark' must have class/struct/union
 Error: left of '.num' must have class/struct/union

Isn't this the correct way to assign the values through getter and setters?


In C++, this is a pointer-type. Try this->mark or (*this).mark

EDIT: As Richard pointed out below, also be sure to name your parameters which you are trying to assign. In context, this->mark = mark is the same thing as this->mark = this->mark. Unless mark is a parameter, this is really unnecessary in this case. So realistically, you can get rid of this all together in your example by doing something like this:

void setVisit(int newMark) { mark = newMark; }

Regards,
Dennis M.


Change setNum and setVisit to the following

void setNum(int num) {this->num = num; }
void setVisit(int mark) {this->mark = mark; }


this is a pointer, so you have to use this->num.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜