Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 来自默认构造函数的神秘int值_C++_Constructor_Initialization - Fatal编程技术网

C++ 来自默认构造函数的神秘int值

C++ 来自默认构造函数的神秘int值,c++,constructor,initialization,C++,Constructor,Initialization,当我运行程序时,我得到的是垃圾值,而不是2、4和6 -858993460 -858993460 Sum of potion charges: -858993460Press any key to continue . . . 除了我在main中给出的参数,我不明白为什么构造函数会初始化任何东西 potions.cpp: #include "stdafx.h" #include "potions.h" int Potion::getCharges() const { return p

当我运行程序时,我得到的是垃圾值,而不是2、4和6

-858993460
-858993460
Sum of potion charges: -858993460Press any key to continue . . .
除了我在main中给出的参数,我不明白为什么构造函数会初始化任何东西

potions.cpp:

#include "stdafx.h"
#include "potions.h"


int Potion::getCharges() const
{
    return potion_charges;
}

Potion::Potion()
{
    int potion_charges = 0;
}

Potion::Potion(int charges)
{
    int potion_charges = charges;
}

Potion::~Potion()
{
    ;
}

Potion operator+(const Potion &potion_charges1, const Potion &potion_charges2)
{
    return Potion(potion_charges1.potion_charges + potion_charges2.potion_charges);
}
药剂h:

#pragma once
#include "stdafx.h"
using namespace std;

#ifndef POTIONS_H
#define POTIONS_H

class Potion
{
private:
    int potion_charges;                                                                 
public:
    Potion();                       // Default constr
    Potion(int charges);            // Overloaded constr
    ~Potion();                      // Destr
    int getCharges() const;
    friend Potion operator+(const Potion &potion_charges1, const Potion &potion_charges2);
};

#endif
main.cpp:

#include "stdafx.h"
#include "potions.h"
#include <iostream>

int main()
{
    Potion potion1(2);
    Potion potion2(4);
    Potion potion3 = potion1 + potion2;
    cout << potion1.getCharges() << endl
        << potion2.getCharges() << endl;
    cout << "Sum of potion charges: " << potion3.getCharges();
    system("PAUSE");    
    return 0;
}
#包括“stdafx.h”
#包括“药剂.h”
#包括
int main()
{
药剂药剂1(2);
药剂药剂2(4);
药剂药剂3=药剂1+药剂2;

构造器内部的cout

Potion::Potion()
{
    int potion_charges = 0;
}   
Potion::Potion(int charges)
{
    int potion_charges = charges;
}
您正在定义和初始化名为
potion\u charges
的局部变量,该变量与成员变量
potion\u charges
无关;成员
potion\u charges
根本没有初始化

将其更改为:

Potion::Potion()
{
    potion_charges = 0;
}
Potion::Potion(int charges)
{
    potion_charges = charges;
}
或使用:


构造器内部

Potion::Potion()
{
    int potion_charges = 0;
}   
Potion::Potion(int charges)
{
    int potion_charges = charges;
}
您正在定义和初始化名为
potion\u charges
的局部变量,该变量与成员变量
potion\u charges
无关;成员
potion\u charges
根本没有初始化

将其更改为:

Potion::Potion()
{
    potion_charges = 0;
}
Potion::Potion(int charges)
{
    potion_charges = charges;
}
或使用:


就像宋元耀所说的,你从不更新你的成员变量,只更新一个同名的局部变量

我建议您清理类定义,并使用现代编译器构建它:

class Potion
{
private:
    int potion_charges = 0; // Follows the c++ core guidelines. The default value
                            // is in the class definition itself.                                                            
public:
    Potion(int charges) : potion_charges(charges)
    {}
    int getCharges() const;
    friend Potion operator+(const Potion &potion_charges1, const Potion &potion_charges2);
};

您的类也不需要用户定义的析构函数,因为它不管理任何资源。通常最好遵循如宋元耀所说的那样。您从不更新成员变量,只更新具有相同名称的局部变量

我建议您清理类定义,并使用现代编译器构建它:

class Potion
{
private:
    int potion_charges = 0; // Follows the c++ core guidelines. The default value
                            // is in the class definition itself.                                                            
public:
    Potion(int charges) : potion_charges(charges)
    {}
    int getCharges() const;
    friend Potion operator+(const Potion &potion_charges1, const Potion &potion_charges2);
};
您的类也不需要用户定义的析构函数,因为它不管理任何资源

您在构造函数中的药水费用是自动可变的,而不是您的类的字段


构造函数中的药水费用是自动变量,而不是类的字段。

您从未更新成员变量
药水费用
,因此它包含垃圾值,因为它未初始化。发布文本,而不是文本图片。您从未更新成员变量
药水费用
,因此它包含垃圾值b因为它是未初始化的。张贴文本,而不是文本图片。谢谢,这就把它清理了。谢谢,这就清除了。不幸的是,VS2015没有给我一个警告,但是感谢你们两个,这是我再也不会犯的一个错误:)PaulPaWoWSky-变量阴影是合法的C++。所以通常很难得到编译器警告。小伙子,你得到了你的帮助。不幸的是,VS2015没有给我一个警告,但是多亏了你们两个,这是我再也不能犯的一个错误:)PaulPaWoWSky-变量遮蔽是合法的C++。所以通常很难得到编译器警告。很高兴得到了你们的帮助。