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

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

这是我的 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;
};

这是我的 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;

    
};

每当我尝试将 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   

根据错误消息,我认为 threeD.h 文件中的 projectAndTransform 函数有问题。所以我试着把它注释掉,效果很好。问题一定是 projectAndTransform 函数,但我在函数中看不到任何可能导致错误的内容。谢谢

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. 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.

  • How can a C++ header file include implementation?