Reputation: 13
I keep getting a function redefinition error, and I'm not sure why. I've looked through similar questions, but I couldn't glean any answers from them -- I've got header guards, and as far as I can tell, I have included the correct files and haven't redefined the functions outside of the header. The errors are as follows:
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
The header and relevant chunk of the CPP file are below. Any help is much appreciated.
Header:
#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;
}
Upvotes: 0
Views: 1320
Reputation: 3017
In your header file you've declared
Contact(const Contact& other) = delete;
Contact& operator=(const Contact& other) = delete;
as delete
and then in your source (cpp) file you are trying to implement them which is an unexpected behaviour by the compiler. You should either remove the delete
keyword in your header file, or remove their implementations from your source (cpp) file.
Upvotes: 1