[英]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
的內容。
您可以如下初始化字符串:
SMTPServer::SMTPServer() { newMessage = ""; newRec = ""; newSender = ""; newData = ""; }
更改以下代碼:
void SMTPServer:: send(HELO h1);
至
void send(HELO h1);
在執行以下操作后刪除分號:
SMTPServer::SMTPServer() { newMessage = NULL; newRec = NULL; newSender = NULL; newData = NULL; };
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.