getter 和 setter 的错误

发布于 2024-10-31 12:00:27 字数 1040 浏览 3 评论 0原文

我有一个顶点向量,我希望最初将 vertice.setVisit 设置为 false 或 0。我为此定义了一些 getter 和 setter,但出现了一些类型错误。代码如下:

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

//Class containing methods:
template <class VertexType, 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; }
};

在某些函数中,我将值分配给它:

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

下面是在函数中编译“this.mark=mark”和“this.num=num”的代码时遇到的错误类中的定义。

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

这不是通过 getter 和 setter 赋值的正确方法吗?

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 VertexType, 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?

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

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

发布评论

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

评论(3

浅紫色的梦幻 2024-11-07 12:00:27

在 C++ 中,this 是指针类型。尝试 this->mark(*this).mark

编辑: 正如理查德在下面指出的,也请确保将您的参数命名为你正在尝试分配。在上下文中,this->mark = markthis->mark = this->mark 相同。除非 mark 是一个参数,否则在这种情况下 this 确实没有必要。所以实际上,您可以通过执行以下操作来在示例中一起摆脱this

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

问候,
丹尼斯·M.

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.

挽手叙旧 2024-11-07 12:00:27

将 setNum 和 setVisit 更改为以下内容

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

Change setNum and setVisit to the following

void setNum(int num) {this->num = num; }
void setVisit(int mark) {this->mark = mark; }
溺深海 2024-11-07 12:00:27

this 是一个指针,因此必须使用 this->num

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

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