我有一个包含以下内容的 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 文件中,它工作正常怎么来的
最佳答案
不确定如何解决您的问题,但按照惯例,您永远不应包含在头文件中,因为有一天您可能会编写代码,需要您的头文件,但不是所有包含。
关于c++ - 为什么 #include 在 .h 文件或 .cpp 文件中很重要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11071678/