tec*_*man 1 c++ vector getter-setter
我有一个顶点向量,我希望将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; }
};
Run Code Online (Sandbox Code Playgroud)
在某些函数中,我将值分配给它:
for(int i=0; i<g1.Vertice1.size(); i++)
{
g1.Vertice1.at(i)->setNum(0);
g1.Vertice1.at(i)->setVisit(0);
}
Run Code Online (Sandbox Code Playgroud)
下面是编译"this.mark = mark"代码和类中函数定义中"this.num = num"的错误.
Error: left of '.mark' must have class/struct/union
Error: left of '.num' must have class/struct/union
Run Code Online (Sandbox Code Playgroud)
这不是通过getter和setter分配值的正确方法吗?
在C++中,this是指针类型.试试this->mark或(*this).mark
编辑:正如理查德在下面指出的那样,也要确保命名您要分配的参数.在上下文中,this->mark = mark是一样的this->mark = this->mark.除非mark是参数,否则this在这种情况下实际上是不必要的.实际上,你可以this通过这样的方式在你的例子中摆脱所有:
void setVisit(int newMark) { mark = newMark; }
Run Code Online (Sandbox Code Playgroud)
此致,
Dennis M.
| 归档时间: |
|
| 查看次数: |
501 次 |
| 最近记录: |