gpt4 book ai didi

c++ - '错误 : expected a type' creating a new instance

转载 作者:太空狗 更新时间:2023-10-29 23:05:23 24 4
gpt4 key购买 nike

我有一个问题,我希望你能帮助我。我正在创建一个在 linux 上运行的 C++ 程序。我定义了两个类,其中主要的类称为 Downloader,如下所示:

 #ifndef __DOWNLOADER_H__
#define __DOWNLOADER_H__
#include "configDownloader.h"
#include "mailServer.h"
#include <logger.h>

using namespace ObjectModel;

namespace Downloader
{
class Downloader
{
private:
...
MailServer *m_mailServer;

public:
Downloader(char* configFileName);
...
};
}
#endif

在这个类的构造函数中,我试图创建类 MailServer 的一个新实例,我将其定义到同一个命名空间中。 MailServer 的代码看起来是这样的:

#ifndef __MAILSERVER_H__
#define __MAILSERVER_H__

#include <stdio.h>
#include <list>
#include <mail.h>

using namespace std;
using namespace ObjectModel;

namespace Downloader
{
class MailServer
{
private:
list<Mail> m_mails;
char *m_username;
char *m_password;

public:
MailServer();
~MailServer();

void Open(char *username,char *password);
bool SaveEmails(char *pathFiles);
void Close();
};
}
#endif

此类的构造函数已正确定义到 .cpp 文件中,一切似乎都是正确的。问题是,当我尝试在 Downloader 的构造函数中创建一个新的 MailServer 实例时,编译器显示 "error: expected a type"

#include <stdio.h>
#include "downloader.h"
#include <unistd.h>

namespace Downloader
{
Downloader::Downloader(char* fileName)
{
this->m_config = new ConfigDownloader(fileName);
this->m_log = new Logger("Log",LOG_LEVEL_INFO,0);
this->m_mailServer = new MailServer();//the compiler shows the error right here
}
...

有什么想法吗?我在某处读到,可能是编译器太旧了,但我觉得在 makefile 中编码并不是很舒服。

最佳答案

我找到了解决方案!对不起,因为我没看到。问题是我定义了一个公共(public) getter 来返回私有(private)字段,如下所示:

class Downloader
{
private:
ConfigDownloader* m_config;
Logger* m_log;
MailServer *m_mailServer;

public:
MailServer *MailServer();

因为两者都定义在同一个作用域中,编译器可能会对类的构造函数和方法产生混淆,因为它们都是用相同的名称调用的。问题是我的!但是每个人都应该注意这一点,因为智能感知不会告诉你任何关于它的信息

关于c++ - '错误 : expected a type' creating a new instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19791789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com