【问题标题】:Including a specific function in the header file causes and error when trying to include that header file尝试包含该头文件时,在头文件中包含特定函数的原因和错误
【发布时间】:2020-10-03 12:43:15
【问题描述】:

这是我的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 函数,但我看不到函数中可能导致错误的任何内容。谢谢

【问题讨论】:

  • 尝试将该函数标记为内联
  • @puio 哇哦,我不知道怎么做,但它奏效了。谢谢
  • @puio 我只是在网上查看内联是什么,我得到了现在的样子,但我没有看到不使函数内联会导致#include 语句失败。你能给我这个概念的名称(如果有的话),或者链接到任何解释这个的文章。感谢感谢。

标签: c++ function sdl inline


【解决方案1】:

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;
}

程序中可能有多个内联函数或变量的定义(C++17 起),只要每个定义出现在不同的翻译单元中,并且(对于非静态内联函数和变量(C 起++17)) 所有定义都是相同的。例如,内联函数或内联变量(C++17 起)可以在多个源文件中#include 的头文件中定义。

【讨论】:

    猜你喜欢
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-14
    • 2021-09-19
    相关资源
    最近更新 更多