我已经读过使用时会有一些编译器优化,#pragma once这会导致更快的编译.我认为这是非标准的,因此可能造成跨平台兼容性问题.
这是非Windows平台(gcc)上大多数现代编译器支持的东西吗?
我想避免平台编译问题,但也想避免后备警卫的额外工作:
#pragma once
#ifndef HEADER_H
#define HEADER_H
...
#endif // HEADER_H
Run Code Online (Sandbox Code Playgroud)
我应该担心吗?我是否应该在这方面进一步消耗精力?
我有两个类都在单独的头文件中定义.每个文件都有一个其他类的字段.现在我在每个文件的头文件中包含了其他文件的头文件,但是编译器正在生成错误.我错过了什么?
我的问题很常见,但我一直在寻找和尝试我找到的所有解决方案,但仍然无效.所以任何帮助将不胜感激!=)
提前致谢!
我在编译时遇到此错误:
g++ -ISFML/include -Iclasses/ -W -Wall -Werror -c -o classes/Object.o classes/Object.cpp
In file included from classes/Core.hh:18:0,
from classes/Object.hh:4,
from classes/Object.cpp:1:
classes/MapLink.hh:9:1: error: invalid use of incomplete type ‘struct Object’
classes/MapLink.hh:6:7: error: forward declaration of ‘struct Object’
In file included from classes/Core.hh:19:0,
from classes/Object.hh:4,
from classes/Object.cpp:1:
classes/Player.hh:9:1: error: invalid use of incomplete type ‘struct Object’
classes/MapLink.hh:6:7: error: forward declaration of ‘struct Object’
make: *** [classes/Object.o] Error 1
Run Code Online (Sandbox Code Playgroud)
所以基本上,我有一个主要包含(main.cpp)
#include "Core.hh"
int main(void)
{
...
}
Run Code Online (Sandbox Code Playgroud)
这是包含我所有包含的头文件(Core.hh)
#ifndef __CORE_HH__
# …Run Code Online (Sandbox Code Playgroud) 我目前正在攻读CS课程的期末考试,而且我遇到了一个关于C++ #ifndef语法的小问题(可能是主要的?).
当我将#infndef用作#include后卫时,我已经查看了#infndef的语法,网上的大多数人都说:
#ifndef HEADER_H
#define "header.h"
...
#endif
Run Code Online (Sandbox Code Playgroud)
但我班级的教程幻灯片显示的例子如下:
#ifndef __HEADER_H__
#define "header.h"
...
#endif
Run Code Online (Sandbox Code Playgroud)
我想知道两者之间的差异(如果有的话).考试很可能会让我写一个#include后卫,而且我知道传统的智慧只是与教授/导师所说的一致,但如果在编译过程中存在差异,我想知道.
我正在使用Visual C++ Express创建一个DLL,并在extern ValveInterfaces* VIFace内部声明时
Required.h,编译器告诉我ValveInterfaces没有定义.(我想暴露VIFace给任何文件,包括Required.h)
这是我的文件的结构:
DLLMain.cpp
#include "Required.h" //required header files, such as Windows.h and the SDK
ValveInterfaces* VIFace;
//the rest of the file
Run Code Online (Sandbox Code Playgroud)
Required.h
#pragma once
//include Windows.h, and the SDK
#include "ValveInterfaces.h"
extern ValveInterfaces* VIFace; //this line errors
Run Code Online (Sandbox Code Playgroud)
ValveInterfaces.h
#pragma once
#ifndef _VALVEINTERFACES_H_
#define _VALVEINTERFACES_H_
#include "Required.h"
class ValveInterfaces
{
public:
ValveInterfaces(void);
~ValveInterfaces(void);
static CreateInterfaceFn CaptureFactory(char *pszFactoryModule);
static void* CaptureInterface(CreateInterfaceFn fn, char * pszInterfaceName);
//globals
IBaseClientDLL* gClient; …Run Code Online (Sandbox Code Playgroud) 首先我要说的是,我已经在google上广泛搜索了答案,更具体地说.
事实上我(至少我认为我做过)找到了类似问题的人,虽然给他们的答案给了我另一个问题.
我正在使用Visual Studio 2010 Express并使用SFML库(尽管我不认为这最后一部分是相关的)
所以这里:
我有一个名为player.cpp的源文件,它包含类Player,我有一个名为cc.h(命令和控制)的头文件(包含在所有源文件中),它包含所有必需的包含和外部变量/函数.基本代码可以总结如下:
Player.cpp:
#include "cc.h"
class Player
{
private:
//some variables here
public:
//more variables and some functions
}john;//example instance
Run Code Online (Sandbox Code Playgroud)
cc.h:
#pragma once
//some #includes
//some externs
extern Player john;
Run Code Online (Sandbox Code Playgroud)
现在在cc.h中,单词Player被强调为错误,表示它是一个未定义的标识符,但有时只是,有时,visual studio不会将其标记为错误,而是将其识别为类但不识别john作为同一个类的对象/实例(我希望它是这样称呼的).此外,在编译第一个错误时,它显示error C2146: syntax error : missing ';' before identifier 'john'在约翰的外部声明的行中,在cc.h中,显然(对我来说)没有任何意义.