有一个有点详细的线程(228684)关于如何全局(使用extern结构)声明一个可以在超过1个c ++文件中看到的结构,但我无法弄清楚到底是怎么做的(有很多讨论要做到这一点,做到这一点,也许这样做,尝试这个,等等...).
couuld有人请发一个非常简单的例子来说明如何声明一个可以在2个单独的c ++文件中看到的结构?如果我将所有函数放在与main相同的文件中,它可以正常工作,但是当我尝试将函数拆分为不同的文件时,我无法进行编译.
我不清楚的事情......我应该输入结构吗?我是否在头文件中定义结构并在每个c ++源文件中包含该头?我是否需要头文件中的#ifndef宏?我是否在标题中声明结构extern?
如果文件以注释开头,C编译器是否缓存头文件并仅解析一次?
// Some comment
#ifndef HEADER_GUARD
#define HEADER_GUARD
#endif
Run Code Online (Sandbox Code Playgroud)
问题是关于编译器,它可以缓存标题,不会多次解析它们.
在为包含守卫选择名字时,是否有人遵循的指导方针?我不明白为什么.h文件的名称与include guard中使用的名称略有不同.例如,我见过sphere.h然后#ifndef SPHERE_H_.我可以轻易地使用SPHERE_或者名称必须匹配吗?下划线也是必要的吗?
代码如下
// in ptr.h
#pragma once
#include <memory>
template<class T> using Ptr = std::unique_ptr<T>;
Run Code Online (Sandbox Code Playgroud)
所以每次我使用std :: unique_ptr时,我都包含"ptr.h"并将其用作Ptr.这是一个好习惯吗?
我有一个用C编写的.inc文件.它包含一些带有实现的定义和方法签名.
.inc文件:
#define CONCAT(x,y) x ## y
#define LCONCAT(x,y) CONCAT(x,y)
#define DLIST LCONCAT(LCONCAT(double_,LISTELEMENT),_list)
#define DELETEDFIRST LCONCAT(delete_first_double_,LISTELEMENT)
int DELETEDFIRST (DLIST **first, DLIST **last);
int DELETEDFIRST (DLIST **first, DLIST **last)
{...}
Run Code Online (Sandbox Code Playgroud)
我想把它带到c ++.在我的.h文件中,我有define指令和方法签名(封装在命名空间中).在.cpp我只有方法实现.
.h文件
#define CONCAT(x,y) x ## y
#define LCONCAT(x,y) CONCAT(x,y)
#define DLIST LCONCAT(LCONCAT(double_,LISTELEMENT),_list)
#define DELETEDFIRST LCONCAT(delete_first_double_,LISTELEMENT)
namespace ListFunctions {
int DELETEDFIRST (DLIST **first, DLIST **last);
}
Run Code Online (Sandbox Code Playgroud)
.cpp文件
# include ListFunctions.h
namespace ListFunctions {
int DELETEDFIRST (DLIST **first, DLIST **last) {...}
}
Run Code Online (Sandbox Code Playgroud)
我打算在开发我的模块时使用这些列表函数.在我的module.h中,我定义了一个类型double_ node _list(双端),在我的module.cpp中,我将LISTELEMENT定义为" node ",然后包含ListFunctions.h.但ListFunctions.cpp中的相同内容会导致编译错误:
..\ListFunctions.h(86): error …Run Code Online (Sandbox Code Playgroud) 我有以下文件结构:
XH
#pragma once
#include "Y.h"
int ONE = 1;
int combine();
Run Code Online (Sandbox Code Playgroud)
XC
#include "X.h"
int combine()
{
return ONE + TWO;
}
Run Code Online (Sandbox Code Playgroud)
YH
#pragma once
int TWO = 2;
Run Code Online (Sandbox Code Playgroud)
YC
#include "Y.h"
Run Code Online (Sandbox Code Playgroud)
MAIN.C
#include "X.h"
int main()
{
int fusion = combine();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
LNK1169 one or more multiply defined symbols found
LNK2005 _ONE already defined in Main.obj
LNK2005 _TWO already defined in Main.obj
KLNK2005 _TWO already defined in Main.obj
Run Code Online (Sandbox Code Playgroud)
这没有任何意义.如果我们从Main.c编译器开始必须包含X.h.然后编译器查找与之关联的C文件X.h.里面 …
我试着制作一个名为Paitent_info.h的标题,你可以在这里看到:
#ifdef GUARD_Paitent_info
#define GUARD_Paitent_info
#include <iostream>
#include <string>
#include <vector>
struct Paitent_info {
std::string name;
std::vector<double> tem;
};
bool compare(const Paitent_info&, const Paitent_info&);
std::istream& read(std::istream&, Paitent_info&);
std::istream& read_tem(std::istream&, std::vector<double>&);
#endif
Run Code Online (Sandbox Code Playgroud)
这是Paitent_info.cpp:
#include "Paitent_info.h"
using std::istream; using std::vector;
bool compare(const Paitent_info& x, const Paitent_info& y)
{
return x.name < y.name;
}
istream& read(istream& ip, Paitent_info& p)
{ // do something
return ip;
}
istream& read_tem(istream& in, vector<double>& tem)
{ // do something
return in;
}
Run Code Online (Sandbox Code Playgroud)
我收到了来自此代码的许多错误消息:
c++ ×5
c ×3
comments ×1
extern ×1
file ×1
global ×1
header-files ×1
optimization ×1
structure ×1