我查看了C++编程语言,试图找到答案.当我#include "my_dir/my_header.hpp"在标题中,它在哪里寻找这个文件?是相对于包含它的源文件或其他内容的标题?
我正在寻找一个工具(最好是一个Visual Studio插件),它可以显示给定文件包含的所有文件,并显示这些文件包含的所有文件,依此类推.
我目前有两个XSD方案,一个是另一个的"轻"版本.现在我在"完整"模式中重复了"轻"版本中的所有内容,但是当我需要进行更改时,这会变得很痛苦,而且无论如何都违背了DRY原则,所以我想知道是否有元素这有助于将另一个模式包含在模式中,因此我可以从"轻量级"模式中获得"完整"继承,以减少维护障碍.
我有一个C头,编写为C和C++编译(它只使用公共子集中的功能,并使用该extern "C"东西).
问题是,该头部在全局命名空间中声明了东西.我宁愿通常的原因避免这种情况.我想过这样做:
namespace foo {
#include <foo.h>
}
Run Code Online (Sandbox Code Playgroud)
这样做是个好主意吗?我是否有不包含编辑头文件的替代方法?
我有一个主目录,A有两个子目录B和C.
目录B包含头文件structures.c:
#ifndef __STRUCTURES_H
#define __STRUCTURES_H
typedef struct __stud_ent__
{
char name[20];
int roll_num;
}stud;
#endif
Run Code Online (Sandbox Code Playgroud)
目录C包含main.c代码:
#include<stdio.h>
#include<stdlib.h>
#include <structures.h>
int main()
{
stud *value;
value = malloc(sizeof(stud));
free (value);
printf("working \n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:
main.c:3:24: error: structures.h: No such file or directory
main.c: In function ‘main’:
main.c:6: error: ‘stud’ undeclared (first use in this function)
main.c:6: error: (Each undeclared identifier is reported only …Run Code Online (Sandbox Code Playgroud) 我只需要有人告诉我,我是否正确理解何时使用<include>以及何时使用<merge>.
所以,我创建了一个标题布局,我希望将其包含在其他XML布局中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header text" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我以这种方式将它包含在其他XML中(这是非常基本的):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<include
android:id="@+id/header"
layout="@layout/top"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这将很好,没有问题.但是为了优化代码,我必须<merge>在包含的布局中使用.所以不top layout应该有标签,<LinearLayout>但它必须如下所示:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Header text" />
</merge>
Run Code Online (Sandbox Code Playgroud)
我理解正确吗?
A.这是做什么的?
require ("./file.php");
Run Code Online (Sandbox Code Playgroud)
B.与此相比?
require ("file.php");
Run Code Online (Sandbox Code Playgroud)
(它不是一个目录..这将是)
require ("../file.php");
Run Code Online (Sandbox Code Playgroud) 常用的路径分隔符有两种:Unix正斜杠和DOS反斜杠.安息吧,经典的Mac冒号.如果在#include指令中使用,它们是否符合C++ 11,C++ 03和C99标准的规则?
解决了
真正帮助我的是,我可以在.cpp文件中#include标头而不会导致重新定义的错误.
我是C++的新手,但我在C#和Java方面有一些编程经验,所以我可能会遗漏一些C++独有的基础知识.
问题是我真的不知道什么是错的,我会粘贴一些代码来试图解释这个问题.
我有三个类,GameEvents,Physics和GameObject.我有每个标题.GameEvents有一个Physics和一个GameObjects列表.Physics有一个GameObjects列表.
我想要实现的是我希望GameObject能够访问或拥有一个Physics对象.
如果我只是在GameObject中#include"Physics.h",我会得到"错误C2111:'ClassXXX':'class'类型redtifinition",我理解.这就是我认为#include-guard有帮助的地方所以我在我的Physics.h中添加了一个包含守卫,因为那是我想要包含两次的标题.
这是它的外观
#ifndef PHYSICS_H
#define PHYSICS_H
#include "GameObject.h"
#include <list>
class Physics
{
private:
double gravity;
list<GameObject*> objects;
list<GameObject*>::iterator i;
public:
Physics(void);
void ApplyPhysics(GameObject*);
void UpdatePhysics(int);
bool RectangleIntersect(SDL_Rect, SDL_Rect);
Vector2X CheckCollisions(Vector2X, GameObject*);
};
#endif // PHYSICS_H
Run Code Online (Sandbox Code Playgroud)
但如果我在GameObject.h中#include"Physics.h",现在就像这样:
#include "Texture2D.h"
#include "Vector2X.h"
#include <SDL.h>
#include "Physics.h"
class GameObject
{
private:
SDL_Rect collisionBox;
public:
Texture2D texture;
Vector2X position;
double gravityForce;
int weight;
bool isOnGround;
GameObject(void);
GameObject(Texture2D, Vector2X, int);
void UpdateObject(int);
void Draw(SDL_Surface*);
void SetPosition(Vector2X);
SDL_Rect GetCollisionBox(); …Run Code Online (Sandbox Code Playgroud) 我试图让整个<head>部分成为自己的包含文件.一个缺点是标题和描述以及关键字是相同的; 我无法弄清楚如何将参数传递给包含文件.
所以这是代码:
<?php include("header.php?header=aaaaaaaaaaaaaaaaaaaaa"); ?>
<body>
.....
..
.
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<link rel="shortcut icon" href="favicon.ico">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content=" <?php $_GET["header"]?> " >
<meta name="Description" content=" <?php $_GET["header"]?> " >
<title> <?php $_GET["header"]?> </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
Run Code Online (Sandbox Code Playgroud)
显然这不起作用; 如何将参数传递给包含的文件?