0
我不斷收到函數重定義錯誤,我不知道爲什麼。我已經看過類似的問題,但我無法從他們那裏收集任何答案 - 我有頭衛兵,據我所知,我已經包含了正確的文件,並沒有重新定義標題。錯誤如下:C++函數重定義
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;
}
您已將它們標記爲刪除,這意味着編譯器不會期望它們。是否有一個原因? –
你爲什麼刪除這些功能? –
另外,使用std :: string,而不是char數組。 –