C ++函数重定义

时间:2017-03-03 00:38:56

标签: c++ c++11

我不断收到函数重定义错误,我不确定原因。我已经查看了类似的问题,但我无法从他们那里收到任何答案 - 我已经有了头部警卫,据我所知,我已经包含了正确的文件和避难所&# 39;重新定义标题之外的函数。错误如下:

Contact.cpp:188:2: error: redefinition of ‘communication::Contact::Contact(const communication::Contact&)’
Contact.h:35:3: error: ‘communication::Contact::Contact(const communication::Contact&)’ previously defined here
Contact.cpp:208:18: error: redefinition of ‘communication::Contact& communication::Contact::operator=(const communication::Contact&)’
Contact.h:36:12: error: ‘communication::Contact& communication::Contact::operator=(const communication::Contact&)’ previously defined here

CPP文件的标题和相关块位于下方。非常感谢任何帮助。

部首:

#ifndef CONTACT_H
#define CONTACT_H

namespace communication {
        class Contact
        {
                char m_name[21];
                long long* m_phoneNumbers;
                int m_noOfPhoneNumbers;
        public:
                Contact();
                Contact (const char * c_name, long long contactList[], int numContacts);
                ~Contact();
                void display() const;
                bool isEmpty() const;
                void addPhoneNumber(long long phoneNumber);

                //Definitions of the functions causing problems
                Contact(const Contact& other) = delete;
                Contact& operator=(const Contact& other) = delete;
        };
}

#endif

CPP:

//Copy constructor
        Contact::Contact (const Contact& other) {
                strcpy(m_name,other.m_name);
                m_noOfPhoneNumbers = other.m_noOfPhoneNumbers;

                //Checks to see if the array being copied is empty
                if (other.m_phoneNumbers != nullptr) {
                        //Allocates the appropriate amount of memory to m_phoneNumbers
                        m_phoneNumbers = new long long[m_noOfPhoneNumbers];

                        //Goes through and copies the phone numbers to this.m_phoneNumbers from other.m_Phon$
                        for (int i = 0; i < m_noOfPhoneNumbers; i++) {
                                m_phoneNumbers[i] = other.m_phoneNumbers[i];  
                        }
                }
                else {
                        m_phoneNumbers = nullptr;
                }
        }
        //Copy assignment operator
        Contact& Contact::operator=(const Contact& other) {
                //Checks to make sure that the object is not being assigned to itself
                if (this != &other) {
                        //Copies name and number of phone numbers to the left operand
                        strcpy(m_name,other.m_name);
                        m_noOfPhoneNumbers = other.m_noOfPhoneNumbers;

                        //Deallocates memory that may have been given previously
                        delete [] m_phoneNumbers;

                        //Checks to see if the array being copied is empty
                        if (other.m_phoneNumbers != nullptr) {
                                //Allocates the appropriate amount of memory to m_phoneNumbers
                                m_phoneNumbers = new long long[m_noOfPhoneNumbers];

                                //Goes through and copies the phone numbers to this.m_phoneNumbers from other.m_PhoneNumbers
                                for (int i = 0; i < m_noOfPhoneNumbers; i++) {
                                      m_phoneNumbers[i] = other.m_phoneNumbers[i];
                                }

                        }
                        else {
                                m_phoneNumbers = nullptr;
                        }
                }

                return *this;
        }

1 个答案:

答案 0 :(得分:1)

在您的头文件中,您已声明

   Country1 Country2    year
          A   B         1993
          A   B         1994
          A   C         1993
          A   C         1994
          B   A         1993
          B   A         1994
          B   C         1993 
          B   C         1994

作为Contact(const Contact& other) = delete; Contact& operator=(const Contact& other) = delete; ,然后在源(cpp)文件中,您尝试实现它们,这是编译器的意外行为。您应该删除头文件中的delete关键字,或者从源(cpp)文件中删除它们的实现。