【发布时间】:2019-06-18 13:50:58
【问题描述】:
我尝试使用以下命令在 Linux 中运行带有.hpp 文件的.cpp:g++ -c main.cpp,但我有这个关于calloc() 的错误:
我注意到这段代码在 Microsoft Visual Studio 中工作:
#pragma once
#include <iostream>
#include <fstream>
template <typename T>
class MyMatrix {
private:
int Rows;
int Colomns;
T* A; //Matricea
T* Tr; //Transpusa acesteia
float* Inv; //Inversa
public:
MyMatrix(int L, int C)
{
Rows = L;
Colomns = C;
A = (T*)calloc(Rows * Colomns, sizeof(T));
if (A == NULL)
throw("Eroare la alocarea matricii! :(");
}
MyMatrix(T* S, int L, int C)
: MyMatrix(L, C)
{
for (int i = 0; i < Rows * Colomns; ++i)
A[i] = S[i];
}
~MyMatrix() { free(A); }
void Transposed()
{
Tr = (T*)calloc(Rows * Colomns, sizeof(T));
for (int i = 0; i < Colomns; ++i)
for (int j = 0; j < Rows; ++j)
Tr[j * Colomns + i] = A[i * Rows + j];
}
void Inverse()
{ //some code
T* Adj = Adjoint();
Inv = (float*)calloc(Rows * Rows, sizeof(float));
for (int i = 0; i < this->Rows * this->Rows; ++i)
Inv[i] = Adj[i] / (float)Det;
}
};
#endif // MYMATRIX_HPP_INCLUDED
【问题讨论】:
-
最好的解决办法是根本不使用
calloc。请改用std::vector。 -
如果您迫切需要手动管理内存,您应该使用
new[]和delete[],而不是 C 函数。 -
查找在哪个标头
calloc中声明了。 -
停止用 C++ 编写 C!只需使用
std::vector并应用“零规则”。
标签: c++ templates unix g++ calloc