I have read about these two errors pretty extensively but despite all of my efforts I can not see the problem here. I have absolutely flooded my code with header guards to see if that will help, with no change in the result. here are each of the files: Mystring.cpp
#pragma once
#include "MyString.h"
#ifndef MYSTRING_CPP
#define MYSTRING_CPP
using namespace std;
MyString::MyString(char chars[]){
str = (char*) malloc(strlen(chars));
str = chars;
}
//etc, other methods as defined in header file
#endif
Mystring.h
#pragma once
#include <string.h>
#include <iostream>
#ifndef MYSTRING_H
#define MYSTRING_H
using namespace std;
class MyString {
protected:
char * str;
public:
~MyString();
MyString(char chars[]);
MyString(const char chars[]);
MyString();
int length();
void resize(int n, char c);
void clear();
bool empty();
void insert(int pos1, const MyString& str);
void insert (int pos1, const char* s);
void erase (int pos, int n);
int find(const MyString& str);
int find(const char* s);
int compare(const MyString& str);
int compare(const char* s);
const char* getStr() const;
MyString::MyString(const MyString &that);
MyString & operator=(const char chars[]);
MyString & operator=(const MyString& str);
};
#endif
MystringDerived.h
#pragma once
#include "MyString.cpp"
#ifndef MYSTRINGDERIVED_H
#define MYSTRINGDERIVED_H
class MyStringDerived :
public MyString
{
public:
MyStringDerived(char chars[]);
bool operator>(MyString rhs);
bool operator>=(MyString rhs);
bool operator<(MyString rhs);
bool operator<=(MyString rhs);
bool operator==(MyString rhs);
bool operator!=(MyString rhs);
bool operator+(MyString rhs);
bool operator[](MyString rhs);
MyStringDerived(void);
~MyStringDerived(void);
};
#endif
MyStringDerived.cpp
#pragma once
#include "MyStringDerived.h"
#ifndef MYSTRINGDERIVED_CPP
#define MYSTRINGDERIVED_CPP
MyStringDerived::MyStringDerived(char * const)
{
}
MyStringDerived::~MyStringDerived(void)
{
}
#endif
and the errors I get
1>test.obj : error LNK2005: "public: int __thiscall MyString::length(void)" (?length@MyString@@QAEHXZ) already defined in MyString.obj
1>test.obj : error LNK2005: "public: void __thiscall MyString::resize(int,char)" (?resize@MyString@@QAEXHD@Z) already defined in MyString.obj
1>C:\Users\Arthur\Documents\Visual Studio 2012\Projects\Project1\Debug\Project2.exe : fatal error LNK1169: one or more multiply defined symbols found
There are hundreds of error lines that all look like this. With the way I organized my includes, I definitely don't see how I might be ending up with multiple copies being included. Is there something else I'm missing? Any insights are appreciated. This is homework (obviously), so I definitely want to actually understand the problem when I'm done here. No one I've shown it to has caught what i'm doing wrong.
Thanks!
Forgot to include test.cpp. it's as simple as this at the moment :
MyStringDerived m = "test"; cout<< m.getStr(); getchar(); return 0;