c++ - 为什么 #include 在 .h 文件或 .cpp 文件中很重要?

标签 c++ xcode

我有一个包含以下内容的 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/

相关文章:

c++ - 指针引用模式 - 常用?

ios - swift 3 iOS 兼容性

xcode - 启用 AVX 时,Clang 会生成较慢的代码?

c++ - 在 boost::spirit mini_c 中实现 "NOT"

c++ - MFC 还活着吗?

c++ - c++什么时候需要用#include

ios - 从 NSDictionary Swift 设置枚举

c++ - 集合迭代器中的对象

xcode - Apple TV 没有出现在 Xcode > 设备和模拟器菜单中

当我尝试存档我的项目 : line 7:/resources-to-copy-project. txt 时出现 xcode 错误消息:权限被拒绝