复制构造函数和链接错误

时间:2014-09-26 17:31:16

标签: c++ visual-studio-2013 copy-constructor linker-errors default-copy-constructor

我们来看一下这段代码:

class MyClass
{
public:
    int m_num;
    MyClass(int n) { m_num = n;}
}

如果我按以下方式创建此类的对象:

MyClass mc1(5);
MyClass mc2(mc1);

这会调用默认的copy-constructor,它会自动分配mc2.m_num = mc1.m_num? 此外,如果在类中进行调用,则以下列方式创建实例:

MyClass mc3(*this);

这将调用与mc2相同的默认复制构造函数*这只是让对象本身被复制?

我遇到的第二个问题是使用Microsoft Visual Studio 2013编译我的代码。 我已经创建了一个String类,当我尝试编译时,我得到许多链接错误,告诉我函数已经由于某种原因已经定义了。 String.h:

#pragma once
class String
{
private:
    char* m_szStr;
    unsigned int m_length;
public:
    String(const char*);
    explicit String(unsigned int);
    String(const String&);
    ~String();
    String& operator=(const String&);
    bool operator==(const String&) const;
    String operator+(const String&) const;
    operator const char*() const;
    int findStr(char*) const;
    int getLen() const;
    void copyStr(const char*);

};

String.cpp:

#include "stdafx.h"
#include "String.h"

String::String(const char* pch)
{
    m_szStr = NULL;
    copyStr(pch);
}

String::String(unsigned int len)
{
    m_length = len;
    m_szStr = new char[m_length];
}

String::String(const String& that)
{
    copyStr(that.m_szStr);
}

String::~String()
{
    delete[] m_szStr;
}

String& String::operator=(const String& that)
{
    copyStr(that.m_szStr);
    return *this;
}

bool String::operator==(const String& that) const
{

    if (m_length != that.m_length)
        return false;
    for (unsigned int i = 0; i < m_length; i++)
    {
        if (m_szStr[i] != that.m_szStr[i])
            return false;
    }
    return true;
}

String String::operator+(const String& that) const
{
    String temp(m_length + that.m_length - 1);
    unsigned int offset = m_length;
    for (unsigned int i = 0; i < that.m_length; i++)
    {
        temp.m_szStr[offset] = that.m_szStr[i];
    }
    return temp;
}

String::operator const char*() const
{
    return m_szStr;
}

int String::findStr(char* pch) const
{
    unsigned int offset = 0;
    unsigned int strIndex = -1;
    for (unsigned int i = 0; m_szStr[i] != NULL && pch[offset] != NULL; i++)
    {
        if (m_szStr[i] == pch[offset])
        {
            if (strIndex == -1)
                strIndex = i;
            offset++;
        }
        else
        {
            strIndex = -1;
            offset = 0;
        }
    }
    return strIndex;
}

int String::getLen() const
{
    unsigned int len = 0;
    for (unsigned int i = 0; m_szStr[i] != NULL; i++)
    {
        len++;
    }
    return len;
}

void String::copyStr(const char* pch)
{
    if (!m_szStr)
        delete[] m_szStr;
    unsigned int pchLen = 0;
    for (unsigned int i = 0; pch[i] != NULL; i++)
    {
        pchLen++;
    }
    m_length = pchLen;
    m_szStr = new char[m_length];
    for (unsigned int i = 0; i < m_length; i++)
    {
        m_szStr[i] = pch[i];
    }
}

代码文件:

#include "stdafx.h"
#include <iostream>
#include "String.cpp"

using namespace std;


int _tmain(int argc, _TCHAR* argv[])s
{
    String s1("Hi!");
    String s2(5);
    s2 = "Hello, Hi!";
    const char* pch = static_cast<const char*>(s2);
    cout << pch << endl;
    return 0;
}

错误:

Error   1   error LNK2005: "public: __thiscall String::String(class String const &)" (??0String@@QAE@ABV0@@Z) already defined in assignment4.obj    C:\Users\****\Desktop\C++ Programming\assignment4\assignment4\String.obj    assignment4
Error   2   error LNK2005: "public: __thiscall String::String(unsigned int)" (??0String@@QAE@I@Z) already defined in assignment4.obj    C:\Users\****\Desktop\C++ Programming\assignment4\assignment4\String.obj    assignment4
Error   3   error LNK2005: "public: __thiscall String::String(char const *)" (??0String@@QAE@PBD@Z) already defined in assignment4.obj  C:\Users\****\Desktop\C++ Programming\assignment4\assignment4\String.obj    assignment4
Error   4   error LNK2005: "public: __thiscall String::~String(void)" (??1String@@QAE@XZ) already defined in assignment4.obj    C:\Users\****\Desktop\C++ Programming\assignment4\assignment4\String.obj    assignment4
Error   5   error LNK2005: "public: class String & __thiscall String::operator=(class String const &)" (??4String@@QAEAAV0@ABV0@@Z) already defined in assignment4.obj  C:\Users\****\Desktop\C++ Programming\assignment4\assignment4\String.obj    assignment4
Error   6   error LNK2005: "public: bool __thiscall String::operator==(class String const &)const " (??8String@@QBE_NABV0@@Z) already defined in assignment4.obj    C:\Users\****\Desktop\C++ Programming\assignment4\assignment4\String.obj    assignment4
Error   7   error LNK2005: "public: __thiscall String::operator char const *(void)const " (??BString@@QBEPBDXZ) already defined in assignment4.obj  C:\Users\****\Desktop\C++ Programming\assignment4\assignment4\String.obj    assignment4
Error   8   error LNK2005: "public: class String __thiscall String::operator+(class String const &)const " (??HString@@QBE?AV0@ABV0@@Z) already defined in assignment4.obj  C:\Users\****\Desktop\C++ Programming\assignment4\assignment4\String.obj    assignment4
Error   9   error LNK2005: "public: void __thiscall String::copyStr(char const *)" (?copyStr@String@@QAEXPBD@Z) already defined in assignment4.obj  C:\Users\****\Desktop\C++ Programming\assignment4\assignment4\String.obj    assignment4
Error   10  error LNK2005: "public: int __thiscall String::findStr(char *)const " (?findStr@String@@QBEHPAD@Z) already defined in assignment4.obj   C:\Users\****\Desktop\C++ Programming\assignment4\assignment4\String.obj    assignment4
Error   11  error LNK2005: "public: int __thiscall String::getLen(void)const " (?getLen@String@@QBEHXZ) already defined in assignment4.obj  C:\Users\****\Desktop\C++ Programming\assignment4\assignment4\String.obj    assignment4
Error   12  error LNK1169: one or more multiply defined symbols found   C:\Users\****\Desktop\C++ Programming\assignment4\Debug\assignment4.exe 1   1   assignment4

1 个答案:

答案 0 :(得分:2)

在主代码文件中,写下:

#include "String.h"   // with .cpp you redefine everything !! 

目前您包含String.cpp。因此,在主文件中,您将重新定义已在String.cpp中定义的所有内容。它将编译,但链接器将被混淆,因为他将在String.obj和main.obj(“一个定义规则”)中找到String成员函数的定义。

你已经回答了自己的复制构造函数问题