在自定义的namespace中使用模版,为何连接不成功?

执假以为真 2013-05-09 01:10:31
请教一个问题,我自定义了一个namespace,然后在其中写了个模版函数,不知道为何最终连接的时候不成功。
各位试试?代码如下:


// testnamespace.h

#ifndef TESTNAMESPACE_H
#define TESTNAMESPACE_H

namespace MySpace
{

template<typename T>
void PrintArray(T * array, long size);


void PrintIntArray(int * array, long size);
}

#endif



// testnamespace.cp

#include <iostream>

#include "testnamespace.h"

namespace MySpace
{

template<typename T>
void PrintArray(T * array, long size) {
for (long i = 0; i < size; i++) {
std::cout<<array[i] <<", ";
}
std::cout<<std::endl;
}

void PrintIntArray(int * array, long size) {
for (long i = 0; i < size; i++) {
std::cout<<array[i] <<", ";
}
std::cout<<std::endl;
}
}



// main.cpp

#include "testnamespace.h"

int main(void)
{
int array[] = {10, 20, 30};
// MySpace::PrintArray<int>(array, sizeof(array)/sizeof(int));
MySpace::PrintIntArray(array, sizeof(array)/sizeof(int));

return 0;
}


如果去掉main.cpp中的注释,则连接不通过。虽然我只在Cygwin上试了,但相信在真实Linux上也一样。
...全文
175 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ztenv 版主 2013-05-09
  • 打赏
  • 举报
回复
引用 1 楼 turingo 的回复:
带模板的代码不支持.h和.cpp分离的方式,把它都集中到.h中后gcc main.cpp编译即可:

// testnamespace.h
 
#ifndef TESTNAMESPACE_H
#define TESTNAMESPACE_H

#include <iostream>
 
namespace MySpace 
{
 
    template<typename T>
    void PrintArray(T * array, long size);
 
     
    void PrintIntArray(int * array, long size);
}

namespace MySpace 
{
 
    template<typename T>
    void PrintArray(T * array, long size) {
        for (long i = 0; i < size; i++) {
            std::cout<<array[i] <<", ";
        }
        std::cout<<std::endl;
    }
     
    void PrintIntArray(int * array, long size) {
        for (long i = 0; i < size; i++) {
            std::cout<<array[i] <<", ";
        }
        std::cout<<std::endl;
    }
}
#endif
有的编译器支持将.cpp与.h一并include到所需文件
hugett 2013-05-09
  • 打赏
  • 举报
回复
模版的定义也应该放在头文件中

// testnamespace.h

#ifndef TESTNAMESPACE_H
#define TESTNAMESPACE_H

#include <iostream>

namespace MySpace 
{
    template<typename T>
    void PrintArray(T * array, long size) {
        for (long i = 0; i < size; i++) {
            std::cout<<array[i] <<", ";
        }
        std::cout<<std::endl;
    }
    
    void PrintIntArray(int * array, long size);
}

#endif

// testnamespace.cp

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

namespace MySpace 
{
    void PrintIntArray(int * array, long size) {
        for (long i = 0; i < size; i++) {
            std::cout<<array[i] <<", ";
        }
        std::cout<<std::endl;
    }
}
qj5656 2013-05-09
  • 打赏
  • 举报
回复
模板的定义和声明都要放在一起的啊。
Jackie_Zhu 2013-05-09
  • 打赏
  • 举报
回复
templete申明和定义要放在头文件中,分开写有些编译器可能会不支持。或者在cpp的函数定义之前加export关键字
图灵狗 2013-05-09
  • 打赏
  • 举报
回复
带模板的代码不支持.h和.cpp分离的方式,把它都集中到.h中后gcc main.cpp编译即可:

// testnamespace.h
 
#ifndef TESTNAMESPACE_H
#define TESTNAMESPACE_H

#include <iostream>
 
namespace MySpace 
{
 
    template<typename T>
    void PrintArray(T * array, long size);
 
     
    void PrintIntArray(int * array, long size);
}

namespace MySpace 
{
 
    template<typename T>
    void PrintArray(T * array, long size) {
        for (long i = 0; i < size; i++) {
            std::cout<<array[i] <<", ";
        }
        std::cout<<std::endl;
    }
     
    void PrintIntArray(int * array, long size) {
        for (long i = 0; i < size; i++) {
            std::cout<<array[i] <<", ";
        }
        std::cout<<std::endl;
    }
}
#endif

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧