This is my full code:
"String.h"
Code:
#ifndef STRING_H
#define STRING_H
#include "List.h"
class String{
public:
String();
~String();
String(const String&);
String(const char*);
String(List<char> ©);
String& operator=(const String ©);
const char* c_str() const;
protected:
char * entry;
int length;
};
static bool operator==(const String &first, const String &second);
static bool operator>(const String &first, const String &second);
static bool operator<(const String &first, const String &second);
static bool operator>=(const String &first, const String &second);
static bool operator<=(const String &first, const String &second);
#endif
"String.cpp"
Code:
#ifndef STRING_CPP
#define STRING_CPP
#include "String.h"
#include <string>
using std::string;
String::String(){
// entry = NULL;
length = 0;
}
String::~String(){
delete entry;
}
String::String(const String &string1 ){
length = strlen(string1.c_str());
entry = new char[length+1];
for (int i=0;i<length;i++)
entry[i] = string1.c_str()[i];
entry[length] = '\0';
}
String& String::operator=(const String ©){
length = strlen(copy.c_str());
entry = new char[length+1];
for (int i=0;i<length;i++)
entry[i] = copy.c_str()[i];
entry[length] = '\0';
return (*this);
}
String::String(const char * CharToString){
length = strlen(CharToString);
entry = new char[length+1];
strcpy_s(entry,length, CharToString);
}
String::String(List<char> &ListChar){
length = ListChar.Size();
entry = new char[length+1];
for (int i=i;i<length;i++)
ListChar.Retrieve(entry[i],i);
entry[length] = '\0';
}
const char * String ::c_str() const{
return (const char*) entry;
}
static bool operator==(const String &first, const String &second){
return strcmp(first.c_str(),second.c_str()) == 0;
}
static bool operator <(const String &first, const String &second){
return (strcmp(first.c_str(),second.c_str()) < 0);
}
static bool operator >(const String &first, const String &second){
return (strcmp(first.c_str(),second.c_str()) > 0);
}
static bool operator <=(const String &first, const String &second){
return (strcmp(first.c_str(),second.c_str()) <= 0);
}
static bool operator >=(const String &first, const String &second){
return (strcmp(first.c_str(),second.c_str()) >= 0);
}
#endif
"UseString.cpp"
Code:
#include "stdafx.h"
#include "List.cpp"
#include "String.cpp"
int _tmain(int argc, _TCHAR* argv[])
{
String stringone="abcde";
String stringtwo = "1234";
stringone = stringtwo;
return 0;
}
Compiler informs errors that:
error LNK2005: "public: __thiscall String::String(void)" (??0String@@QAE@XZ) already defined in UseString.obj
1>String.obj : error LNK2005: "public: __thiscall String::~String(void)" (??1String@@QAE@XZ) already defined in UseString.obj
1>String.obj : error LNK2005: "public: __thiscall String::String(class String const &)" (??0String@@QAE@ABV0@@Z) already defined in UseString.obj
1>String.obj : error LNK2005: "public: class String & __thiscall String:perator=(class String const &)" (??4String@@QAEAAV0@ABV0@@Z) already defined in UseString.obj
1>String.obj : error LNK2005: "public: __thiscall String::String(char const *)" (??0String@@QAE@PBD@Z) already defined in UseString.obj
1>String.obj : error LNK2005: "public: __thiscall String::String(class List<char> &)" (??0String@@QAE@AAV?$List@D@@@Z) already defined in UseString.obj
1>String.obj : error LNK2005: "public: char const * __thiscall String::c_str(void)const " (?c_str@String@@QBEPBDXZ) already defined in UseString.obj
1>E:\School\UseString\Debug\UseString.exe : fatal error LNK1169: one or more multiply defined symbols found