简体   繁体   English

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

[英]How to call on a function found on another file?

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.cpp where it says window.draw();我需要帮助的地方在main.cpp ,它说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.在那个括号中,应该有我想要加载到屏幕上的 Sprite 的名称。 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.我觉得我错过了一些大的,非常明显的(在任何一个文件上),但话说回来,每个专业人士都曾经是新手。

You can use header files.您可以使用头文件。

Good practice.好习惯。

You can create a file called player.h declare 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();  
    //...

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

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

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 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播放器

#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.cpp where it says window.draw();我需要帮助的地方是在main.cpp ,上面写着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.在该括号中,应该有要加载到屏幕上的Sprite的名称。 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.据我搜索并通过猜测尝试后,我还没有成功地使该draw函数与我的sprite一起作用于另一个文件。 I feel like I'm missing something big, and very obvious (on either files), but then again, every pro was once a newb.我觉得我缺少大的东西,而且很明显(在两个文件中都很明显),但是每位专业人士再一次都是新手。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何正确地从另一个文件调用函数? - How correctly to call a function from another file? 如何从另一个文件中的函数调用main的同一文件中的函数 - How to call a function in the same file of main from a function in another file C ++如何同时从另一个文件中调用与另一个文件中的函数同名的函数? - C++ How to call a function from another file with the same name as a function in another file when both are included? 如何在一个文件中用C ++定义一个函数并在另一个文件中调用它? - How to define a function in C++ in one file and call it in another one? 如何从C ++中的另一个头文件调用函数? - How to call a function from another header file in C++? 如何从另一个头文件cpp文件调用main函数 - How to call main function from another header files cpp file 如何从C ++调用另一个文件中定义的python函数? - How to call a python function defined in another file from c++? 在Google Test Framework中,如何期望一个函数调用或另一个函数调用? - In Google Test Framework, how to expect a function call OR another function call? 从其他文件调用 function - 在虚幻引擎上找不到标识符? - Call function from other file - identifier not found on Unreal Engine? 从另一个源文件中声明一个函数“没有匹配的函数要调用” - Declaring a function from another source file “no matching function to call”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM