简体   繁体   English

在头文件中包含特定函数会导致尝试包含该头文件时出错

[英]Including a specific function in the header file causes and error when trying to include that header file

This is my threeD.h :这是我的threeD.h

#pragma once

#include "SDL.h"
#include <vector>

struct vec3
{
    float x, y, z;
};

struct vec2
{
    float x, y;
};

struct triangle3D
{
    vec3 v[3];
};

class Triangle2D
{
    Triangle2D(vec2 v1, vec2 v2, vec2 v3)
    {
        this->v[0] = v1;
        this->v[1] = v2;
        this->v[2] = v3;
    }
    ~Triangle2D() { };

    void draw(SDL_Renderer* renderer, int r, int g, int b, int a)
    {
        SDL_SetRenderDrawColor(renderer, r, g, b, a);
        SDL_RenderDrawLine(renderer, v[0].x, v[0].y, v[1].x, v[1].y);
        SDL_RenderDrawLine(renderer, v[1].x, v[1].y, v[2].x, v[2].y);
        SDL_RenderDrawLine(renderer, v[2].x, v[2].y, v[0].x, v[0].y);
    }

private:
    vec2 v[3];
};

vec2 projectAndTransform(SDL_Window* window, vec3 v, float fov)
{
    vec2 newVec;
    float fovScale = fov/ v.z;
    float w = (float)SDL_GetWindowSurface(window)->w;
    float h = (float)SDL_GetWindowSurface(window)->h;
    newVec.x = (v.x + 1) * (w / 2);
    newVec.y = (v.y + 1) * (h / 2);
    return newVec;
}

struct mesh3D
{
    std::vector<triangle3D> m;
};
 
struct mesh2D
{
    std::vector<Triangle2D> m;
};

This is my game.h :这是我的game.h

#pragma once

#include <iostream>
#include "SDL.h"
#include "threeD.h"

class Game
{
public:

    Game(const char* windowName, unsigned int xWindowPos, unsigned int yWindowPos
        , unsigned int windowWidth, unsigned int windowHeight, bool fullScreen);
    ~Game();

    bool init();
    bool constructWindowAndRenderer
    (
        const char* windowName,
        unsigned int xWindowPos,
        unsigned int yWindowPos,
        unsigned int windowWidth,
        unsigned int windowHeight,
        bool fullScreen = false
    );

    void update();
    SDL_Window* getWindow();
    void clearWindow();
    void render();
    bool handleEvents();

private:
    SDL_Window* window = nullptr;
    SDL_Renderer* renderer = nullptr;
    SDL_Event event;
    bool quit = false;

    mesh3D m;

    
};

Whenever I try to include threeD.h into game.h I get an error:每当我尝试将threeD.h包含到 game.h 中时,我都会收到错误消息:

Severity    Code    Description Project File    Line    Suppression State
Error   LNK2005 "struct vec2 __cdecl projectAndTransform(struct SDL_Window *,struct vec3,float)" (? 
projectAndTransform@@YA?AUvec2@@PAUSDL_Window@@Uvec3@@M@Z) already defined in game.obj  learnSDL     
C:\Users\myself\source\repos\learn\learn\main.obj   1   

From the error message, I figured there is something wrong with projectAndTransform function in threeD.h file.从错误消息中,我认为threeD.h文件中的projectAndTransform函数有threeD.h So I tried commenting it out, and it worked fine.所以我试着把它注释掉,它工作得很好。 The problem must be projectAndTransform function, but I cant see anything in the function that could cause errors.问题一定是projectAndTransform函数,但我在函数中看不到任何可能导致错误的内容。 Thanks谢谢

https://en.cppreference.com/w/cpp/language/inline https://en.cppreference.com/w/cpp/language/inline

// function included in multiple source files must be inline
inline int sum(int a, int b)
{
    return a + b;
}

There may be more than one definition of an inline function or variable (since C++17) in the program as long as each definition appears in a different translation unit and (for non-static inline functions and variables (since C++17)) all definitions are identical.只要每个定义出现在不同的翻译单元中并且(对于非静态内联函数和变量(自 C++17 )) 所有定义都是相同的。 For example, an inline function or an inline variable (since C++17) may be defined in a header file that is #include'd in multiple source files.例如,一个内联函数或一个内联变量(C++17 起)可以在多个源文件中#include 的头文件中定义。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM