链接器错误:函数重复 [英] Linker Error: Duplicated Functions

查看:97
本文介绍了链接器错误:函数重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意::我制作了DFH​​_lib.CPP,其中包含fstream和iomanip.我将所有模板函数都保存在DFH_lib.CPP中.现在,如果我在MAIN.CPP中编写其余的NON-TEMPLATE函数,并且仅包含DFH_lib.h,则它将成功运行.我不明白为什么...

NOTE: I made a DFH_lib.CPP where I included fstream and iomanip. I kept all the template functions in DFH_lib.CPP. Now, if I write the remaining NON-TEMPLATE functions in the MAIN.CPP and include DFH_lib.h only then it successfully runs. I don't understand why...

我当时正在使用模板制作数据文件处理库.我创建了两个文件:

I was making a Data File Handling library using templates. I created two files:

DFH_lib.CPP
Lib_Test.CPP

我创建了一个项目,然后点击全部构建"正在编译.我遇到以下链接器错误:

I made a project and clicked on "Build All" under compile. I encountered the following linker error:

在模块DFH_LIB.CPP中定义的

file_init(char near *)重复在模块LIB_TEST.CPP中

file_init(char near*) defined in module DFH_LIB.CPP is duplicated in module LIB_TEST.CPP

AddColumn(const int near&)重复在模块LIB_TEST.CPP中

AddColumn(const int near&) defined in module DFH_LIB.CPP is duplicated in module LIB_TEST.CPP

file_init(char*);AddColumn(T data, const int& width);AddColumn(const int& width);是仅在DFH_lib.CPP中定义的函数.我只在Lib_Test.CPP中调用了这些函数.

file_init(char*); and AddColumn(T data, const int& width); and AddColumn(const int& width); are functions which I only defined in DFH_lib.CPP. I only made calls to these functions in Lib_Test.CPP.

DFH_lib.CPP

template <class T>    //Function belongs to Pretty Printing Libary
void AddColumn(T data, const int& width) {
    cout<<setw(width)<<data<<" | ";
}
void AddColumn(const int& width) {
    cout<<setw(width)<<setfill('_')<<"|";
}
void file_init(char* file) {   //File initialization function
    ofstream fout;
    fout.open(file, ios::binary|ios::noreplace);   //File Created, noreplace prevents data loss
    fout.close();
}

Lib_Test.CPP

cout<<endl; AddColumn(record_id,7); AddColumn(char_member, 20); AddColumn(int_member, 11); AddColumn(float_member, 13);
file_init(file);    //initializes the file

其中文件"定义为: char file [] ="lib_Test.dat";

有人可以解释为什么我收到此链接器错误吗?我不明白这意味着什么,因此不知道如何解决...

Could someone please explain why I'm getting this Linker Error? I don't understand what it means and therefore, how to fix it...

我注意到,这可能是由于在将文件Lib_Test.CPP转换为"Hello World"时将文件包含在内时出错所导致的.程序,并出现相同的错误.我还指出了另一件事:仅非模板函数会导致链接错误!

I've noticed that this might be resulting due to a mistake done while including files, as I turned the Lib_Test.CPP into a "Hello World" program and the same error appeared. One more thing I noted: Only the non-template functions are causing the linking error!

DFH_lib.CPP

#ifndef _DFH_lib_cpp
#define _DFH_lib_cpp

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<iomanip.h>
#include<string.h>
.....
#endif

Lib_Test.CPP

#include<iostream.h>
#include<conio.h>
#include"DFH_lib.CPP"  //Including DFH Libary

推荐答案

我看到此问题的以下原因:

I see following reasons for the problem:

  1. 编译器错误

旧的 TC ++ 有时会出现模板编译问题,这些模板通常添加空行(在正确的位置)或交换一些代码帮助.但这通常仅在源代码达到特定大小(如 30-50 KB )时才开始确定,而不再确定它的使用时间是多少.我不相信这是您的问题.

Old TC++ has sometimes compile problems with templates usually adding empty line (at the right place) or swap some lines of code help. But that usually starts only when your source code hit certain size like 30-50 Kbyte not sure anymore about the value it was ages ago. I am not convinced this is your issue.

确实是重复的

如果多次包含某些文件,则将导致类似您的错误.为了解决这个问题,您可以将每个文件封装到此文件中:

if you are including some file multiple times then it will lead to errors like yours. To remedy that you can encapsulate each file into this:

#ifndef _DFH_lib_cpp
#define _DFH_lib_cpp
// here comes your file DFH_lib.cpp content
#endif

其中 _DFH_lib_cpp 令牌是您要封装的编码文件名.这将丢弃所有重复的包含.这也解决了存在全局变量的问题,但请注意,如果未正确包含它们,则它们在整个项目中可能并不相同.

where _DFH_lib_cpp token is encoded filename you are encapsulating. This will throw away any duplicate includes. This also solves problem with global variables present but be careful they may not be the same across whole project if not included properly.

这篇关于链接器错误:函数重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆