我有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 *)" ([email protected]@[email protected]) 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);
}
你能用真實的代碼展示一個最簡單的例子嗎? – juanchopanza
@juanchopanza我加了。 –
你爲什麼要在Sentence.cpp中定義Word :: getLen()? –