xcman的个人空间 https://blog.eetop.cn/1574971 [收藏] [复制] [分享] [RSS]

空间首页 动态 记录 日志 相册 主题 分享 留言板 个人资料

日志

C++ 全局函数

已有 1381 次阅读| 2020-8-31 10:58 |个人分类:C++|系统分类:其他

为了方便调试,想写一个vector打印的功能而且能用在多个类中。

尝试了采用和C语言相同的方法,写了个common.h并include 到每个类的cpp中,但是发现这种方法会报“找不到函数”的错误。

正确的做法应该是写common.h 和common.cpp, 并在想使用的类的h文件中include common.h

common.h

#ifndef COMMON_H

#define COMMON_H

#include <vector>

extern void displayVectorInt(std::vector<int> vec);

#endif

common.cpp

#include<common.h>

extern void  displayVectorInt(std::vector<int> vec)

{

    printf("[");

    for (int i=0;i<vec.size();i++){

        printf(" %d",vec[i]);

        if (i!=vec.size()-1){

            printf(",");

        }

    }

    printf("]\n");

}


点赞

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 注册

  • 关注TA
  • 加好友
  • 联系TA
  • 0

    周排名
  • 0

    月排名
  • 0

    总排名
  • 0

    关注
  • 2

    粉丝
  • 0

    好友
  • 0

    获赞
  • 0

    评论
  • 1068

    访问数
关闭

站长推荐 上一条 /1 下一条

小黑屋| 关于我们| 联系我们| 在线咨询| 隐私声明| EETOP 创芯网
( 京ICP备:10050787号 京公网安备:11010502037710 )

GMT+8, 2024-4-27 14:15 , Processed in 0.014223 second(s), 8 queries , Gzip On, Redis On.

eetop公众号 创芯大讲堂 创芯人才网
返回顶部