我的情況是這樣的:
我在頭文件中定義了一個函數,然後這個函數被其他函數引用。但是編譯的時候死活通不過,一直報錯:error LINK2005,費了好大力氣-_-才解決。
#include "aabb.h"
#pragma once
#define M_PI 3.14159
aabb surrounding_box(aabb box0, aabb box1) {
vec3 small(fmin(box0.min().getX(), box1.min().getX()),
fmin(box0.min().getY(), box1.min().getY()),
fmin(box0.min().getZ(), box1.min().getZ()));
vec3 big(fmax(box0.max().getX(), box1.max().getX()),
fmax(box0.max().getY(), box1.max().getY()),
fmax(box0.max().getZ(), box1.max().getZ()));
return aabb(small, big);
}
解決方法:
將這個函數移動到head.cpp中(莫偷懶!),只在head.h中聲明該函數,注意加上extern關鍵字。
解決後如下:
head.h:
#ifndef HEAD_H
#define HEAD_H
#include "vec3.h"
#include "aabb.h"
#pragma once
#define M_PI 3.14159
extern void get_sphere_uv(const vec3& p, float& u, float& v);
extern aabb surrounding_box(aabb box0, aabb box1);
#endif
head.cpp:
#include "head.h"
void get_sphere_uv(const vec3& p, float& u, float& v)
{
float phi = atan2(p.getZ(), p.getX());
float theta = asin(p.getY());
u = 1 - (phi + M_PI) / (2 * M_PI);
v = (theta + M_PI / 2) / M_PI;
}
aabb surrounding_box(aabb box0, aabb box1) {
vec3 small(fmin(box0.min().getX(), box1.min().getX()),
fmin(box0.min().getY(), box1.min().getY()),
fmin(box0.min().getZ(), box1.min().getZ()));
vec3 big(fmax(box0.max().getX(), box1.max().getX()),
fmax(box0.max().getY(), box1.max().getY()),
fmax(box0.max().getZ(), box1.max().getZ()));
return aabb(small, big);
}
搞定!