简体   繁体   English

带有Getter函数的C ++错误

[英]Error in C++ with a Getter function

I'm getting a strange error when i tried do display some information. 我尝试显示某些信息时遇到一个奇怪的错误。 Here it's a portion of my class code: 这是我的班级代码的一部分:

class Vertex
{
public:
    Vertex(int name)
    {
        name = name;
        pred = NULL;
        color = 'w';
        desc = 1000000;
    }

    int getName() {return name;} 
    char getColor() {return color;} 
    Vertex* getPred() {return pred;} 
    int getDesc() {return desc;}

    void setColor(char c) {color = c;} 
    void setPred(Vertex *p) {pred = p;}
    void setDesc(int d) {desc = d;}

private:
    int name;
    char color;
    Vertex* pred;
    int desc;
};

I believe this is okay... but when i do this in my main function (which i believe it's also ok...): 我相信这是可以的...但是当我在我的主要功能中执行此操作时(我认为这也可以...):

for(int z = 1; z <= nsomething; z++){
    Vertex v(z);
    cout << "from z=" << z << " is vertex name: " << v.getName() << endl;
    cout << "from z=" << z << " is vertex color: " << v.getColor() << endl;
    Vertex *vp = &v;
    cout << "from z=" << z << " is vertex name: " << vp->getName() << endl;
    cout << "from z=" << z << " is vertex color: " << vp->getColor() << endl;
}

I get this: 我得到这个:

from z=1 is vertex name: 1854338496
from z=1 is vertex color: w
from z=1 is vertex name: 1854338496
from z=1 is vertex color: w
from z=2 is vertex name: 1854338496
from z=2 is vertex color: w
from z=2 is vertex name: 1854338496
from z=2 is vertex color: w

Can someone help me, telling why is this happening only with the getName()? 有人可以帮我,说为什么只有getName()会发生这种情况?

Thanks in advance! 提前致谢!

Change constructor 变更构造函数

Vertex(int name)
{
    name = name;
    pred = NULL;
    color = 'w';
    desc = 1000000;
}

the following way 以下方式

Vertex(int name)
{
    this->name = name;
    pred = NULL;
    color = 'w';
    desc = 1000000;
}

or 要么

Vertex(int name)
{
    Vertex::name = name;
    pred = NULL;
    color = 'w';
    desc = 1000000;
}

or 要么

Vertex(int name) : name( name )
{
    pred = NULL;
    color = 'w';
    desc = 1000000;
}

or 要么

Vertex(int name) : name( name ), pred( NULL ), color( 'w' ), desc( 1000000 )
{
}

Otherwise local variable name that hides data member name of the class is assigned to itself. 否则,将隐藏该类的数据成员name的局部变量name分配给它自己。

Also it is better to declare all getters with qualifier const. 同样最好用限定符const声明所有的获取方法。 For example 例如

int getName() const {return name;} 
char getColor() const {return color;} 
Vertex* getPred() const {return pred;} 
int getDesc() const {return desc;}

The variable name in your constructor shadows the member name . 构造函数中的变量name遮盖成员name You thus assign the local variable to itself. 因此,您将局部变量分配给它自己。

To fix this, either write this->name = name; 要解决此问题,请输入this->name = name; or change the name of the parameter. 或更改参数名称。

You could also use the member initialization list instead, which IMO is the best way to do this. 您也可以改用成员初始化列表 ,这是IMO的最佳方法。 With that, be aware of the fact that members will always be initialized in the order they were declared in the class, regardless of the order of initializers in the list. 这样,请注意,无论列表中初始化程序的顺序如何,成员将始终按照在类中声明的顺序进行初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM