调试对象上的LNK2005和LNK1169

时间:2012-10-20 01:21:19

标签: c++ lnk2005

我已经非常广泛地阅读了这两个错误,但尽管我付出了很多努力,但我还是看不到这个问题。我绝对用代码保护程序充斥我的代码,看看是否有帮助,结果没有变化。这里是每个文件: 的 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

和我得到的错误

    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

有数百条错误行都是这样的。通过我组织我的包含的方式,我绝对不会看到我最终会包含多个副本。还有什么我想念的吗?任何见解都表示赞赏。这是功课(很明显),所以当我在这里完成时,我当然希望真正理解这个问题。没有人我已经证明它已经抓住了我做错了。

谢谢!

忘记包含test.cpp。现在就这么简单:

  

MyStringDerived m =“test”; COUT&LT;&LT; m.getStr();的getchar();返回   0;

1 个答案:

答案 0 :(得分:2)

我认为问题出在MyStringDerived.h的第2行。如果要包含MyString.h

,则包括MyString.cpp

[更新]

你编码的方式,MyString.cpp的所有定义都被编译到MyString.obj文件中 - 这很好,应该是这样。不幸的是,由于你的#include,MyString.cpp的所有定义也出现在MyDerivedString.obj中

要真正理解为什么这是一个问题,你需要花一些时间研究声明和定义之间的区别 - 只需谷歌“c ++声明与定义”。这篇文章可能会有所帮助。 http://www.cprogramming.com/declare_vs_define.html

任何“声明”的内容都可以一次又一次地包含在程序中而不会造成任何伤害。所定义的任何内容必须在链接到应用程序的所有目标文件中出现一次且仅一次。

这导致以下一般指导原则:

头文件(.h)必须只包含声明。它不得包含任何定义。只有.cpp文件可以包含定义。然后你必须确保永远不要#include .cpp文件。它是确保您在链接时永远不会有重复定义的最简单方法。