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

为何C++泛型模板会返回十六进制值?求助解析

C++泛型Node类get_data()返回十六进制值的原因及修正

问题背景

编写了一个带泛型模板的C++ Node类,用于存储和获取泛型变量,存储变量时输出正常,但调用get_data()获取变量时返回十六进制值,相关代码及运行输出如下:

node.h

#pragma once
#include <iostream>

template <typename T>
class Node {
    private:
        T data;
        Node *next_node;

    public:
        Node(T data) {
            this->data = data;
            std::cout << "Node with data - " << this->data << " - has been created." << std::endl;
        }
        
        T get_data() {
            return this->data;
        }
        
        void set_next_node(Node next) {
            this->next_node = next;
        }

        Node get_next_node() {
            return this->next_node;
        }
};

main.cpp

#include <iostream>
#include "node.h"

int main() {

    Node node = new Node("Hello World");

    std::cout << "Node data: " << node.get_data() << std::endl;

    return 0;
}

运行输出

gabriel@desktop:/media/gabriel/main/repositories/CPP Sandbox/nodes$ g++ main.cpp -o main
gabriel@desktop:/media/gabriel/main/repositories/CPP Sandbox/nodes$ ./main
Node with data - Hello World - has been created.
Node with data - 0x55ec552d3eb0 - has been created.
Node data: 0x55ec552d3eb0

问题原因分析

  1. 模板类实例化错误
    C++模板类必须显式指定类型参数(除非编译器能从上下文明确推导)。原代码中new Node("Hello World")虽然能推导T为const char*,但new返回的是Node<const char*>*指针;而Node node = ...试图将指针赋值给未指定模板参数的Node对象,编译器会错误地将T推导为Node<const char*>*,此时data存储的是指针的内存地址,输出时自然显示为十六进制值。

  2. 指针与对象的错误赋值
    new创建的是堆上的对象,返回指针。直接将指针赋值给栈上的Node对象会触发拷贝构造函数,把指针作为T类型的值存入新的Node对象——这就是输出中第二个Node with data - 0x...的来源,这个新对象的data就是原指针的地址。

  3. 链表节点方法的类型错误
    原代码中set_next_node接受Node对象,但next_node是Node*指针,赋值时会隐式取对象地址;get_next_node返回Node对象,但next_node是指针,返回时会将指针强制转换为对象,这会导致未定义行为,完全不符合链表节点的设计逻辑。


修正后的代码

修正后的node.h

#pragma once
#include <iostream>

template <typename T>
class Node {
private:
    T data;
    Node* next_node;

public:
    // 使用初始化列表初始化成员,同时将next_node默认置空
    Node(T data) : data(data), next_node(nullptr) {
        std::cout << "Node with data - " << this->data << " - has been created." << std::endl;
    }
    
    T get_data() {
        return this->data;
    }
    
    // 修改参数为指针类型,匹配next_node的类型
    void set_next_node(Node* next) {
        this->next_node = next;
    }

    // 修改返回值为指针类型,正确返回下一个节点的指针
    Node* get_next_node() {
        return this->next_node;
    }
};

修正后的main.cpp

#include <iostream>
#include "node.h"

int main() {
    // 方式1:直接创建栈对象,显式指定模板类型
    Node<const char*> node("Hello World");
    std::cout << "Node data: " << node.get_data() << std::endl;

    // 方式2:创建堆对象(需手动释放内存)
    // Node<const char*>* node_ptr = new Node<const char*>("Hello World");
    // std::cout << "Node data: " << node_ptr->get_data() << std::endl;
    // delete node_ptr;

    return 0;
}

修正后的运行输出

gabriel@desktop:/media/gabriel/main/repositories/CPP Sandbox/nodes$ g++ main.cpp -o main
gabriel@desktop:/media/gabriel/main/repositories/CPP Sandbox/nodes$ ./main
Node with data - Hello World - has been created.
Node data: Hello World

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 18:48:18