C++文件拆分后编译问题

dgmzf 2004-11-09 04:36:06
//有以下程序,编译能运行没有问题
#include <iostream>
#include <string>
class T1 {
private :
std::string filename ;
public :
T1() ;
void print() ;
} ;
T1::T1(){ filename = "dfs" ;}

void T1::print(){ std::cout << filename << std::endl ;}

int main(void)
{
T1 a1 ;
a1.print() ;
return 0 ;
}

//-------但如果把它拆分成几个文件,则编译有警告-----
//文件t1.h
#include <iostream>
#include <string>
class T1 {
private :
std::string filename ;
public :
T1() ;
void print() ;
} ;
//文件t1.C
#include "t1.h"
T1::T1(){ filename = "dfs" ;}
void T1::print(){ std::cout << filename << std::endl ;}
//文件t2.C
#include "t1.h"
int main(void)
{
T1 a1 ;
a1.print() ;
return 0 ;
}
编译时报如下警告:
xlC t2.C t1.C
t2.C:
t1.C:
ld: 0711-224 WARNING: Duplicate symbol: .std::basic_string<char,std::char_traits<char>,std::allocator<char> >::~basic_string()
ld: 0711-224 WARNING: Duplicate symbol: .std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Tidy(bool)
ld: 0711-224 WARNING: Duplicate symbol: .std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Refcnt(const char*)
ld: 0711-224 WARNING: Duplicate symbol: .std::allocator<char>::deallocate(char*,unsigned long)
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information.
...全文
116 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
dgmzf 2004-11-10
  • 打赏
  • 举报
回复
谢谢!
aa1298 2004-11-09
  • 打赏
  • 举报
回复
重复定义了!
皮鲁 2004-11-09
  • 打赏
  • 举报
回复
某些开发平台把这些文件当成一个工程编译,可能会出错
dgmzf 2004-11-09
  • 打赏
  • 举报
回复
平台是IBM AIX。
dgmzf 2004-11-09
  • 打赏
  • 举报
回复
在t1.h中加了#inndef...,如下,编译时还是会出现那些警告:
#ifndef __T1_H_
#define __T1_H_

#include <iostream>
#include <string>
class T1 {
private :
std::string filename ;
public :
T1() ;
void print() ;
} ;
#endif
new1mm 2004-11-09
  • 打赏
  • 举报
回复
t1.h 头部加上
#ifndef _T1_H_
#define _T1_H_

最后加上
#endif



#pragma once
snipersu 2004-11-09
  • 打赏
  • 举报
回复
Maybe the problem of your software development kit.
There are no problems and warnings when i run you program in my vc6.0.

mathe 2004-11-09
  • 打赏
  • 举报
回复
不就是一些Warning吗,不用管了。
你用的是什么编译器?好像对Template处理有问题。
deltacat 2004-11-09
  • 打赏
  • 举报
回复
t1.h 头部加上
#ifndef _T1_H_
#define _T1_H_

最后加上
#endif
xuzheng318 2004-11-09
  • 打赏
  • 举报
回复
重复定义了!
deltacat 2004-11-09
  • 打赏
  • 举报
回复
你头文件 tl.h 重复包含了啊

64,648

社区成员

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

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