在C++中,我可以通过引用传递多少个参数,而不会出现异常行为
How many parameters can I pass by reference in C++, without getting abnormal behavior
我遇到了一个函数问题:
int parsearRestricciones(char linea[], unsigned int& x, unsigned int& y, unsigned int& tiempo, char restric[])
在这个函数中,我解析linea[]。
输入包含:三个无符号整数和一个标点符号字符串。我需要这样读。当我将atoi(linea+offset(分配给变量tiempo时,问题就出现了。在函数外部(即main((中(,tiempo的值与其内部的值不同。我只在tiempo中遇到了问题(我用指向结构的指针替换了x、y和tiempo。它有效(
可能是什么问题?
谢谢你的帮助。
-----再次编辑
完整代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <cassert>
#define MAX_RESTRIC 3 // Tres sentidos. Si hay 4, se usa '+'
#define MAX_LINEA 80
#define entrada cin
using namespace std;
int parsearRestricciones(char *linea, unsigned int& x, unsigned int& y, unsigned int& t, char *restric) {
// ! solo funciona si x,y,t,restric estan en una misma linea (i.e. no hay CR LF)
int i=0, j=0;
//Parsea x de la casilla
x = atol(linea+i);//strtol(linea,(char**)NULL,10);
while (isdigit(linea[i])) i++;
while (isspace(linea[i])) i++;
//Parsea y de la casilla
y = atol(linea+i);
while (isdigit(linea[i])) i++;
while (isspace(linea[i])) i++;
if(linea[i] == ' ')
return -1;
//Parsea tiempo
t = atol(linea+i);
cout << "---" << t << endl;
while (!ispunct(linea[i])) i++;
//Parsea restricciones
while (linea[i] != ' ' && linea[i] != ' '){
restric[j] = linea[i];
i++; j++;
}
restric[j]=' ';
return 1;
}
int main (int argc, char** argv)
{
// Sugerencia de argumentos
// --d (por Dijkstra)
// --axe (por A*, distancia euclideana)
// --axm (por A*, distancia manhattan)
// y dos màs que veremos luego :)
unsigned int X, Y;
unsigned int xi, yi;
unsigned int xf, yf;
unsigned int x,y,tiempo;
char restricciones[MAX_RESTRIC + 1];
//Buffer para parsear las lineas con restricciones
char linea[MAX_LINEA + 1];
bool finCasos = false;
bool siguienteCaso = false;
while (!finCasos)
{
if (siguienteCaso){
// Se leyó otro mapa antes que éste (hubo parseo, y quedo en xcasilla,ycasilla)
X = x;
Y = y;
siguienteCaso = false;
} else {
// Sino, lee por primera vez las dimensiones del mapa
entrada >> X >> Y;
}
if ( X == 0 && Y == 0 )
finCasos = true;
else {
entrada >> xi >> yi;
entrada >> xf >> yf;
// Lee restricciones hasta que encuentra una linea sin ellas (sin tiempo ni direccion)
// se asumira, que corresponde a las dimensiones del siguiente caso, y los usará en la siguiente
// iteracion
while(!siguienteCaso) {
cin.get(); //lee un ' ' que quedó (?)
cin.getline(linea, MAX_LINEA+1);
if ( parsearRestricciones(linea,x,y,tiempo, restricciones) == -1 ) {
siguienteCaso = true;
} else {
cout << "X = " << x << endl;
cout << "Y = " << y << endl;
cout << "tiempo = " << tiempo << endl;
cout << "restric = " << restricciones << endl;
int j=0;
cout << "restric = " ;
while(restricciones[j]!=' '){
cout << restricciones[j];j++;}
cout << endl;
//-- agregar datos al grafo/mapa
}
}
// Resolver usando algun algoritmo
//--- resolver(MAPA)
}
}
return 0;
}
CFLAGS-墙-管道-ggdb-DONLINE_JUDGE-DNDEBUG(Makefile还为uvaonlinejudge构建了另一个源代码(
输入:
101
10
1
1
2
2
1000 10000 100000 +++++
输出:
---100000
X = 1000
Y = 10000
tiempo = 65579
restric = +++++
restric = +++++
^X^C (I did break)
我刚刚在Windows中测试了这个程序(使用代码::块,默认设置(,它工作了:/
顺便说一句,我在Virtualbox 中使用Ubuntu
你能告诉我我做错了什么吗?
在C++中,我可以通过引用传递多少个参数,而不会出现异常行为
想要多少就有多少!
没有这样的限制。然而,您的函数并没有以任何方式使用tiempo,而是使用了一些名为t的变量:t = atol(linea+i);
相关文章:
- 有没有办法捕获 STL 函数未被赋予正确参数的异常?
- 调用参数不是原子参数的函数是此代码引发异常的原因吗?
- 我正在尝试创建一个使用 c++ 中的参数包构造函数的异常类
- 如何修复"ctypes"。参数错误:参数 2:<键入"异常.类型错误">:RaspberryPi 中的错误类型"错误
- 当构造函数的参数类型错误时引发异常
- 在 Myfile.exe 中0x00831D39时未处理的异常:0xC00000FD:堆栈溢出(参数:0x0000000
- 为什么可以指定异常规范的参数类型
- 如何避免字符串到整数转换情况下的无效参数异常
- basic_string::替换的超出范围异常,而在范围内,正如调试相同参数的输出所证明的那样
- 自动存储中没有无参数构造函数的类对象和异常
- 为什么初始化构造函数列表参数时会发生异常?
- JNI自定义异常具有多个参数
- 防止在将未引发的异常作为参数传递时在"throw e;"中切片
- 传递参数时引发异常
- 如果在可变参数模板解包中达到终止函数,则出现异常
- 与构造函数参数相关的异常安全的习语
- 从 noexcept 函数参数的构造函数引发的异常会立即导致对 std::terminate() 的调用吗?
- 插入>>运算符重载:从 CIN 检索对象的 CTOR 参数时的异常处理
- 接收器参数和异常
- 抛出类似可变参数宏包装的函数,替换抛出的异常