簡體   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