R[0][0]; 对于(i=1;i,c++,visual-studio,C++,Visual Studio" /> R[0][0]; 对于(i=1;i,c++,visual-studio,C++,Visual Studio" />

错误C2440 can';t将void*转换为double**或double* 我在C++中编写的代码有问题。编译器向我发送错误C2440,我不知道为什么。这是代码,问题出现在void main()区域,特别是在R=calloc和R[i]=calloc行中。谢谢你的回答 #include <stdio.h> #include <stdlib.h> #include <math.h> #include <iostream> using namespace std; double F (double x); double G (double x); double P (double x); void romberg(double f(double), double a, double b, int n, double **R); double F (double x) { return (1.0/ (1.0 + x)); } double G (double x) { return (exp(x)); } double P (double x) { return (sqrt(x)); } void romberg(double f(double), double a, double b, int n, double **R) { int i, j, k; double h, sum; h = b - a; R[0][0] = 0.5 * h * (f(a) + f(b)); cout << " R[0][0] = "; cin >> R[0][0]; for (i = 1; i <= n; i++) { h *= 0.5; sum = 0; for (k = 1; k <= pow(2,i)-1; k+=2) { sum += f(a + k * h); } R[i][0] = 0.5 * R[i-1][0] + sum * h; cout << "R[i][0] = "; cin>>i, R[i][0]; for (j = 1; j <= i; j++) { R[i][j] = R[i][j-1] + (R[i][j-1] - R[i-1][j-1]) / (pow(4,j)-1); cout << " R[i][j] = "; cin>>i, j, R[i][j]; } } } void main() { int n = 10; int i; double **R; double F(double), G(double), P(double); R = calloc((n+1), sizeof(double *)); for (i = 0; i <= n; i++) R[i] = calloc((n+1), sizeof(double)); cout<<"The first function is F(x) = 1/(1 + x)\n"; romberg(F, 0.0, 2.0,3, R); cout<<"The second function is G(x) = exp(x)\n"; romberg(G,-1.0, 1.0, 4, R); cout<<"The third function is P(x) = sqrt(x)\n"; romberg(P,0.0, 1.0, 7, R); } #包括 #包括 #包括 #包括 使用名称空间std; 双F(双x); 双G(双x); 双P(双x); void-romberg(双f(双),双a,双b,整数n,双**R); 双F(双x) { 回报率(1.0/(1.0+x)); } 双G(双x) { 返回(exp(x)); } 双P(双x) { 返回(sqrt(x)); } void romberg(双f(双),双a,双b,整数n,双**R) { int i,j,k; 双h,和; h=b-a; R[0][0]=0.5*h*(f(a)+f(b)); cout>R[0][0]; 对于(i=1;i

错误C2440 can';t将void*转换为double**或double* 我在C++中编写的代码有问题。编译器向我发送错误C2440,我不知道为什么。这是代码,问题出现在void main()区域,特别是在R=calloc和R[i]=calloc行中。谢谢你的回答 #include <stdio.h> #include <stdlib.h> #include <math.h> #include <iostream> using namespace std; double F (double x); double G (double x); double P (double x); void romberg(double f(double), double a, double b, int n, double **R); double F (double x) { return (1.0/ (1.0 + x)); } double G (double x) { return (exp(x)); } double P (double x) { return (sqrt(x)); } void romberg(double f(double), double a, double b, int n, double **R) { int i, j, k; double h, sum; h = b - a; R[0][0] = 0.5 * h * (f(a) + f(b)); cout << " R[0][0] = "; cin >> R[0][0]; for (i = 1; i <= n; i++) { h *= 0.5; sum = 0; for (k = 1; k <= pow(2,i)-1; k+=2) { sum += f(a + k * h); } R[i][0] = 0.5 * R[i-1][0] + sum * h; cout << "R[i][0] = "; cin>>i, R[i][0]; for (j = 1; j <= i; j++) { R[i][j] = R[i][j-1] + (R[i][j-1] - R[i-1][j-1]) / (pow(4,j)-1); cout << " R[i][j] = "; cin>>i, j, R[i][j]; } } } void main() { int n = 10; int i; double **R; double F(double), G(double), P(double); R = calloc((n+1), sizeof(double *)); for (i = 0; i <= n; i++) R[i] = calloc((n+1), sizeof(double)); cout<<"The first function is F(x) = 1/(1 + x)\n"; romberg(F, 0.0, 2.0,3, R); cout<<"The second function is G(x) = exp(x)\n"; romberg(G,-1.0, 1.0, 4, R); cout<<"The third function is P(x) = sqrt(x)\n"; romberg(P,0.0, 1.0, 7, R); } #包括 #包括 #包括 #包括 使用名称空间std; 双F(双x); 双G(双x); 双P(双x); void-romberg(双f(双),双a,双b,整数n,双**R); 双F(双x) { 回报率(1.0/(1.0+x)); } 双G(双x) { 返回(exp(x)); } 双P(双x) { 返回(sqrt(x)); } void romberg(双f(双),双a,双b,整数n,双**R) { int i,j,k; 双h,和; h=b-a; R[0][0]=0.5*h*(f(a)+f(b)); cout>R[0][0]; 对于(i=1;i,c++,visual-studio,C++,Visual Studio,你的主要问题看起来很奇怪: void main而不是int main 双F(双),G(双),P(双);- 如果使用MalC/CaloC/ReLoCc,必须在C++中铸造返回空指针。 不要忘记删除分配的内存 例如: int main() { const int n = 10; double **R; R = new double*[n+1]; for (int i = 0; i <= n; i++) R[i] = new double[n

你的主要问题看起来很奇怪:

  • void main
    而不是
    int main
  • 双F(双),G(双),P(双);
    -
  • 如果使用MalC/CaloC/ReLoCc,必须在C++中铸造返回空指针。
  • 不要忘记删除分配的内存
例如:

int main()
{
    const int n = 10;
    double **R;

    R = new double*[n+1];
    for (int i = 0; i <= n; i++)
        R[i] = new double[n+1];
    cout << "The first function is F(x) = 1/(1 + x)\n";
    romberg(F, 0.0, 2.0,3, R);
    cout << "The second function is G(x) = exp(x)\n";
    romberg(G,-1.0, 1.0, 4, R);
    cout<<"The third function is P(x) = sqrt(x)\n";
    romberg(P,0.0, 1.0, 7, R);

    // free allocated memory
    for (int i = 0; i <= n; i++)
        delete[] R[i];
    delete[] R;
}
intmain()
{
常数int n=10;
双**R;
R=新的双*[n+1];

对于(int i=0;C++中的i ,有一个<代码> vector < /COD>容器(一个C++标准模板),它与一个数组类似,但异常情况是,在它增长

时,它会自动处理它自己的存储需求。 您需要切换到RAII
例如:

int main()
{
    const int n = 10;
    double **R;

    R = new double*[n+1];
    for (int i = 0; i <= n; i++)
        R[i] = new double[n+1];
    cout << "The first function is F(x) = 1/(1 + x)\n";
    romberg(F, 0.0, 2.0,3, R);
    cout << "The second function is G(x) = exp(x)\n";
    romberg(G,-1.0, 1.0, 4, R);
    cout<<"The third function is P(x) = sqrt(x)\n";
    romberg(P,0.0, 1.0, 7, R);

    // free allocated memory
    for (int i = 0; i <= n; i++)
        delete[] R[i];
    delete[] R;
}
由此:

double **R;

  R = calloc((n+1), sizeof(double *));
  for (i = 0; i <= n; i++)
    R[i] = calloc((n+1), sizeof(double));

也可以阅读C++概念。

PS:只需将
void*
显式转换为
double*
double**
,就可以避免出现错误,但这种方式会导致更多的复杂情况,如内存泄漏等。

为什么会出现错误? 对于编译器在执行赋值时将为您执行何种类型的转换,C++的规则比C(而且
calloc
是一个C函数)严格得多。将
void*
赋值给除
void*
以外的任何对象都需要您告诉编译器,“是的,这看起来很愚蠢,很危险,但我知道我在做什么。”

为什么这是一个错误? 在C++和C++编程的编年史中有一段很长时间的隐式转换,使这种转换至少会使程序员在继续之前停止思考。 我能怎么办? 在这种情况下,最符合逻辑的简单解决方案是

std::vector<std::vector<double>> descriptive_name_goes_here(n+1, std::vector<double>(n+1));
和一个
for
循环,以
make_unique
内部维度,并将所有分配的值分配给0

unique\u ptr
处理内存管理,确保它引用的分配数据在不再被引用时被销毁。作为成员变量,
unique\u ptr
可能会使传递对象或将对象存储在容器中成为一件棘手的事情,因为如果可以复制
unique\u ptr
,它就不一定会被复制这一切都是独一无二的,不是吗?
vector
为你处理这些废话

如果上述两种方法在某种程度上是不可能的,到2017年,这应该是完全人为的限制,或者是对需要传统编译器的传统代码进行操作的结果,那么使用

double ** descriptive_name_goes_here = new double*[n+1]
for
循环到
new
内部维度,并将所有分配的值分配给0

此选项将所有内存管理责任都交给程序员,甚至编程之神偶尔也会错过一个
delete[]
是必需的。这也意味着您程序员有责任支持,以确保任何包装类中的销毁、复制、分配和移动逻辑都是正确的

对于编写
向量
或智能指针之类的低级工作,这是绝对的最后手段

<> C++中唯一的地方>代码> CaloC将是在奇怪的边缘情况下,坦白地说,我现在想不起来了。很可能没有任何。

有了这些选择。。。 我可以推荐用1D数组生成一个简单的矩阵类吗?数组数组方法看起来很好、简单、优雅,并且通常具有非常糟糕的缓存性能由分散在存储中的许多不同内存块组成。这使得CPU更难预测下一步要去哪里以及要加载哪些数据。每次CPU必须停止并等待加载缓存时,您都会遭受性能下降的影响,以数量级衡量

#include <iostream>
#include <vector>

class Matrix
{
private:
    size_t rows, columns;
    std::vector<double> matrix;
public:
    Matrix(size_t numrows, size_t numcols) :
            rows(numrows), columns(numcols), matrix(rows * columns)
    {
    }

    double & operator()(size_t row, size_t column)
    {
        // check bounds here
        return matrix[row * columns + column]; // note 2D coordinates are flattened to 1D
    }

    double operator()(size_t row, size_t column) const
    {
        // check bounds here
        return matrix[row * columns + column];
    }

    size_t getRows() const
    {
        return rows;
    }
    size_t getColumns() const
    {
        return columns;
    }
};
#包括
#包括
类矩阵
{
私人:
行、列的大小;
向量矩阵;
公众:
矩阵(大小numrows、大小numcols):
行(numrows)、列(numcols)、矩阵(行*列)
{
}
双运算符()(行大小、列大小)
{
//在这里检查边界
返回矩阵[行*列+列];//注意二维坐标展平为1D
}
双运算符()(行大小、列大小)常量
{
//在这里检查边界
返回矩阵[行*列+列];
}
大小\u t getRows()常量
{
返回行;
}
大小\u t getColumns()常量
{
返回列;
}
};

这很容易变成一个模板,用于泛化存储的数据

Never
void main
int main
!1.必须分配内存时使用
new
。2.很少需要分配内存(使用标准容器,如
std::vector
或使用智能指针)@crashmstr:不,永远不要使用
new
k请阅读关于发布代码的指南-目前,post包含的代码太多了,通常1-2行就足以说明这样的编译器错误。这就是你的问题解释了?“你的main看起来很奇怪。”?@user4581301同意,使用vector更好here@Pavel
vect
std::unique_ptr<std::unique_ptr<double[]>[]> descriptive_name_goes_here =
    std::make_unique<std::unique_ptr<double[]>[]>(n+1);
double ** descriptive_name_goes_here = new double*[n+1]
#include <iostream>
#include <vector>

class Matrix
{
private:
    size_t rows, columns;
    std::vector<double> matrix;
public:
    Matrix(size_t numrows, size_t numcols) :
            rows(numrows), columns(numcols), matrix(rows * columns)
    {
    }

    double & operator()(size_t row, size_t column)
    {
        // check bounds here
        return matrix[row * columns + column]; // note 2D coordinates are flattened to 1D
    }

    double operator()(size_t row, size_t column) const
    {
        // check bounds here
        return matrix[row * columns + column];
    }

    size_t getRows() const
    {
        return rows;
    }
    size_t getColumns() const
    {
        return columns;
    }
};