C++ C++;函数重新定义
我一直收到一个函数重新定义错误,我不知道为什么。我看过类似的问题,但我无法从中找到任何答案——我有标题保护,据我所知,我已经包含了正确的文件,并且没有重新定义标题之外的函数。错误如下:C++ C++;函数重新定义,c++,c++11,C++,C++11,我一直收到一个函数重新定义错误,我不知道为什么。我看过类似的问题,但我无法从中找到任何答案——我有标题保护,据我所知,我已经包含了正确的文件,并且没有重新定义标题之外的函数。错误如下: Contact.cpp:188:2: error: redefinition of ‘communication::Contact::Contact(const communication::Contact&)’ Contact.h:35:3: error: ‘communication::Contact
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:
//复制构造函数
联系人::联系人(常数联系人和其他){
strcpy(m_名称,其他.m_名称);
m_noOfPhoneNumbers=其他.m_noOfPhoneNumbers;
//检查要复制的数组是否为空
如果(其他m_电话号码!=nullptr){
//为m_PhoneNumber分配适当的内存量
m_phoneNumbers=新长长[m_noOfPhoneNumbers];
//从other.m_Phon将电话号码从other.m_Phon复制到this.m_PhoneNumber$
对于(int i=0;i
在您声明的头文件中
Contact(const Contact& other) = delete;
Contact& operator=(const Contact& other) = delete;
当
delete
时,然后在源(cpp)文件中尝试实现它们,这是编译器的意外行为。您应该删除头文件中的delete
关键字,或者从源(cpp)文件中删除它们的实现。您已经将它们标记为delete,这意味着编译器不会期望它们。这有什么原因吗?为什么要删除这些函数?另外,请使用std::string,而不是char数组。我很乐意,但这是用于赋值的,我们还不允许使用字符串。类似地,我们不应该修改标题中给出的内容——因此需要删除。根据我的理解,这是为了保持客户端代码复制/分配到类的实例。@如果这些函数实际上应该被删除,就不要实现它们。
Contact(const Contact& other) = delete;
Contact& operator=(const Contact& other) = delete;