我有 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
同样的错误是 subs(function)。
有什么建议为什么会发生?
谢谢!
编辑:
 
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
单词.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);
}