Pergunta

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;

Foi útil?

Solução

I think the problem is on line 2 of MyStringDerived.h. You are including MyString.cpp when you should be including MyString.h

[update]

The way you have it coded, all the definitions of MyString.cpp are compiled into the MyString.obj file - this is good and as it should be. Unfortunately, because of your #include, all of the definitions of MyString.cpp also appear in MyDerivedString.obj

To really understand why this is a problem, you need to take some time and study the difference between a declaration and a definition - just google "c++ declaration vs definition". Here is an article that may be helpful. http://www.cprogramming.com/declare_vs_define.html

Anything that is "declared" can be included again and again into a program without any harm. Anything that is defined must appear once and only once across all object files that are linked into the application.

This leads to the following general guidelines:

A header file (.h) must only contain declarations. It must not contain any definitions. Only .cpp files can contain definitions. Then you must make sure to never to #include a .cpp file. Its the easiest way to ensure that you will never have duplicate definitions at link time.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top