繁体   English   中英

错误:未在此范围内使用C ++声明

[英]error: was not declared in this scope with C++

我正在尝试实现一种用于发送电子邮件的简单协议。 直到现在,我实现了四个命令和一个服务器类,该服务器类接收所有命令并检查命令的顺序是否正确。 但是,当我创建服务器类的实例时,它显示了一个错误:在此范围内未声明SMTPServer,我不知道该怎么办。 感谢您提供任何帮助,因为如果不解决此错误,我将无法完成程序。

SMTPServer头文件:

#include <string>
#include <iostream>
#include "HELO.h"

using namespace std;

#ifndef HELO_H_INCLUDED
#define HELO_H_INCLUDED

class SMTPServer
{
    public: SMTPServer();

    private: string newMessage;
    private: string newRec;
    private: string newSender;
    private: string newData;


    // overload constructor
   // public: SMTPServer(string, string, string, string);

   void SMTPServer:: send(HELO h1);

};

#endif // HELO_H_INCLUDED

SMTPServer cpp

#include "SMTPServer.h"


SMTPServer::SMTPServer()
{
    newMessage = NULL;
    newRec = NULL;
    newSender = NULL;
    newData = NULL;
};

void SMTPServer:: send(HELO h1)
{

}

主班

#include <iostream>
#include <string>
#include "SMTPServer.h"

using namespace std;

int main() {

    string mes;
    string rec;
    string sen;
    string dat;

    SMTPServer test;

    //cout << endl << "HELO message: " << test.send() << endl;

    return 0;

}

提前致谢。

在我看来,您已经重用了SMTPServer.h HELO.h的包含保护。 也就是说,应将它们更改为以下内容:

#ifndef SMTPSERVER_H_INCLUDED
#define SMTPSERVER_H_INCLUDED

...

#endif

如果您在两个文件中都使用了相同的包含保护,则只能将其中一个包含在另一个文件中。 实际上, SMTPServer.h本身包含HELO.h ,因此立即使自己的内容永远不会超出预处理阶段。

如果尚不清楚,请阅读SMTPServer.h的顶部:

#include <string>
#include <iostream>
#include "HELO.h"

using namespace std;

#ifndef HELO_H_INCLUDED

因此,我们正在检查是否已定义HELO_H_INCLUDED 因为它仅包含HELO.h ,并且该文件大概定义了HELO_H_INCLUDED ,所以我们总是说“是的,它已定义!”。 我们将永远不会使用此#ifndef的内容。

  1. 您可以如下初始化字符串:

     SMTPServer::SMTPServer() { newMessage = ""; newRec = ""; newSender = ""; newData = ""; } 

  2. 更改以下代码:

    void SMTPServer:: send(HELO h1);

    void send(HELO h1);

  3. 在执行以下操作后删除分号:

 SMTPServer::SMTPServer() { newMessage = NULL; newRec = NULL; newSender = NULL; newData = NULL; }; 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM