You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++类成员输出异常:字符串为空、float显示nan/乱码求助

C++类成员输出异常:空字符串、NaN或奇怪数值

问题场景

编写存储游戏角色信息的C++类并定义构造函数后,输出类中字符串或float类型成员时出现异常:字符串为空,float显示NaN或无意义的长数字,int类型也显示错误值。

原代码

#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>

using namespace std;

class Character {
public:
    string type;
    string name;
    float HP;
    float MP;
    float str;
    float def;
    float agility;
    int LVL;

    Character(string atype, string aname, float aHP, float aMP, float astr, float adef, float aagility, int aLVL){
        string type = atype;
        string name = aname;
        float HP = aHP;
        float MP = aMP;
        float str = astr;
        float def = adef;
        float agility = aagility;
        int LVL = aLVL;
    }
};

int main()
{

    Character war("Warrior", "Achilles",    100, 30,  50,  35,    30,     1);

    cout << war.type << endl;
    cout << war.name << endl;
    cout << war.HP << endl;
    cout << war.MP << endl;
    cout << war.str << endl;
    cout << war.def << endl;
    cout << war.agility << endl;
    cout << war.LVL << endl;
    cout << "Test String" << endl;

    return 0;
}

异常输出

//nothing
//nothing again
1.08418e-038
1.08418e-038
4.15749e+033
4.19234e-033
nan
36
Test String

问题根源

构造函数中,给每个变量都重新声明了类型(比如string type = atype;),这会在构造函数内部创建局部变量,和类的成员变量重名但完全无关。类的成员变量根本没有被赋值,所以字符串默认是空,数值类型会保留内存中的随机垃圾值,导致输出异常。

修复方案

有两种常用的修复方式:

方式1:直接给类成员变量赋值

去掉构造函数中赋值语句的类型声明,直接操作类的成员变量:

#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>

using namespace std;

class Character {
public:
    string type;
    string name;
    float HP;
    float MP;
    float str;
    float def;
    float agility;
    int LVL;

    Character(string atype, string aname, float aHP, float aMP, float astr, float adef, float aagility, int aLVL){
        // 去掉前面的类型声明,直接给类成员赋值
        type = atype;
        name = aname;
        HP = aHP;
        MP = aMP;
        str = astr;
        def = adef;
        agility = aagility;
        LVL = aLVL;
    }
};

int main()
{
    Character war("Warrior", "Achilles", 100, 30, 50, 35, 30, 1);

    cout << war.type << endl;
    cout << war.name << endl;
    cout << war.HP << endl;
    cout << war.MP << endl;
    cout << war.str << endl;
    cout << war.def << endl;
    cout << war.agility << endl;
    cout << war.LVL << endl;
    cout << "Test String" << endl;

    return 0;
}

方式2:使用构造函数初始化列表(推荐)

这是C++中更高效、更规范的初始化类成员的方式,直接在构造函数声明后用冒号初始化成员:

#include <iostream>
#include <conio.h>
#include <string>
#include <windows.h>

using namespace std;

class Character {
public:
    string type;
    string name;
    float HP;
    float MP;
    float str;
    float def;
    float agility;
    int LVL;

    // 使用初始化列表直接初始化类成员
    Character(string atype, string aname, float aHP, float aMP, float astr, float adef, float aagility, int aLVL)
        : type(atype), name(aname), HP(aHP), MP(aMP), str(astr), def(adef), agility(aagility), LVL(aLVL)
    {
        // 构造函数体可以留空,或者添加其他逻辑
    }
};

int main()
{
    Character war("Warrior", "Achilles", 100, 30, 50, 35, 30, 1);

    cout << war.type << endl;
    cout << war.name << endl;
    cout << war.HP << endl;
    cout << war.MP << endl;
    cout << war.str << endl;
    cout << war.def << endl;
    cout << war.agility << endl;
    cout << war.LVL << endl;
    cout << "Test String" << endl;

    return 0;
}

修复后输出

两种方式修复后,都会得到正确的输出:

Warrior
Achilles
100
30
50
35
30
1
Test String

内容的提问来源于stack exchange,提问作者jad123xp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 19:54:28