C ‘;新’;未声明(首次在此函数中使用)

C ‘;新’;未声明(首次在此函数中使用),c,C,此代码用于哈密顿循环 我无法解决此错误: In function ‘hamCycle’: error: ‘new’ undeclared (first use in this function) int *path = new int [V]; ^ note: each undeclared identifier is reported only once for each function it appears in

此代码用于哈密顿循环 我无法解决此错误:

    In function ‘hamCycle’:
   error: ‘new’ undeclared (first use in this function)
         int *path = new int [V];
                     ^
 note: each undeclared identifier is reported only once for each function it appears in
 error: expected ‘,’ or ‘;’ before ‘int’
         int *path = new int [V];
                         ^
哈密顿循环代码为:

/*
 * C Program to Find Hamiltonian Cycle
 */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define V 5

void printSolution(int path[]);

/* 
 * check if the vertex v can be added at index 'pos' in the Hamiltonian Cycle 
 */
bool isSafe(int v, bool graph[V][V], int path[], int pos)
{
    if (graph [path[pos-1]][v] == 0)
        return false;
   for (int i = 0; i < pos; i++)
        if (path[i] == v)
            return false;
    return true;
}

/* solve hamiltonian cycle problem */
bool hamCycleUtil(bool graph[V][V], int path[], int pos)
{
    if (pos == V)
    {
        if (graph[ path[pos-1] ][ path[0] ] == 1)
            return true;
        else
            return false;
    }

    for (int v = 1; v < V; v++)
    {
        if (isSafe(v, graph, path, pos))
        {
            path[pos] = v;
            if (hamCycleUtil (graph, path, pos+1) == true)
                return true;
            path[pos] = -1;
        }
    }
    return false;
}

/* solves the Hamiltonian Cycle problem using Backtracking.*/
bool hamCycle(bool graph[V][V])
{
    int *path = new int [V];
    for (int i = 0; i < V; i++)
        path[i] = -1;
    path[0] = 0;
    if (hamCycleUtil(graph, path, 1) == false)
    {
      printf("\nSolution does not exist");
        return false;
    }
    printSolution(path);
    return true;
}

/* Main */
void printSolution(int path[])
{
    printf("Solution Exists:");
    printf(" Following is one Hamiltonian Cycle \n");
    for (int i = 0; i < V; i++)
        printf(" %d",path[i]);
    printf(" %d",path[0]);
}

int main()
{
   /* Let us create the following graph
      (0)--(1)--(2)
       |   / \   |
       |  /   \  |
       | /     \ |
      (3)-------(4)    */
   bool graph1[V][V] = {{0, 1, 0, 1, 0},
                      {1, 0, 1, 1, 1},
                      {0, 1, 0, 0, 1},
                      {1, 1, 0, 0, 1},
                      {0, 1, 1, 1, 0},
                     };
   hamCycle(graph1);

   /* Let us create the following graph
      (0)--(1)--(2)
       |   / \   |
       |  /   \  |
       | /     \ |
      (3)       (4)    */
    bool graph2[V][V] = {{0, 1, 0, 1, 0},
                      {1, 0, 1, 1, 1},
                      {0, 1, 0, 0, 1},
                      {1, 1, 0, 0, 0},
                      {0, 1, 1, 0, 0},
                     };
    hamCycle(graph2);
    return 0;
}
/*
*求哈密顿圈的C程序
*/
#包括
#包括
#包括
#定义v5
无效打印解决方案(int路径[]);
/* 
*检查是否可以在哈密顿循环中的索引“pos”处添加顶点v
*/
布尔isSafe(整数v,布尔图[v][v],整数路径[],整数位置)
{
if(图[path[pos-1]][v]==0)
返回false;
对于(int i=0;i

<如何解决这个问题?

< P> C和C++不是同一种语言。c中没有
new
关键字,因为

int*path=newint[V]在c中不存在

如果要分配一个大小为V的整数数组,可以做两件事

int *path = malloc(V*sizeof(int));


C和C++不是同一种语言。c中没有

new
关键字,因为

int*path=newint[V]在c中不存在

如果要分配一个大小为V的整数数组,可以做两件事

int *path = malloc(V*sizeof(int));


您是从哪里了解分配内存的
new
的?您是从哪里了解分配内存的
new
的?