无论出于何种原因,我们公司都有一个编码指南,规定:
Each class shall have it's own header and implementation file.
因此,如果我们编写一个名为的类,MyString
我们需要一个关联的MyStringh.h和MyString.cxx.
还有其他人这样做吗?有没有人看到任何编译性能影响?10000个文件中的5000个类的编译速度是否与2500个文件中的5000个类一样快?如果没有,差异是否明显?
[我们编写C++并使用GCC 3.4.4作为我们的日常编译器]
解决了
真正帮助我的是,我可以在.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) 我刚开始用C++编程,我试图创建两个类,其中一个将包含另一个.
档案A.h
:
#ifndef _A_h
#define _A_h
class A{
public:
A(int id);
private:
int _id;
B _b; // HERE I GET A COMPILATION ERROR: B does not name a type
};
#endif
Run Code Online (Sandbox Code Playgroud)
档案A.cpp
:
#include "A.h"
#include "B.h"
#include <cstdio>
A::A(int id): _id(id), _b(){
printf("hello\n the id is: %d\n", _id);
}
Run Code Online (Sandbox Code Playgroud)
档案B.h
:
#ifndef _B_h
#define _B_h
class B{
public:
B();
};
#endif
Run Code Online (Sandbox Code Playgroud)
档案B.cpp
:
#include "B.h"
#include <cstdio>
B::B(){
printf("this is hello from B\n");
} …
Run Code Online (Sandbox Code Playgroud) 我不知道该搜索什么来找到解释,所以我在问.
我有这个代码报告错误:
struct Settings{
int width;
int height;
} settings;
settings.width = 800; // 'settings' does not name a type error
settings.height = 600; // 'settings' does not name a type error
int main(){
cout << settings.width << " " << settings.height << endl;
Run Code Online (Sandbox Code Playgroud)
但如果我将值赋值放在main中,它的工作原理如下:
struct Settings{
int width;
int height;
} settings;
main () {
settings.width = 800; // no error
settings.height = 600; // no error
Run Code Online (Sandbox Code Playgroud)
你能解释一下为什么吗?
编辑:
关于Ralph Tandetzky的回答,这是我的完整结构代码.你能告诉我如何像你的片段结构那样分配值吗?
struct Settings{
struct Dimensions{
int width;
int height; …
Run Code Online (Sandbox Code Playgroud) 我得到一个问题,而返回类型是结构
Example.h
class Example {
private:
typedef struct connection_header {
string url;
string method;
};
static connection_header get_connection_header();
};
Example.cpp
connection_header Example::get_connection_header() {
return NULL;
}
Run Code Online (Sandbox Code Playgroud)
我正进入(状态 'connection_header' does not name a type
我可以知道为什么会出现这个错误
error: 'func' does not name a type
当一个func
类的成员函数B
试图返回一个类时,我获得了一个C
:
class A {
public:
class B {
public:
C func() const {
...
}
private:
friend class A;
}
class C {
public:
...
private:
friend class A;
}
private:
...
}
Run Code Online (Sandbox Code Playgroud)
然而,如果func
是A的成员函数,则以下内容不会产生此错误:
class A {
public:
class B {
public:
...
private:
friend class A;
}
C func() const {
...
}
class C {
public:
...
private:
friend class A;
}
private: …
Run Code Online (Sandbox Code Playgroud) 我已经看到了类似的答案,但我似乎无法通过查看这些例子(例如这个或那个)来解决我的问题.
所以,我有这个.
啊
#ifndef INCLUDE_CLASS_NAME
#define INCLUDE_CLASS_NAME
#include <B.h>
using namespace C;
D::DPtr myvariable; <-- Error in here
#endif
Run Code Online (Sandbox Code Playgroud)
在包括Bh我有这个:
namespace C{
namespace E{
class D
{
public:
typedef shared_ptr<D> DPtr;
}
} //end of namespace E
} // end of namespace C
Run Code Online (Sandbox Code Playgroud)
为什么我在上述行中收到此错误:
'D' does not name a type
Run Code Online (Sandbox Code Playgroud)
我包含.h文件,它定义了类.我错过了什么?