C++ 如何调用在另一个文件中找到的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15891781/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:51:49  来源:igfitidea点击:

How to call on a function found on another file?

c++sfmlidentifier

提问by Safixk

I'm recently starting to pick up C++ and the SFML library, and I was wondering if I defined a Sprite on a file appropriately called "player.cpp" how would I call it on my main loop located at "main.cpp"?

我最近开始学习 C++ 和 SFML 库,我想知道我是否在一个名为“player.cpp”的文件上定义了一个 Sprite,我将如何在位于“main.cpp”的主循环中调用它?

Here is my code (Be aware that this is SFML 2.0, not 1.6!).

这是我的代码(请注意,这是 SFML 2.0,而不是 1.6!)。

main.cpp

主程序

#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "player.cpp"

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Skylords - Alpha v1");

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw();
        window.display();
    }

    return 0;
}

player.cpp

播放器.cpp

#include "stdafx.h"
#include <SFML/Graphics.hpp>

int playerSprite(){
    sf::Texture Texture;
    if(!Texture.loadFromFile("player.png")){
        return 1;
    }
    sf::Sprite Sprite;
    Sprite.setTexture(Texture);
    return 0;
}

Where I need help is in the main.cppwhere it says window.draw();in my draw code. In that parenthesis, there should be the name of the Sprite that I want to load onto the screen. As far as I've searched, and tried by guessing, I have not succeeded into making that draw function work with my sprite on the other file. I feel like I'm missing something big, and very obvious (on either files), but then again, every pro was once a newb.

我需要帮助的main.cpp地方就window.draw();在我的绘图代码中。在那个括号中,应该有我想要加载到屏幕上的 Sprite 的名称。就我的搜索和猜测而言,我还没有成功地使该绘图功能与我在另一个文件上的精灵一起工作。我觉得我错过了一些大的,非常明显的东西(在任何一个文件上),但话说回来,每个专业人士都曾经是新手。

回答by

You can use header files.

您可以使用头文件。

Good practice.

好习惯。

You can create a file called player.hdeclare all functions that are need by other cpp files in that header file and include it when needed.

您可以player.h在该头文件中创建一个名为声明所有其他 cpp 文件需要的函数的文件,并在需要时包含它。

player.h

播放器.h

#ifndef PLAYER_H    // To make sure you don't declare the function more than once by including the header multiple times.
#define PLAYER_H

#include "stdafx.h"
#include <SFML/Graphics.hpp>

int playerSprite();

#endif

player.cpp

播放器.cpp

#include "player.h"  // player.h must be in the current directory. or use relative or absolute path to it. e.g #include "include/player.h"

int playerSprite(){
    sf::Texture Texture;
    if(!Texture.loadFromFile("player.png")){
        return 1;
    }
    sf::Sprite Sprite;
    Sprite.setTexture(Texture);
    return 0;
}

main.cpp

主程序

#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "player.h"            //Here. Again player.h must be in the current directory. or use relative or absolute path to it.

int main()
{
    // ...
    int p = playerSprite();  
    //...


Not such a good practice but works for small projects. declare your function in main.cpp

不是很好的做法,但适用于小型项目。在 main.cpp 中声明你的函数

#include "stdafx.h"
#include <SFML/Graphics.hpp>
// #include "player.cpp"


int playerSprite();  // Here

int main()
{
    // ...   
    int p = playerSprite();  
    //...

回答by rahuljain1311

Small addition to @user995502's answer as to how to run the program.

对@user995502 关于如何运行程序的回答的补充。

g++ player.cpp main.cpp -o main.out && ./main.out

g++ player.cpp main.cpp -o main.out && ./main.out

回答by cppguy

Your sprite is created mid way through the playerSprite function... it also goes out of scope and ceases to exist at the end of that same function. The sprite must be created where you can pass it to playerSprite to initialize it and also where you can pass it to your draw function.

您的精灵是在 playerSprite 函数中途创建的……它也超出了范围,并在同一函数结束时不再存在。必须创建精灵,您可以将其传递给 playerSprite 以对其进行初始化,并且还可以将其传递给您的绘制函数。

Perhaps declare it above your first while?

也许在你的第一个上面声明它while