wangjianxyz 2023-05-18 14:08 采纳率: 0%
浏览 18

C++ 2个结构体重名,g++有办法发现吗?

我有2个cpp文件,2个里面各自有一个结构体,成员不一样,但是名字恰好一样。
我使用的时候,就会出问题,2个结构体混淆了,可能会导致程序崩溃。
用g++编译的时候,g++并没有报错。
有没有方法,能主动发现类似的错误?

下面的源码,运行时会直接崩溃。
而且,看打印,test_fun.cpp里面的变量,析构时,会调用main.cpp里面的析构函数。

#include <stdio.h>
#include <string>
#include <vector>
#include <unistd.h>

#define    ht_log_info(xx,args...)    fprintf(stderr, xx"\n", ##args)


int test_key_fun1();
int test_key_fun2();
int test_key_fun3();

int a;
typedef struct key_info_s{
    std::string key;
    std::string value;
    ~key_info_s(){
           ht_log_info("main::key_info_s: delete ,size=%d", sizeof( struct key_info_s));
       }
}KEY_INFO_T;

class KEY_ITEM_T{
    std::string key;
    std::string value;
    int a;
    int b;
    int c;
};


int main()
{


   // test_key_fun2();
    test_key_fun3();
    test_key_fun1();
     KEY_INFO_T aaaa;
    while(1)
    {
        usleep(1e6);

        int a = 34;
        int b= 34;
        int c = a + b;
        ht_log_info("c=%d", c);
    }

    return 0;
}



static int m_test_key1=13;
#include <stdio.h>
#include <string>
#include <vector>

#define    ht_log_info(xx,args...)    fprintf(stderr, xx"\n", ##args)


typedef struct key_info_s{
    int x;
    int y;
    std::string key;
    std::string value;
    int a;
    int b;
    int c;

    ~key_info_s(){
           ht_log_info("test_fun.cpp::key_info_s: delete ,size=%d", sizeof( struct key_info_s));
       }
}KEY_INFO_T;

class KEY_ITEM_T{
    public:
    int x;
    int y;
    std::string key;
    std::string value;
    int a;
    int b;
    int c;
};

static std::vector<KEY_INFO_T>   m_key_info_list;
extern int a;
int test_key_fun1()
{
    KEY_INFO_T key_info;
    key_info.x = 11;
    key_info.y = 12;
    key_info.key = "abcd";
    key_info.value = "123";
    key_info.a = 21;
    key_info.b = 22;
    key_info.c = 23;

    m_key_info_list.push_back(key_info);

    KEY_INFO_T &key_info2 = m_key_info_list[0];
    ht_log_info("size=%d, [%d][%d][%s][%s]", sizeof(KEY_INFO_T), key_info2.x, key_info2.y, key_info2.key.data(), key_info2.value.data());
    ht_log_info("size=%d, [%d][%d][%d]", sizeof(KEY_INFO_T), key_info2.a, key_info2.b, key_info2.c);

    return 0;
}


int test_key_fun2()
{
    ht_log_info("test_key_fun2:m_test_key1=%d", m_test_key1);
    ht_log_info("a=%d", a);
    ht_log_info("a=%lf", a);
    return 1;
}

static std::vector<KEY_ITEM_T>   m_key_item_list;
int test_key_fun3()
{
    KEY_ITEM_T key_info;
    key_info.x = 11;
    key_info.y = 12;
    key_info.key = "abcd";
    key_info.value = "123";

    m_key_item_list.push_back(key_info);

    KEY_ITEM_T &key_info2 = m_key_item_list[0];
    ht_log_info("size=%d, [%d][%d][%s][%s]",sizeof(KEY_ITEM_T),  key_info2.x, key_info2.y, key_info2.key.data(), key_info2.value.data());

    return 0;
}



  • 写回答

2条回答 默认 最新

  • Loup&卡普 2023-05-18 17:05
    关注

    你把结构体 都定义到 头文件里 包含进来 就会报错了

    评论

报告相同问题?

问题事件

  • 创建了问题 5月18日

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加