我有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.0\VC\1033\Ass3\Ass3\B.obj Ass3
同样的错误是潜艇(功能) 有什么建议为什么会发生? 谢谢!
修改
functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include <math.h>
int len(char *w){
int count=0;
int i=0;
while (w[i]!='\0')
{
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]='\0';
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<<" ";}
Sentence.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);
}
答案 0 :(得分:4)
似乎已将函数实现放入functions.h
。保留 prototypes 并将实现移动到单独的文件中,例如functions.cpp
。
这应该可以解决问题。
更新对您所遇到的错误的更多解释。
当您使用#include
时,它将在编译之前由处理器处理(将其视为将.h
文件内联到源文件中)。每个编译单元都会发生此过程,即.cpp
文件(将其视为树的根)。
现在,由于包含树的“根”不同(A.cpp
,B.cpp
,...),包括后卫将无法帮助您避免功能实现从内联到每个编译单元。
这会导致链接错误,因为这些函数有多个实现。
答案 1 :(得分:2)
每个编译单元(即A.cpp,B.cpp和C.cpp)都定义了functions.h
中包含的每个函数。
包含守卫对此没有帮助,因为它们恰好在编译单元边界内有效(即如果你直接或间接地包括functions.h
两次,比如A.cpp)< / p>
可能的解决方案是:
functions.h
中声明了这些函数,并在一个单独的编译单元中定义它们,比如functions.cpp
(这是你的例子中的方法)functions.h
模板函数中创建了每个函数(只有在有意义时才这样做)functions.h
中的每一项功能都是静态的(这只是一种解决方法,我绝不建议你这样做!) P.S。关于第3点。这可能是有道理的,所以你可能想知道static
是什么。当引用自由函数时,static
关键字将此函数标记为编译单元的本地函数。因此,该函数不会在链接表中导出,也不能从另一个编译单元中调用。这就是为什么“技巧”有效,而且你仍然有多个定义,它们都是每个编译单元的“私有”,并且没有可能的冲突。当您使用不属于“接口”的自由函数时,这尤其有用,并且只是本地帮助函数。