![]() |
#2
Jonny02012018-05-14 22:13
|

#ifndef Str_my_H
#define Str_my_H
#define Maxsize 20
class Str_my{
private:
char *data;
public:
Str_my();
Str_my(char a[],int n);
~Str_my();
int StrLen();
char * StrCat();
int StrCmp();
};
#endif
#define Str_my_H
#define Maxsize 20
class Str_my{
private:
char *data;
public:
Str_my();
Str_my(char a[],int n);
~Str_my();
int StrLen();
char * StrCat();
int StrCmp();
};
#endif
---------------源文件Str_my.cpp------------

#include<iostream>
#include<stdlib.h>
#include"Str_my.h"
using namespace std;
Str_my::Str_my(){
data=(char * )malloc(sizeof(char)*Maxsize);
}
Str_my::Str_my(char a[],int n){
data=(char * )malloc(sizeof(char)*Maxsize);
for(int i=0;i<n;i++){
*(data+i)=a[i];
}
*(data+n)='\0';
}
Str_my::~Str_my(){
if(data!=NULL) free(data);
}
#include<stdlib.h>
#include"Str_my.h"
using namespace std;
Str_my::Str_my(){
data=(char * )malloc(sizeof(char)*Maxsize);
}
Str_my::Str_my(char a[],int n){
data=(char * )malloc(sizeof(char)*Maxsize);
for(int i=0;i<n;i++){
*(data+i)=a[i];
}
*(data+n)='\0';
}
Str_my::~Str_my(){
if(data!=NULL) free(data);
}
--------------主函数源程序Str_my_main.cpp------------

#include<iostream>
#include<stdlib.h>
#include"Str_my.h"
#include"Str_my.cpp"
using namespace std;
int main(){
Str_my S1;
char a[4]={'C','J','L','!'};
Str_my S2(a,4);
return 0;
}
#include<stdlib.h>
#include"Str_my.h"
#include"Str_my.cpp"
using namespace std;
int main(){
Str_my S1;
char a[4]={'C','J','L','!'};
Str_my S2(a,4);
return 0;
}
错误提示在下面

Deleting intermediate files and output files for project '字符串的实现 - Win32 Debug'.
--------------------Configuration: 字符串的实现 - Win32 Debug--------------------
Compiling...
Str_my.cpp
Str_my_main.cpp
Linking...
Str_my_main.obj : error LNK2005: "public: __thiscall Str_my::Str_my(void)" (??0Str_my@@QAE@XZ) already defined in Str_my.obj
Str_my_main.obj : error LNK2005: "public: __thiscall Str_my::Str_my(char * const,int)" (??0Str_my@@QAE@QADH@Z) already defined in Str_my.obj
Str_my_main.obj : error LNK2005: "public: __thiscall Str_my::~Str_my(void)" (??1Str_my@@QAE@XZ) already defined in Str_my.obj
Debug/字符串的实现.exe : fatal error LNK1169: one or more multiply defined symbols found
执行 link.exe 时出错.
字符串的实现.exe - 1 error(s), 0 warning(s)
--------------------Configuration: 字符串的实现 - Win32 Debug--------------------
Compiling...
Str_my.cpp
Str_my_main.cpp
Linking...
Str_my_main.obj : error LNK2005: "public: __thiscall Str_my::Str_my(void)" (??0Str_my@@QAE@XZ) already defined in Str_my.obj
Str_my_main.obj : error LNK2005: "public: __thiscall Str_my::Str_my(char * const,int)" (??0Str_my@@QAE@QADH@Z) already defined in Str_my.obj
Str_my_main.obj : error LNK2005: "public: __thiscall Str_my::~Str_my(void)" (??1Str_my@@QAE@XZ) already defined in Str_my.obj
Debug/字符串的实现.exe : fatal error LNK1169: one or more multiply defined symbols found
执行 link.exe 时出错.
字符串的实现.exe - 1 error(s), 0 warning(s)
太久没摸代码了,请指导,在线等~