为什么#include 在 .h 文件或 .cpp 文件中很重要? [英] why does it matter if the #include are in the .h file or in .cpp file?

查看:27
本文介绍了为什么#include 在 .h 文件或 .cpp 文件中很重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下内容的 cpp 文件:

I have a cpp file contains this include:

#include "twitServer.h"

在 twitServer.h 中我有:

and in twitServer.h I have:

#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 对这一行说:

But the Xcode says for this line:

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

getaddrinfo 没有定义......为什么?

that the getaddrinfo is not defined... why that?

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

if the includes are in the cpp file it works fine how comes

推荐答案

不确定如何解决您的问题,但按照惯例,您永远不应该在头文件中包含,因为有一天您可能会编写代码,需要您的头文件,但不是所有的包含.

Not sure how to fix your problem, but by convention, you should never include in the header file, because you might write code some day, needing your header file, but not all the includes.

这篇关于为什么#include 在 .h 文件或 .cpp 文件中很重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆