使用多个头文件和 cpp 文件帮助

发布于 2024-10-30 09:28:57 字数 2566 浏览 4 评论 0原文

我正在使用 DragonFireSDK 制作一个应用程序,我想用 .cpp 和 .h 文件组织我的数千行应用程序,

但在尝试执行操作时遇到大量错误

所以我的 app.cpp (主要的,必需的)看起来像这样

代码:

#include "DragonFireSDK.h"

#include "SaveData.h"
#include "Structures.h"
#include "Definitions.h"
#include "Variables.h"
#include "SaveData.h"

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "Functions.cpp"
#include "AppMain.cpp"
#include "AppExit.cpp"
#include "OnTimer.cpp"

#include“SaveData.h”到#include“Variables.h” 都有类似的东西 代码:

#ifndef _HeaderName
#define _HeaderName
//STUFF HERE LIKE
#define player1 0
#define player2 1
//OR
typedef struct _number {
    int view;
    int number;
    bool able;
    int opacity;
};_number number[4];
//OR
int whoseturn;

int bet[5];
bool reachedmax[5];

int playerimg[5];
#endif

现在我可能已经做错了一些事情,但这里还有更多...... 我的 AppMain.cpp、OnTimer.cpp 等看起来像这样 (AppMain() 等也是必需的函数) 代码:

#include "DragonFireSDK.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "Definitions.h"
#include "Structures.h"
#include "Variables.h"
#include "SaveData.h"

#include "Functions.cpp"

void AppMain() {
//STUFF HERE
};

现在这就是我认为问题所在...... 函数.cpp 代码:

#include "DragonFireSDK.h"

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "SaveData.h"
#include "Structures.h"
#include "Definitions.h"
#include "Variables.h"
//SOME FUNCTIONS
void   SavePlayerMoney();
void   SetInfo            (int idnum, bool actuallyset = false);
void   SwitchButton       (int idnum, bool makeactive=true);
void   DisableButton      (int idnum);
double round              (double number);

void SavePlayerMoney() {
    //...
}
void   SetInfo(int idnum, bool actuallyset) {
      //...
}
void   SwitchButton(int idnum, bool makeactive) {
      //...
}
void   DisableButton(int idnum){
      //...
}

现在我想如果修复了所有的东西之后遇到的错误...... 代码:

1>AppMain.obj : error LNK2005: "void __cdecl SwitchButton(int,bool)" (?SwitchButton@@YAXH_N@Z) already defined in App.obj
1>AppMain.obj : error LNK2005: "double __cdecl round(double)" (?round@@YANN@Z) already defined in App.obj
1>AppMain.obj : error LNK2005: "void __cdecl SetInfo(int,bool)" (?SetInfo@@YAXH_N@Z) already defined in App.obj
1>AppMain.obj : error LNK2005: "int __cdecl Digits(int)" (?Digits@@YAHH@Z) already defined in App.obj

非常感谢任何帮助!

I'm making an app with DragonFireSDK and I want to organize my multi thousand line app with .cpp and .h files

I get tons of errors when trying to do stuff though

So my app.cpp (main, required one) looks like this

Code:

#include "DragonFireSDK.h"

#include "SaveData.h"
#include "Structures.h"
#include "Definitions.h"
#include "Variables.h"
#include "SaveData.h"

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "Functions.cpp"
#include "AppMain.cpp"
#include "AppExit.cpp"
#include "OnTimer.cpp"

The #include "SaveData.h" through #include "Variables.h"
all have something like
Code:

#ifndef _HeaderName
#define _HeaderName
//STUFF HERE LIKE
#define player1 0
#define player2 1
//OR
typedef struct _number {
    int view;
    int number;
    bool able;
    int opacity;
};_number number[4];
//OR
int whoseturn;

int bet[5];
bool reachedmax[5];

int playerimg[5];
#endif

Now I may be doing something wrong already but here's some more...
My AppMain.cpp, OnTimer.cpp etc look like this
(AppMain(), etc are required functions too)
Code:

#include "DragonFireSDK.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "Definitions.h"
#include "Structures.h"
#include "Variables.h"
#include "SaveData.h"

#include "Functions.cpp"

void AppMain() {
//STUFF HERE
};

Now this is where I think the problem is...
Functions.cpp
Code:

#include "DragonFireSDK.h"

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "SaveData.h"
#include "Structures.h"
#include "Definitions.h"
#include "Variables.h"
//SOME FUNCTIONS
void   SavePlayerMoney();
void   SetInfo            (int idnum, bool actuallyset = false);
void   SwitchButton       (int idnum, bool makeactive=true);
void   DisableButton      (int idnum);
double round              (double number);

void SavePlayerMoney() {
    //...
}
void   SetInfo(int idnum, bool actuallyset) {
      //...
}
void   SwitchButton(int idnum, bool makeactive) {
      //...
}
void   DisableButton(int idnum){
      //...
}

Now the errors I get after I thought if fixed all the stuff...
Code:

1>AppMain.obj : error LNK2005: "void __cdecl SwitchButton(int,bool)" (?SwitchButton@@YAXH_N@Z) already defined in App.obj
1>AppMain.obj : error LNK2005: "double __cdecl round(double)" (?round@@YANN@Z) already defined in App.obj
1>AppMain.obj : error LNK2005: "void __cdecl SetInfo(int,bool)" (?SetInfo@@YAXH_N@Z) already defined in App.obj
1>AppMain.obj : error LNK2005: "int __cdecl Digits(int)" (?Digits@@YAHH@Z) already defined in App.obj

Any help is very greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

琉璃繁缕 2024-11-06 09:28:57

不要#include .cpp 文件。

C 编译模型是每个函数定义一次,即恰好在一个编译单元(即一个目标文件)中。您将每个源文件独立编译为单独的目标文件(#include-ing header 文件,以便编译器知道要使用的函数的原型)。然后,您将这些单独的目标文件链接在一起以形成最终的可执行文件。

如果您#include .cpp 文件,您最终将在多个编译单元中定义相同的函数(请记住#include 是基本上相当于将内容复制粘贴到正在执行包含操作的文件中)。因此链接器会感到困惑,并给出您所看到的消息。

更新

哦,我发现问题是您没有 Functions.cpp 相应的头文件。这个想法是,您还可以编写一个 Functions.h,大致如下:

#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_

void SavePlayerMoney();
void SetInfo(int idnum, bool actuallyset);
void SwitchButton(int idnum, bool makeactive);
void DisableButton(int idnum);

#endif

然后您 #include this 头文件,而不是.cpp 文件。

Don't #include the .cpp files.

The C compilation model is that each function is defined precisely once, i.e. in exactly one compilation unit (i.e. one object file). You compile each source file independently into a separate object file (#include-ing header files so that the compiler knows e.g. the prototype of functions to be used). You then link these separate object files together to form the final executable.

If you #include the .cpp files, you will end up with the same function being defined in multiple compilation units (remember that #include is basically equivalent to copy-pasting the contents into the file that's doing the including). So the linker will get confused, and give you the messages that you are seeing.

UPDATE

Oh, I see the problem is that you don't have a corresponding header file for Functions.cpp. The idea is that you also write a Functions.h, along the lines of:

#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_

void SavePlayerMoney();
void SetInfo(int idnum, bool actuallyset);
void SwitchButton(int idnum, bool makeactive);
void DisableButton(int idnum);

#endif

And then you #include this header file, rather than the .cpp file.

滥情空心 2024-11-06 09:28:57

链接器会抱怨,因为函数被定义了多次。函数只能在一个翻译单元(cpp 文件,编译后成为 obj 文件)中定义 - 除非它被声明为内联。

您将 Functions.cpp 包含在其他单元中,因此 Function.cpp 中的函数定义会重复到这些单元中,从而导致链接器出现问题。

解决方案是声明函数内联 - 或者更好的在标头中声明它们(即Functions.h)和 >在Functions.cpp中定义它们。这些函数的任何用户都可以#include Functions.h 并访问这些函数,即使他们不知道它们的实现。

声明一个函数,请执行:int foo();,要实际定义它,请执行int foo() { 你的代码到这里}。

The linker complains because functions are defined more than once. A function may only be defined in one translation unit (cpp file, after compilation it becomes an obj file) - except if it is declared inline.

You're including Functions.cpp in other units, so the function definitions from Function.cpp get duplicated into those, thus causing the linker trouble.

The solution would be to declare the functions inline - or, even better, declare them in a header (i.e. Functions.h) and define them in Functions.cpp. Any users of those functions may then #include Functions.h and have access to these functions even though they don't know their implementation.

To declare a function, do: int foo();, to actually define it, do int foo() { your code goes here}.

┾廆蒐ゝ 2024-11-06 09:28:57

我认为每个人都回答得很好,所以我将向您介绍我关于大型项目的 C++ 哲学,因为它似乎是您可能会发现有用的信息。

始终将函数声明和实现分开。
这将使您的生活变得更加轻松。在 .h 文件中声明函数原型,然后在 .cpp 文件中编写实现。

例如:

// mystuff.h
#ifndef MYSTUFF_H
#define MYSTUFF_H

int myFunction(int value, char letter);

#endif

在我的 .cpp 文件中:

// mystuff.cpp

#include "mystuff.h"

int myFunction(int value, char letter) {
    // insert implementation here
}

为什么要这样做?一个重要的原因是,当您的代码无法工作时(表面上它会工作,这对任何程序员来说都是不可避免的现实),您可以用替代实现替换 .cpp 文件,而无需修改代码的结构。不仅如此,您还会发现各种依赖于分离声明和实现的技巧,这将大大减轻您的生活。底线是,去做吧。

尽可能尝试封装。
如果您正在做一个大项目(您会发现对于您遇到的大多数大项目都是如此),封装类似的函数、变量等将为您节省大量时间和精力。看起来您正在编写一个程序来玩游戏 - 您是否考虑过将每个玩家封装到 Player 或 Human 类中,并为每个玩家提供特定于类的函数?如果您像我一样是 C++ 或 Java 迷,您会发现面向对象的方法百有九十九是最有效的方法(1% 的情况通常是您拥有不真正适合的辅助函数)在您定义的任何对象中)。

此外,封装使您能够利用面向对象设计的另外两个基本原则——多态性和继承。例如,您可以定义一个 Player 类,那么如果您的游戏涉及计算机玩家和人类玩家,您可以为他们每个人编写一个单独的类,该类继承 Player 的基本功能,但在一个实例中实现 Player 的每个功能。不同的方式(即,如果有 makeMove 函数,则人类的实现与计算机的实现不同。因此,继承极大地简化了您的工作)。显然,面向对象设计有许多吸引人的品质,但就我从您的代码中收集到的信息而言,我想说您将从这些品质中受益最多。

显然,这是我自己的哲学,而不是我想强加给你们的哲学。但希望您能从我简短的漫谈中获得一些有用的提示,以改进编写代码的方式和/或避免一长串的错误。祝你好运!

I think everyone answered this really well so I'm just going to give you my C++ philosophy on big projects because it seems like it is information that you may find useful.

ALWAYS separate function declarations and implementation.
It will make your life considerably easier. Declare function prototypes in a .h file, then write the implementation in a .cpp file.

For example:

// mystuff.h
#ifndef MYSTUFF_H
#define MYSTUFF_H

int myFunction(int value, char letter);

#endif

And in my .cpp file:

// mystuff.cpp

#include "mystuff.h"

int myFunction(int value, char letter) {
    // insert implementation here
}

Why do this? Well one great reason is that when your code doesn't work (as it ostensibly will, an inescapable reality for any programmer), you can substitute out your .cpp file with alternate implementations without modifying the structure of your code. Not only that, there are various tricks you will discover that will rely on separating declarations and implementation that will ease your life considerably. Bottom line, do it.

Attempt encapsulation wherever possible.
If you're doing a big project (and you will notice this is true for most big projects you encounter), encapsulating similar functions, variables, and the like will save you considerable time and energy. It seems like you're making a program to play a game- have you thought about encapsulating each player into a Player or Human class, with class-specific functions for each one? If you're a C++ or Java junkie like myself, you will find that an object-oriented approach is the most effective approach 99 times out of 100 (the 1% of situations is usually where you have helper functions that don't really fit in any of the objects you've defined).

Also, encapsulation enables you to take advantage of the two other fundamental principles of object-oriented design- polymorphism and inheritance. For example, you could define a Player class, then if your game involves a computer player and a human player, you could write a separate class for each of them that inherits the basic functionality of a Player but implements each function of a Player in a different way (i.e. if there is a makeMove function, you would have a different implementation for a human than a computer. Thus, inheritance greatly simplifies your job). There are obviously many qualities of OO design that are appealing, but for what I've gleaned from your code, I'd say you would benefit the most from these ones.

Obviously, this is my own philosophy and not one that I wish to forcefully impose on you. But hopefully you will take a few helpful tips out of my terse rambling to improve the way you write code and/or avoid long lists of errors. Best of luck!

小帐篷 2024-11-06 09:28:57

将函数声明移至头文件。例如,看起来 Functions.h 应该包含:

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

//SOME FUNCTIONS
void   SavePlayerMoney();
void   SetInfo            (int idnum, bool actuallyset = false);
void   SwitchButton       (int idnum, bool makeactive=true);
void   DisableButton      (int idnum);
double round              (double number);

#endif

然后 Functions.cpp 可以只包含 Functions.h 而不是那些声明。某些头文件可能需要包含其他头文件才能获得适当的类型。

最后,永远不要#include *.cpp 文件。

Move your function declarations to header files. For example, looks like Functions.h should contain:

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

//SOME FUNCTIONS
void   SavePlayerMoney();
void   SetInfo            (int idnum, bool actuallyset = false);
void   SwitchButton       (int idnum, bool makeactive=true);
void   DisableButton      (int idnum);
double round              (double number);

#endif

Then Functions.cpp can just include Functions.h instead of those declarations. Some header files may need to include other header files to get the appropriate types.

Finally, never #include a *.cpp file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文