[英]Include header file causes compilation error when added to a specific class
[英]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
函數有threeD.h
。 所以我試着把它注釋掉,它工作得很好。 問題一定是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;
}
只要每個定義出現在不同的翻譯單元中並且(對於非靜態內聯函數和變量(自 C++17 )) 所有定義都是相同的。 例如,一個內聯函數或一個內聯變量(C++17 起)可以在多個源文件中#include 的頭文件中定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.