簡體   English   中英

#include是在.h文件還是.cpp文件中為什么為什么重要?

[英]why does it matter if the #include are in the .h file or in .cpp file?

我有一個cpp文件,其中包含:

#include "twitServer.h"

在twitServer.h中,我有:

#ifndef twitServer_twitServer_h
#define twitServer_twitServer_h
#include <string>
#include <map>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstring>
#include <netdb.h>
#include <errno.h>
#include <sstream>
#include <algorithm>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "twitUser.h"

using namespace std;

void startServer(string port);

#endif

但是Xcode對此行說:

if ((rv = getaddrinfo(NULL, port, &hints, &ai)) != 0) {
    fprintf(stderr, "selectserver: %s\n", gai_strerror(rv));
    exit(1);
}

沒有定義getaddrinfo ...為什么?

如果包含在cpp文件中,則可以正常工作

不確定如何解決問題,但按照慣例,您絕對不應將其包含在頭文件中,因為有一天可能會編寫代碼,需要頭文件,但不是所有包含的內容。

嘗試包括Ws2tcpip.h,看看是否可以解決問題。 這就是MSDN要說的(請參閱需求部分)

http://msdn.microsoft.com/zh-CN/library/windows/desktop/ms738520%28v=vs.85%29.aspx

另外,讓我說,這似乎包括了很多頭文件,有些不應該匹配,除非您真的知道自己在做什么。 有很多C ++頭文件(iostream(包括兩次),cstdio,cstdlib,算法,列表,字符串,ctime ...)與C頭文件(stdio.h,stdlib.h string.h)混合在一起)。 也許是時候在那里進行后期春季清潔了:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM