0

이의이 조각 코드를 보자복사 생성자와 연결 오류

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

이 기본 복사 생성자 호출 자동으로 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 &)" ([email protected]@[email protected]@@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)" ([email protected]@[email protected]@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 *)" ([email protected]@[email protected]@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)" ([email protected]@[email protected]) 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 &)" ([email protected]@[email protected]@@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 " ([email protected]@[email protected]@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 " ([email protected]@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 " ([email protected]@[email protected]@@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 *)" ([email protected]@@[email protected]) 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 " ([email protected]@@[email protected]) 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 " ([email protected]@@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 
+0

@ πάνταῥεῖ done을 답장하셨습니다. –

+0

'String.cpp' 파일을 실행 파일에 연결 했습니까? –

+0

가능한 복제본 : [정의되지 않은 참조/확인되지 않은 외부 기호 오류 란 무엇이며 어떻게 수정합니까?] (http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external -symbol-error-and-how-do-i-fix) –

답변

2

는 쓰기 :

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

현재는 String.cpp입니다. 그래서 주 파일에서 이미 String.cpp에 정의한 모든 것을 다시 정의 할 것입니다. 컴파일 될 것이지만 링커는 String.obj와 main.obj ("하나의 정의 규칙")에서 String 멤버 함수에 대한 정의를 찾을 것이기 때문에 혼란스러워 할 것입니다.

그리고 이미 복사 생성자 질문

+0

하지만 왜 .cpp가 아니라 String.h를 포함해야합니까? .cpp는 정의를 포함하고 .cpp는 구현이므로 .h 파일 만 포함한다면 정의 만 포함 할 수 있습니까? –

+1

@ Tugal.44 다른 .cpp 파일의 컴파일 된 출력을 함께 결합하는 "연결"프로세스가 있습니다. 그것을'#include '할 때, 두 개의 사본을 얻는다. –

+0

String.h에는 선언이 포함되어 있기 때문입니다. 이것들은 컴파일러가 코드를 생성하기에 충분하며 링커는 sring.cpp 컴파일 결과와 연결됩니다. – Christophe