-2
我想在將來使用一些函數,並將它們移動到.h文件以使它們可以包含到其他項目中。當我嘗試將其包含到項目中的第二個cpp文件中時,它無法用LNK2005錯誤進行編譯。如何解決它? UPD:錯誤:包含函數定義的頭文件在包含在幾個.cpp文件中時會彈出LNK2005
1>------ Build started: Project: Random Methods, Configuration: Debug Win32 ------
1> Source.cpp
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\my\random\random.hpp(11): warning C4244: 'initializing': conversion from 'time_t' to 'unsigned int', possible loss of data
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\my\random\random.hpp(31): warning C4244: '=': conversion from 'unsigned __int64' to 'unsigned int', possible loss of data
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\my\random\random.hpp(32): warning C4244: 'argument': conversion from 'time_t' to 'unsigned int', possible loss of data
1>Source.obj : error LNK2005: "double __cdecl min(double,double)" ([email protected]@[email protected]) already defined in main.obj
1>Source.obj : error LNK2005: "int __cdecl random(int,int)" ([email protected]@[email protected]) already defined in main.obj
1>Source.obj : error LNK2005: "float __cdecl random(float,float)" ([email protected]@[email protected]) already defined in main.obj
1>Source.obj : error LNK2005: "double __cdecl random(double,double)" ([email protected]@[email protected]) already defined in main.obj
1>Source.obj : error LNK2005: "long double __cdecl random(long double,long double)" ([email protected]@[email protected]) already defined in main.obj
1>Source.obj : error LNK2005: "__int64 __cdecl random(__int64,__int64)" ([email protected]@[email protected]) already defined in main.obj
1>Source.obj : error LNK2005: "unsigned __int64 __cdecl random(unsigned __int64,unsigned __int64)" ([email protected]@[email protected]) already defined in main.obj
1>Source.obj : error LNK2005: "bool __cdecl randomBool(void)" ([email protected]@YA_NXZ) already defined in main.obj
1>Source.obj : error LNK2005: "bool __cdecl randomChance(double)" ([email protected]@[email protected]) already defined in main.obj
1>Source.obj : error LNK2005: "bool __cdecl randomProb(double)" ([email protected]@[email protected]) already defined in main.obj
1>Source.obj : error LNK2005: "void __cdecl randomingInit(void)" ([email protected]@YAXXZ) already defined in main.obj
1>Source.obj : error LNK2005: "void __cdecl swap(int,int)" ([email protected]@[email protected]) already defined in main.obj
1>Source.obj : error LNK2005: "unsigned __int64 * pwrs2" ([email protected]@3PA_KA) already defined in main.obj
1>Source.obj : error LNK2005: "unsigned __int64 rand_max_val" ([email protected]@3_KA) already defined in main.obj
1>Source.obj : error LNK2005: "unsigned int rand_max_val_small" ([email protected]@3IA) already defined in main.obj
1>Source.obj : error LNK2005: "unsigned int RANDOM_SEED" ([email protected]@3IA) already defined in main.obj
1>E:\Google Drive\C++\Random Methods\Debug\Random Methods.exe : fatal error LNK1169: one or more multiply defined symbols found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
代碼: random.hpp
//(C) AdmiralMyxtaR 20/12/2016
//stand-alone randoming from Dictionary
#ifndef ADMIRALMYXTAR_RANDOM_INCLUDED
#define ADMIRALMYXTAR_RANDOM_INCLUDED
#include <ctime>
#include <cstdlib>
using namespace std;
unsigned __int64 pwrs2[64]; //powers of two
unsigned __int64 rand_max_val; //maximum value that can be achieved by random (2^64-1)
unsigned int rand_max_val_small;
unsigned int RANDOM_SEED = time(0);
void swap(int a, int b)
{
int t = a;
a = b;
b = t;
}
double min(double a, double b) //return minimum of two values
{
if (a > b) { return b; }
else { return a; }
}
void randomingInit() //create powers of two array and init random generator
{
pwrs2[0] = 1;
for (int i = 1; i < 65; i++)
{
pwrs2[i] = pwrs2[i - 1] * 2;
}
rand_max_val = pwrs2[64] - 1;
rand_max_val_small = pwrs2[32] - 1;
srand(time(0));
}
bool randomBool()
{
return rand() % 2 == 1;
}
float random(float a, float b)
{
float d = 0.0F;
d += rand() % 10000/1e4F;
d += rand() % 10000/1e8F;
return a + d*(b - a);
}
double random(double a, double b)
{
double d = 0.0;
d += rand() % 10000/1e4;
d += rand() % 10000/1e8;
d += rand() % 10000/1e12;
d += rand() % 10000/1e16;
return a + d*(b - a);
}
long double random(long double a, long double b)
{
long double d = 0.0;
d += rand() % 10000/1e4L;
d += rand() % 10000/1e8L;
d += rand() % 10000/1e12L;
d += rand() % 10000/1e16L;
d += rand() % 10000/1e20L;
d += rand() % 10000/1e24L;
return a + d*(b - a);
}
__int64 random(__int64 a, __int64 b)
{
unsigned __int64 r = 0, diff = b - a;
for (int i = 0; i < 5; ++i)
{
r = (r << 15) | rand();
}
return a + r % (b - a + 1);
}
unsigned __int64 random(unsigned __int64 a, unsigned __int64 b)
{
unsigned __int64 r = 0, diff = b - a;
for (int i = 0; i < 5; ++i)
{
r = (r << 15) | rand();
}
return a + r % (b - a + 1);
}
int random(int a, int b)
{
unsigned int r = 0, diff = b - a;
for (int i = 0; i < 3; ++i)
{
r = (r << 15) | rand();
}
return a + r % (b - a + 1);
}
bool randomProb(double prob) //returns whether a situation with probability prob will success(true) or fail(false)
{
return random(0.0, 1.0) <= prob;
}
bool randomChance(double chance) //returns whether a situation with chance chance percents will success(true) or fail(false)
{
return random(0.0, 100.0) <= chance;
}
#endif
;
main.cpp中:
#include <iostream>
#include <cmath>
#include <my\random\random.hpp>
#include <chrono>
#include <thread>
using namespace std;
unsigned __int64 i = 0;
void startPerfomanceMeter()
{
auto start = std::chrono::high_resolution_clock::now();
while (true)
{
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
double t = elapsed.count();
double T = 1e9/(i/t);
cout << t << " s " << i/t << " Hz " << T << " ns " << endl;
this_thread::sleep_for(500ms);
}
}
int main()
{
int t,max=-0xFFFFFFF,min=0xFFFFFFF;
srand(time(0));
thread perfomanceMeter(startPerfomanceMeter);
for (i; ; i++)
{
t=random(13, 99999);
if (t > max)
{
max = t;
cout << (double)i << " " << " min=" << min << " max=" << max << endl;
}
if (t < min)
{
min = t;
cout << (double)i << " " << " min=" << min << " max=" << max << endl;
}
//cout << random(13,666) << endl;
}
system("pause");
return 0;
}
二CPP只是有一個單一的線 - 包括random.hpp。刪除所有全局變量和函數,依賴於它們並沒有解決問題。使用帶更新3的MSVS 2015.
您還沒有提供您收到的錯誤。 – Borgleader
將你的函數定義從頭文件中取出。把這些放在一個cpp文件中。 – drescherjm
我忘了LNK2005錯誤到底是什麼,你能告訴我嗎?我開始看起來有點老邁:) –