在多个类中使用外部函数时出错
getting error by using outer functions in multiple classes
我有3个类,每个类相互关联,其中3个使用相同的函数,所以我决定创建functions.h文件,将每个人使用的"外部"函数放在该文件中
因此,对于演示:
functions.h
len function
subs function
A.cpp
#include "functions.h"
#include "A.h"
#include "B.h"
cout<<len(word);
B.cpp
#include "functions.h"
#include "B.h"
#include "A.h"
cout<<subs(word,0,1);
C.cpp
cout<<len(word);
所有这些文件都包括带有特定函数的functions.h文件,对于我正在使用的头文件头保护
当我构建项目时,我得到错误:
Error 1 error LNK2005: "int __cdecl len(char *)" (?len@@YAHPAD@Z) already defined in A.obj C:Program Files (x86)Microsoft Visual Studio 10.0VC1033Ass3Ass3B.obj Ass3
subs(函数)也有同样的错误
有什么建议吗
谢谢
编辑:
函数.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <math.h>
int len(char *w){
int count=0;
int i=0;
while (w[i]!=' ')
{
count++;
i++;
}
//cout<<"Length of word is:"<<count<<"n";
return count;
}
char *subs(char *w,int s,int e){
int i,j=0;
int size=0;
size=abs((e))+1;
//cout<<"new size is:"<<size<<"n";
char *newW=new char[size];
for(i = 0 ;i<e; i++)
{
newW[i]=w[s];
s++;
}
newW[i]=' ';
return newW;
}
#endif
Word.cpp
#include "Word.h"
#include "functions.h"
Word::Word(char *_word){word=_word;}
bool Word::equals(char* _word){
cout<<"len of the first word is: "<<len(word)<<" and len of the checked word is: "<<len(_word)<<endl;
if(len(word)!=len(_word))
return false;
else{
for(int i=0;i<len(word);i++)
if(word[i]!=_word[i])
return false;
}
return true;
}
char Word::getWord(){
char *nW = new char[len(word)];
//cout<<"len of word is:"<<len(word);
int l=len(word);
for(int i=0;i<l;i++)
{
nW[i]=word[i];
cout<<nW[i];
}
return *nW;
}
void Word::print(char *word){cout<<word;}
void Word::print(){cout<<word<<" ";}
句子.cpp
#include "Sentence.h"
#include "Word.h"
#include "functions.h"
Sentence::Sentence()
{
char* sentence=new char[300];
cout<<"Entere sentence"<<endl;
cin.getline(sentence,300);
int i,j=0,lastIndex=0,count=0;
int l=len(sentence);
cout<<"Size of sentence is: "<<l<<"n";
for(i=0;i<l;i++){
//' ' ,'.', ',', '?', ':', '!' ,'N','r' ',', 't', ',', '-'
if (sentence[i]==' '||
sentence[i]=='.'||
sentence[i]==','||
sentence[i]=='?'||
sentence[i]==':'||
sentence[i]=='!'||
sentence[i]==';'||
sentence[i]=='-'){
count++;
if(count==1)
{
//cout<<subs(sentence,0,i);
//cout<<"Start Index: 0 and Length is: "<<i<<"n";
words[j]=new Word(subs(sentence,0,i));
lastIndex=i;
j++;
}
else{
//cout<<subs(sentence,lastIndex+1,i-lastIndex-1);
//cout<<"Start Index: "<<lastIndex+1<<" and Length is: "<<i-lastIndex-1<<"n";
words[j]=new Word(subs(sentence,lastIndex+1,i-lastIndex-1));
lastIndex=i;
j++;
}
}
}
if(lastIndex==0){
//cout<<subs(sentence,0,l);
words[j]= new Word(subs(sentence,0,l));
}
else{
//cout<<subs(sentence,lastIndex+1,i);
//cout<<"Start Index: "<<lastIndex+1<<" and length is: "<<i-lastIndex-1<<"n";
words[j]= new Word(subs(sentence,lastIndex+1,i-lastIndex-1));
}
wordNum=count+1;
}
bool Sentence::containsWord(char* _word){
for(int i=0;i<200;i++){
if(words[i]->equals(_word))
return true;
}
return false;
}
int Sentence::getWordNum(char *_word){
for(int i=0;i<200;i++){
cout<<words[i]->getWord();
if(words[i]->equals(_word))
return i+1;
}
return -1;
}
int Sentence::getWords(){return wordNum;}
int Word::getLen(){
return len(word);
}
似乎已将函数实现放入functions.h
中。保留原型,并将实现移动到单独的文件中,如functions.cpp
。
这应该奏效。
更新对您遇到的错误进行更多解释。
当您使用#include
时,它将在编译前由处理器进行处理(就像将.h
文件内联到源文件中一样)。这个过程发生在每个编译单元中,比如.cpp
文件(把它想象成树根)。
现在,由于include树的"根"是不同的(A.cpp
、B.cpp
…),include-guard无法帮助您避免函数实现内联到每个编译单元中。
这将导致链接错误,因为这些函数有多个实现。
每个编译单元(即A.cpp、B.cpp和C.cpp)都有包含在functions.h
中的每个函数的定义。
Include保护程序对此没有帮助,因为它们只在编译单元边界内发生作用(即,如果您在例如A.cpp中直接或间接包含functions.h
两次)
可能的解决方案有:
- 在
functions.h
中声明函数,并在一个单独的编译单元中定义它们,比如functions.cpp
(这是示例中的方法) - 您使
functions.h
中的每个函数都成为模板函数(只有在有意义的情况下才这样做) - 您声明
functions.h
中的每个函数都是静态的(这只是一个变通方法,我决不建议您这样做!!)
关于第3点的附言。这可能是有意义的,所以你可能想知道static
是关于什么的。当引用自由函数时,static
关键字将此函数标记为编译单元的本地函数。因此,该函数将不会在链接表中导出,也无法从另一个编译单元中调用。这就是为什么"技巧"有效的原因,尽管你仍然有多个定义,它们对每个编译单元都是"私有的",并且没有可能的冲突。当您使用不是"接口"的一部分并且仅是本地辅助函数的自由函数时,这尤其有用。
- 调用外部函数,无法指定类型 C++/MVS
- 加载由 MATLAB Coder 生成的带有函数的 DLL,该函数调用外部函数
- add_cuda_library输出:未解析的外部函数
- 外部函数在 main() 上调用时返回分段错误 11.(C++)
- 如何在Windows Media Foundation中获取相机外部函数?
- 内联asm编译器屏障(内存阻塞器)是算作外部函数,还是算作静态函数调用
- antlr cpp target 的标头部分不允许使用具有默认参数的外部函数
- 将多维数组传递给外部"C"函数为 void *
- llvm 调用一个以 char * 作为参数的外部函数
- 将 std::vector<bool> 传递给外部函数
- 如何在类向量中存储/使用外部函数指针
- LLVM - 如何使嵌套函数看到外部函数的变量
- CUDA9.2及以上版本中模板默认参数存在无法解决的外部函数错误
- 宏导入外部函数
- 如何在LLVM Pass中将字符串传递给外部函数
- C++:是否可以区分外部"C"函数和普通函数?
- 从外部函数访问外部结构属性
- 如何将成员声明为指向外部"C"函数的指针?
- C 将外部函数链接为成员函数
- LLVM JIT编译的程序找不到外部函数