Runtime Error:[ERROR] A Not allowed system call

写OJ用CB用惯了,突然用VS写最后不会有按任意键退出。所以加了一句System("pause");

交到GCC上就报这个错

// AAA.cpp : 定义控制台应用程序的入口点。
//


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>


#define ERROR 0
#define OK 1
#define FALSE 0
#define TRUE 1
#define LISTSIZE 100
#define LISTINCREMENT 50
typedef int status;


typedef struct
{
int* elem;
int count;
int size;
}sqlist;


status Initlist(sqlist &l)
{
l.count = 0;
l.size = LISTSIZE;
l.elem = (int*)malloc(LISTSIZE * sizeof(int));
if (!l.elem)
return ERROR;
return OK;
}
status Deletelist(sqlist &l)
{
memset(&l, 0, sizeof(l));
return OK;
}
status Insertlist(sqlist &l, int n, int x)
{
if (n > l.size)return ERROR;
if (l.count == l.size)
{
l.elem = (int*)realloc(l.elem,( l.size + LISTINCREMENT)*sizeof(int));
l.size += LISTINCREMENT;
}

if (!l.elem)return ERROR;
if (n <= l.count)
{
for (int i = n; i<l.count; i++)
l.elem[n + 1] = l.elem[n];


l.elem[n] = x;
}
else
l.elem[n] = x;
l.count++;
return OK;
}
status List_intersection(sqlist l1, sqlist l2, sqlist &l3)
{
int flag = 0;
for (int i = 0, j = 0;;)
{
if (i == l1.count || j == l2.count)
break;
if (l1.elem[i]>l2.elem[j])
j++;
else if (l1.elem[i]<l2.elem[j])
i++;
else if (l1.elem[i] == l2.elem[j])
{
if (flag == 0)
{
printf("%d", l1.elem[i]);
flag++;
}
else
printf(" %d", l1.elem[i]);
Insertlist(l3, l3.count, l1.elem[i]);
//l3.count++;
i++;
j++;
}


}
return OK;
}


status List_union(sqlist l1, sqlist l2)
{
int flag = 0,flag1=0,k=l1.count+l2.count,i=0,j=0;
for (;;)
{


if (l1.elem[i]>l2.elem[j])
{
if (flag == 0)
{
printf("%d", l2.elem[j]);
flag++;
}
else
printf(" %d", l2.elem[j]);
if (j != l2.count )
j++;
}
else if (l1.elem[i]<l2.elem[j])
{
if (flag == 0)
{
printf("%d",l1.elem[i]);
flag++;
}
else
printf(" %d", l1.elem[i]);
if (i != l1.count )
i++;
}
else if (l1.elem[i] == l2.elem[j])
{
k--;
if (flag == 0)
{
printf("%d", l1.elem[i]);
flag++;
}
else
printf(" %d", l1.elem[i]);
if (i != l1.count )
i++;
if (j != l2.count )
j++;
}
k--;
if (i == l1.count || j == l2.count )
break;
}
if (k > 0)
{
if (j==l2.count)
for (; k > 0; k--)
   printf(" %d", l1.elem[i++]);
else if (i==l1.count)
printf(" %d", l2.elem[j++]);
}
return OK;
}
status List_difference(sqlist l1, sqlist l3)
{
int flag = 0;
for (int i = 0, j = 0;;)
{
if (l1.elem[i] == l3.elem[j])
{
i++;
if (j != l3.count - 1)
j++;
}
else if (l1.elem[i]>l3.elem[j])
{
if (flag == 0)
{
printf("%d", l1.elem[i]);
flag++;
}
else
printf(" %d", l1.elem[i]);
//Insertlist(l5, l5.count, l1.elem[i]);
if (j != l3.count - 1)
j++;
i++;
}
else if (l1.elem[i]<l3.elem[j])
{
if (flag == 0)
{
printf("%d", l1.elem[i]);
flag++;
}
else
printf(" %d", l1.elem[i]);
//Insertlist(l5, l5.count, l1.elem[i]);
i++;
}
if (i == l1.count)
break;


}
return OK;
}
void List_show(sqlist l)
{
for (int i = 0; i<l.count; i++)
{
if (i == 0)
printf("%d", l.elem[i]);
else
printf(" %d", l.elem[i]);
}


}
int main()
{
int i, j, m, n, temp;
sqlist l1, l2, l3;
scanf("%d", &m);
for (i = 1; i <= m; i++)
{


Initlist(l1);
Initlist(l2);
Initlist(l3);
scanf("%d", &n);
for (j = 0; j<n; j++)
{
scanf("%d", &temp);
Insertlist(l1, j, temp);
}
scanf("%d", &n);
for (j = 0; j<n; j++)
{
scanf("%d", &temp);
Insertlist(l2, j, temp);
}
printf("Case #%d:\n", i);
List_intersection(l1, l2, l3);
//List_show(l3);
printf("\n");
List_union(l1, l2);
//List_show(l4);
printf("\n");
List_difference(l1, l3);
//List_show(l5);
printf("\n");
List_difference(l2, l3);
// printf("\n");
//List_show(l5);
// Deletelist(l1);
//Deletelist(l2);
//Deletelist(l3);
}
//Deletelist(l1);
//Deletelist(l2);
//Deletelist(l3);
system("pause");
return 0;


}

### 回答1: 这个错误是因为在常量表达式中使用了函数调用。在C++中,常量表达式必须在编译时求值,而函数调用需要在运行时才能执行。因此,函数调用不能用于常量表达式。要解决这个问题,可以将函数调用替换为常量或表达式。 ### 回答2: 在C++中,常量表达式是指编译时可以确定并在程序整个生命周期里都不会改变的表达式。在这种情况下,编译器会在编译时计算表达式的值,并将它放入程序的常量中。 如果在常量表达式中使用了函数调用,就会出现错误“function call is not allowed in a constant expression”。这是因为函数调用的结果无法在编译时确定。比如,如果在常量表达式中调用time函数,它的返回值是当前的系统时间,不可能在程序编译时确定。 另外,像动态内存分配、多态、异常处理等和运行时相关的操作也不能在常量表达式中使用。 如果需要在常量表达式中使用某个函数的返回值,可以考虑将函数转换为constexpr函数。constexpr函数是指可以在编译时计算的函数,可以用于常量表达式中。要让函数成为constexpr函数,它必须满足一定的条件,比如函数体必须是一个单一的return语句,函数参数和返回值类型都必须是字面类型等等。 总之,在C++中,常量表达式是很有用的东西,可以用于各种场合,比如声明数组大小、哈希表大小等等。但是需要注意的是,表达式中不能包含运行时相关的操作,比如函数调用和动态内存分配等。 ### 回答3: 在C++语言中,常量表达式是指编译时能够被计算出结果的表达式。这种表达式在编译期间就已经确定了值,因为它们不需要等到程序运行时才计算。 而如果在常量表达式里使用了函数调用,就会出现“function call is not allowed in a constant expression”的错误提示。这是因为函数的运行需要在程序运行时才能被计算出结果,而编译器在编译时并不知道函数运行后的结果。 例如,在以下代码中,我们定义了一个常量表达式,并在其中调用了sqrt函数: const double r = 5.0; const double PI = 3.14159; const double area = PI * sqrt(r); // 出错:function call is not allowed in a constant expression sqrt函数在运行时需要进行计算,因此不能用在常量表达式中。如果想要在常量表达式中使用sqrt函数,需要使用编译时计算替代函数调用。例如,我们可以手动计算出sqrt(r)的值,然后在常量表达式中使用这个值: const double r = 5.0; const double PI = 3.14159; const double sqrt_r = 2.236; const double area = PI * sqrt_r; // 正确,sqrt_r是编译时常量 在编写常量表达式时,需要注意避免使用函数调用、动态内存分配、非constexpr变量和语句(如if、for等)。只有在常量表达式中使用编译时常量才是安全有效的做法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值