对包含的特定cpp文件的函数的未定义引用



第一次真正使用头文件时遇到了问题。我创建了一个带有相应hpp文件的cpp文件,该文件包含在我的主文件中,并且再次包含在cpp文件中,现在我得到以下错误:

[build] FAILED: test.exe 
[build] cmd.exe /C "cd . && C:msys64mingw64binx86_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文件中所有函数的未定义引用。这些看起来如下:

CellLayer.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(文件,它似乎包含得很好。如果有人知道是什么原因导致了这个问题,那就太好了。

问题是您在命名空间cel声明了RotateParticles,而在全局命名空间(而不是cel命名空间(内定义了。

解决此问题,您需要在命名空间cel的范围内,您可以通过使用cel::对其进行限定来完成此操作,如下所示:

CellLayer.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
}

类似于inputcellsrandom_bitset等其他功能。

最新更新