因此,作为大学项目的一部分,有人要求我创建一个包含变量的Email类,该变量是Attachment对象的容器。问题是每次我尝试调试项目时,都会给我一个错误“附件* const”:大小未知。我知道我已经在Email类中声明了Attachment类,但是如果我将其替换为include“ Attachment.h”,它会给我更多错误,提示它找不到Attachment并且未声明。向量中没有指针,因为它也给出了很多错误。这是代码:
Email.h
#pragma once
class Attachment;
class Email
{
private:
std::string sender;
std::string recipients;
std::string dateTime;
std::string subject;
std::string body;
std::vector<Attachment> attVec;
public:
Email();
Email(std::string sender, std::string recipients, std::string dateTime,
std::string subject, std::string body, std::vector<Attachment> attVec);
~Email();
bool validate(std::string regexStr, std::string data);
std::string getSender() { return this->sender; }
std::string getRecipients() { return this->recipients; }
std::string getDateTime() { return this->dateTime; }
std::string getSubject() { return this->subject; }
std::string getBody() { return this->body; }
std::vector<Attachment>getAttVec() { return this->attVec; }
void setSender(std::string sender);
void setRecipient(std::string recipients);
//Must change
void setDateTime(std::string dateTime) { this->dateTime = dateTime; }
void setSubject(std::string subject) { this->subject = subject.size()
>= 0; }
void setBody(std::string body) { this->body = body.size() >= 1; }
void setAttVec(std::vector<Attachment>getAttVec) { this->attVec.size()
>= 1; }
};
Attachment.h
#pragma once
class Attachment
{
private:
std::string fileName;
std::string fileSuffix;
const char* fileData;
public:
Attachment();
Attachment(std::string fileName, std::string fileSuffix, const char*
fileData);
~Attachment();
bool validate(std::string regexStr, std::string data);
std::string getFileName() { return this->fileName; }
std::string getFileSuffix() { return this->fileSuffix; }
const char* getFileData() { return this->fileData; }
void setFileName(std::string fileName);
void setFileSuffix(std::string fileSuffix);
void setFileData(const char* fileData) { this->fileData != NULL; }
};
答案 0 :(得分:0)
错误被Email.h
文件的最后一行抛出
void setAttVec(std::vector<Attachment>getAttVec) { this->attVec.size() >= 1; }
要解决此问题,您必须在Attachment
中包含类#include "Attachment.h"
,现在.h
文件中不建议这样做,因为它可能导致循环依赖,这就是为什么应该使用forward声明,就像你一样。问题在于Email
类仍然不知道类Attachment
的对象可以做什么。
您有两个选择,如果您确定没有循环依赖项,则可以将include语句放在Email.h
文件中;如果没有(通常是最好的选择),则应该做所有的“工作”(访问Attachment
文件中Email.cpp
类的对象上访问该类的任何对象,在那里可以将#include "Attachment.h"
放到没有问题的位置。
我了解到,也许您认为您不需要拥有.cpp
文件,因为这些方法足够简单(至少我们看到的方法),而只需一个{{1} }文件,您必须确保没有循环依赖关系,否则,如果将它们拆分,将使您的生活更轻松,并且该类将变得更有条理(如果变得更加复杂)。
因此,我已经测试了您的代码,并且能够创建.h
类和Email
类之一的对象,该代码可以编译并很好地运行,因此问题是与其余代码冲突,这里是一个链接,您可以在其中检查代码的运行情况。 Email Project
我仍然需要对您的代码进行更改以使其正常工作,我不知道您是否有Attachment
文件,如果您执行了这两个操作,则可能不需要这样做。
Attachment.cpp
构造函数和析构函数没有主体(如果Attachment(std::string fileName, std::string fileSuffix, const char*
fileData);
~Attachment();
文件上有主体,则认为该主体是一致的,并将每个函数的主体放在那里)。
.cpp
在您的Attachment(std::string fileName, std::string fileSuffix, const char* fileData)
{
this->fileName = fileName;
this->fileSuffix = fileSuffix;
this->fileData = fileData;
}
~Attachment(){};
文件上,我必须添加三个包含项,Email.h
,#include <string>
和#include <vector>
(也许您在其他地方添加了,但是如果没有,需要它们,将它们包含在所需的每个文件中是一个好主意,因为如果以后由于不需要它们而从它们所在的位置删除它们,则该类会产生错误。
在#include <regex>
文件上,您具有以下功能函数,实际上该功能什么也没做(也许只是为了进行某种测试)。
Email.cpp