在多个类中使用外部函数时出错

getting error by using outer functions in multiple classes

本文关键字:外部 函数 出错      更新时间:2023-10-16

我有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.cppB.cpp…),include-guard无法帮助您避免函数实现内联到每个编译单元中。

这将导致链接错误,因为这些函数有多个实现。

每个编译单元(即A.cpp、B.cpp和C.cpp)都有包含在functions.h中的每个函数的定义。

Include保护程序对此没有帮助,因为它们只在编译单元边界内发生作用(即,如果您在例如A.cpp中直接或间接包含functions.h两次)

可能的解决方案有:

  1. functions.h中声明函数,并在一个单独的编译单元中定义它们,比如functions.cpp(这是示例中的方法)
  2. 您使functions.h中的每个函数都成为模板函数(只有在有意义的情况下才这样做)
  3. 您声明functions.h中的每个函数都是静态的(这只是一个变通方法,我决不建议您这样做!!)

关于第3点的附言。这可能是有意义的,所以你可能想知道static是关于什么的。当引用自由函数时,static关键字将此函数标记为编译单元的本地函数。因此,该函数将不会在链接表中导出,也无法从另一个编译单元中调用。这就是为什么"技巧"有效的原因,尽管你仍然有多个定义,它们对每个编译单元都是"私有的",并且没有可能的冲突。当您使用不是"接口"的一部分并且仅是本地辅助函数的自由函数时,这尤其有用。