xcode C++ 类文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18922363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Class Files
提问by JeremyF
I am having much trouble learning to use files for classes in C++. To learn I use Bucky Roberts/The New Boston C++ tutorials, I have tried exactly what he does, but it does not work.
我在学习使用 C++ 类的文件时遇到了很多麻烦。为了学习我使用 Bucky Roberts/The New Boston C++ 教程,我已经完全尝试过他所做的,但它不起作用。
I have the main.cpp and the OtherClass.cpp with the OtherClass.h for header. Every time I try doing OtherClass::OtherClass(){} for the constructor it errors out with "C++ requires a type specifier for all declarations"
我有 main.cpp 和 OtherClass.cpp 和 OtherClass.h 作为标题。每次我尝试为构造函数执行 OtherClass::OtherClass(){} 时,它都会出错,并显示“C++ 需要所有声明的类型说明符”
Could someone give me an example of how to do C++ class files correctly? Really confused right now.
有人能给我一个如何正确处理 C++ 类文件的例子吗?现在真的很迷茫。
Thanks!
谢谢!
采纳答案by Tamathar
A simple example of using header files for classes (with the implementation in a separate .cpp file) looks something like this:
为类使用头文件的简单示例(在单独的 .cpp 文件中实现)如下所示:
Your main.cpp file:
您的 main.cpp 文件:
#include "OtherClass.h"
int main()
{
OtherClass otherClass;
//use otherClass here...
}
Next, your OtherClass.h file:
接下来,您的 OtherClass.h 文件:
class OtherClass
{
public:
OtherClass();
int someFunction(int parameters);
};
And then finally your OtherClass.cpp file:
最后是您的 OtherClass.cpp 文件:
#include "OtherClass.h"
OtherClass::OtherClass()
{
//implementation here
}
int OtherClass::someFunction(int parameters)
{
//implemenation here
return 0;
}
The main things to keep in mind:
要记住的主要事项:
#include "OtherClass.h"
goes in both OtherClass.cpp and main.cpp- make sure you finish constructor and function declarations with ';' not '{}' if you are defining the implementation elsewhere.
- make sure you're compiling OtherClass.cpp as well as main.cpp. With MinGW this looks like
g++ main.cpp OtherClass.cpp
#include "OtherClass.h"
进入OtherClass.cpp 和main.cpp- 确保使用“;”完成构造函数和函数声明 如果您在别处定义实现,则不是“{}”。
- 确保您正在编译OtherClass.cpp 和main.cpp。使用 MinGW 这看起来像
g++ main.cpp OtherClass.cpp
回答by abanana
Your question is a little cryptic to understand, but if I understand correctly you're looking for the `correct' way to create classes with interfaces in the header file. Here is an example of a class that does this:
您的问题有点难以理解,但如果我理解正确,您正在寻找在头文件中创建带有接口的类的“正确”方法。这是执行此操作的类的示例:
Scene.h
场景.h
#pragma once
#include "Window.h"
#include "Entity.h"
class Scene
{
public:
Scene(Window *_window);
~Scene(void);
void render(Entity item);
void render(Entity item, SDL_Rect *clip);
protected:
Window *window;
};
Scene.cpp
场景文件
#include "Scene.h"
Scene::Scene(Window *_window)
{
window = _window;
}
Scene::~Scene(void)
{
}
void Scene::render(Entity item) {
render(item, NULL);
}
void Scene::render(Entity item, SDL_Rect *clip) {
window->draw( item.getImage(), item.getCoordinates(), clip, item.getAngle() );
}
Notice that the header file includes the headers that it needs to link properly, while the implementation file (.cpp) just includes the header file. The linker should automatically manage all this trouble for you as long as you stick to these semantics.
请注意,头文件包含正确链接所需的头文件,而实现文件 (.cpp) 仅包含头文件。只要您坚持这些语义,链接器就会自动为您处理所有这些麻烦。
I hope this helps; if it doesn't, consider rephrasing your question or pasting some code.
我希望这有帮助; 如果没有,请考虑重新表述您的问题或粘贴一些代码。