第一次真正開始使用標題并遇到問題。我創建了一個帶有相應 hpp 檔案的 cpp 檔案,該檔案包含在我的主檔案中,并且再次包含在 cpp 檔案中,現在我收到以下錯誤:
[build] FAILED: test.exe
[build] cmd.exe /C "cd . && C:\msys64\mingw64\bin\x86_64-w64-mingw32-g .exe -g CMakeFiles/test.dir/Main.cpp.obj CMakeFiles/test.dir/random.cpp.obj -o test.exe -Wl,--out-implib,libtest.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/test.dir/Main.cpp.obj:C:/Users/marks/Master_Project/mark-msc/Main.cpp:10: undefined reference to `cel::inputcells(std::vector<std::vector<cel::Bacteria, std::allocator<cel::Bacteria> >, std::allocator<std::vector<cel::Bacteria, std::allocator<cel::Bacteria> > > >&, int, int, int, int, int)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/test.dir/Main.cpp.obj: in function `main':
[build] C:/Users/marks/Master_Project/mark-msc/Main.cpp:25: undefined reference to `std::bitset<25> cel::random_bitset<25>(double)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/marks/Master_Project/mark-msc/Main.cpp:27: undefined reference to `cel::RotateParticles(std::vector<std::vector<cel::Bacteria, std::allocator<cel::Bacteria> >, std::allocator<std::vector<cel::Bacteria, std::allocator<cel::Bacteria> > > >&, int, int, std::bitset<25ull>)'
[build] collect2.exe: error: ld returned 1 exit status
[build] ninja: build stopped: subcommand failed.
[build] Build finished with exit code 1
基本上,它表明存在對我的 CellLayer.cpp 和 hpp 檔案中存在的所有函式的未定義參考。這些看起來如下:
細胞層.cpp:
#include "random.hpp"
#include "CellLayer.hpp"
void cel::inputcells (std::vector<std::vector<Bacteria>> &CellGrid, int NrPfree, int NrP1, int NrP2, int NrP3, int N)
{
for (int i = 0; i < NrPfree; i)
{
int randomnumber1 = rnd::uni_int(0, N);
int randomnumber2 = rnd::uni_int(0, N);
CellGrid[randomnumber1][randomnumber2].alive = true;
}
for (int j = 0; j < NrP1; )
{
int randomnumber1 = rnd::uni_int(0, N);
int randomnumber2 = rnd::uni_int(0, N);
if (!CellGrid[randomnumber1][randomnumber2].alive)
{
CellGrid[randomnumber1][randomnumber2].alive = true;
std::bitset<3> P1 (std::string("001")) ;
CellGrid[randomnumber1][randomnumber2].plasmid = P1;
j;
}
}
for (int p = 0; p < NrP2; )
{
int randomnumber1 = rnd::uni_int(0, N);
int randomnumber2 = rnd::uni_int(0, N);
if (!CellGrid[randomnumber1][randomnumber2].alive)
{
CellGrid[randomnumber1][randomnumber2].alive = true;
std::bitset<3> P2 (std::string("010"));
CellGrid[randomnumber1][randomnumber2].plasmid = P2;
p;
}
}
for (int l = 0; l < NrP3; )
{
int randomnumber1 = rnd::uni_int(0, N);
int randomnumber2 = rnd::uni_int(0, N);
if (!CellGrid[randomnumber1][randomnumber2].alive)
{
CellGrid[randomnumber1][randomnumber2].alive = true;
std::bitset<3> P3 (std::string("100"));
CellGrid[randomnumber1][randomnumber2].plasmid = P3;
l;
}
}
}
std::bitset<25> cel::random_bitset(double p = 0.5) {
std::bitset<25> bits;
std::random_device rd;
std::mt19937 gen(rd());
std::bernoulli_distribution d(p);
for( int n = 0; n < size; n) {
bits[n] = d(gen);
}
return bits;
}
// Function to rotate and shift the margolus neighbourhood
void cel::RotateParticles(std::vector<std::vector<Bacteria>> &Cellgrid, int t, int N, std::bitset<25> bitseq)
{
//offset for even and uneven timesteps
int offset = t % 2;
//for looping through bitseq
int count = 0;
for(int i0 = offset; i0 < N; i0 = 2)
{
const int i1 = (i0 1) % N;
for(int j0 = offset; j0 < N; j0 = 2)
{
const int j1 = (j0 1) % N;;
if(bitseq[count])
{
// rotating clockwise
Bacteria tmp = Cellgrid[i0][j0];
Cellgrid[i0][j0] = Cellgrid[i0][j1];
Cellgrid[i0][j1] = Cellgrid[i1][j1];
Cellgrid[i1][j1] = Cellgrid[i1][j0];
Cellgrid[i1][j0] = tmp;
count;
}
else
{
//rotating counterclockwise
Bacteria tmp = Cellgrid[i0][j0];
Cellgrid[i0][j0] = Cellgrid[i1][j0];
Cellgrid[i1][j0] = Cellgrid[i1][j1];
Cellgrid[i1][j1] = Cellgrid[i0][j1];
Cellgrid[i0][j1] = tmp;
count;
}
}
}
}
//not finished
void cel::Die (std::vector<std::vector<Bacteria>> &Cellgrid, std::vector<std::vector<std::vector<int>>> &system, std::vector<double> DchanceFree, std::vector<double> DchanceBear, std::pair<int, int>)
{
std::vector<int> AntiB = {0, 1, 2};
std::shuffle(std::begin(AntiB), std::end(AntiB), rnd::rn);
}
和 hpp 檔案:
#ifndef CellLayer_hpp
#define CellLayer_hpp
#include <vector>
#include <iosfwd>
#include <string>
#include <memory>
#include <bits/stdc .h>
namespace cel
{
class Bacteria {public: std::bitset<3> plasmid; bool alive; };
void inputcells (std::vector<std::vector<Bacteria>> &CellGrid, int NrPfree, int NrP1, int NrP2, int NrP3, int N);
std::bitset<25> random_bitset(double p = 0.5);
void RotateParticles(std::vector<std::vector<Bacteria>> &Cellgrid, int t, int N, std::bitset<25> bitseq);
void Die (std::vector<std::vector<Bacteria>> &Cellgrid, std::vector<std::vector<std::vector<int>>> &system, std::vector<double> DchanceFree, std::vector<double> DchanceBear, std::pair<int, int>);
}
#endif
然后在我的主要我有這個:
#include "random.hpp"
#include "CellLayer.hpp"
int main()
{
const int N = 10;
const int T = 5;
std::vector<std::vector<cel::Bacteria>> gridab(N, std::vector<cel::Bacteria>(N));
cel::inputcells(gridab, 1, 1, 1, 1, N);
for (int j = 0; j < N; j)
{
std::cout << "\n";
for (int i = 0; i < N; i)
{
std::cout << gridab[j][i].alive << " ";
}
}
std::cout << "\n";
for (int t = 0; t < T; t)
{
std::bitset<25> bitseq;
bitseq = cel::random_bitset( 0.5);
cel::RotateParticles(gridab, t, N, bitseq);
for (int j = 0; j < N; j)
{
std::cout << "\n";
for (int i = 0; i < N; i)
{
std::cout << gridab[j][i].alive << " ";
}
}
std::cout << "\n";
}
return 0;
}
我真的不知道為什么它不起作用,正如你所看到的,我也有一個 random.hpp (& cpp) 檔案,它似乎包含得很好。如果有人對可能導致此問題的原因有任何見解,那就太好了。
uj5u.com熱心網友回復:
問題是您在命名空間內宣告 ,而您在全域命名空間而不是命名空間內定義它。RotateParticlescelcel
為了解決這個問題,你需要在命名空間的范圍內cel,你可以通過限定它來做到這一點,cel::如下所示:
細胞層.cpp
//---vvv-------------------------------------------added cel:: here
void cel::RotateParticles(std::vector<std::vector<Bacteria>> &Cellgrid, int t, int N, std::bitset<25> bitseq)
{
//other code as before
}
類似的其他功能inputcells,如random_bitset等。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/453296.html
