string receiveFromServer();
此函数返回从某个服务器接收的字符串.如果一路上出现错误(包括协议),我想返回一个NULL字符串.但是,这在c ++中不起作用(与java不同).我试过了:
string response = receiveFromServer();
if (response==NULL) {
cerr << "error recive response\n";
}
Run Code Online (Sandbox Code Playgroud)
但它不合法.由于空字符串也是合法的返回,我可以返回什么表明错误?
谢谢!
我正在研究测试OS(unix是我们的模型).我有以下问题:
以下哪两项不会导致用户程序停止并切换到操作系统代码?
答:程序发现错误并将其打印到屏幕上.
B.程序分配了内存,稍后将从磁盘中读取.
好吧,我有答案,但是,我不确定它们有多好.他们说答案是B.但是,B是用户使用malloc哪个是系统调用没有?分配内存不通过操作系统?为什么打印到屏幕应该需要操作系统呢?
谢谢你的帮助
这可能有点长,所以我道歉.考虑以下代码(我从中留下了一些不相关的部分).此代码接收指向结构(BoardP theBoard),x&y coords和值的指针.目标是将值放在结构中找到的2D数组中.如果coords超出范围,我必须增加表的大小,将旧数据复制到新数据并将值放在其位置.以及此代码在第一次调用时工作,但在第二次调用中它会崩溃并写入:
*** glibc detected *** ./b: double free or corruption (top): 0x092ae138 ***
Run Code Online (Sandbox Code Playgroud)
我找不到答案,希望你会帮忙.
这些是来自main()的调用
BoardP p = CreateNewBoard(10,10);
PutBoardSquare(p,10,5,'X');
PutBoardSquare(p,5,10,'O');
Boolean PutBoardSquare(BoardP theBoard, int X, int Y, char val) {
if (inBounds(X,Y,theBoard->_rows,theBoard->_cols)) {
theBoard->_board[X * theBoard->_cols + Y] = val;
return TRUE;
}
else {
int newRows = (X>=theBoard->_rows) ? (2*X) : theBoard->_rows;
int newCols = (Y>=theBoard->_cols) ? (2*Y) : theBoard->_cols;
BoardP newBoard = CreateNewBoard(newCols,newRows); //this creates a new Board with the new dimensions
if (newBoard == …Run Code Online (Sandbox Code Playgroud) 我有以下代码部分:
typedef struct Board* BoardP;
typedef struct Board {
int _rows;
int _cols;
char *_board;
} Board;
char* static allocateBoard(BoardP boardP, int row, int col) {
boardP->_rows = row;
boardP->_cols = col;
boardP->_board = malloc(row * col * sizeof(char));
return boardP->_board;
}
Run Code Online (Sandbox Code Playgroud)
我似乎无法弄清楚为什么它给出了错误预期的标识符或'(''''''之前它给出了错误,我将返回类型更改为char*.当它为void时没有给出错误.
还有一个问题:我被告知在使用malloc时需要强制转换,但是,这似乎在没有强制转换的情况下正常工作.在这种情况下是否需要?
谢谢
我今天早些时候提出了一个关于单身人士的问题,我在理解遇到的一些错误时遇到了一些困难.我有以下代码:
class Timing {
public:
static Timing *GetInstance();
private:
Timing();
static Timing *_singleInstance;
};
Run Code Online (Sandbox Code Playgroud)
#include "Timing.h"
static Timing *Timing::GetInstance() { //the first error
if (!_singleInstance) {
_singleInstance = new Timing(); //the second error
}
return _singleInstance;
}
Run Code Online (Sandbox Code Playgroud)
这段代码中有两个错误,我无法弄清楚.
该方法GetInstance()在标头中声明为static.为什么在cpp文件中我必须省略这个词static?它给出了错误:"无法声明成员函数'静态时序*Timing :: GetInstance()'具有静态链接".写它的正确方法是:
Timing *Timing::GetInstance() { ... }
Run Code Online (Sandbox Code Playgroud)为什么我不能写_singleInstance = new Timing();?它给出了错误:"对Timing :: _ singleInstance的未定义引用".我通过_singleInstance在cpp文件中定义为全局变量来解决此错误.
比方说我有:
class A {
public:
class B {
};
};
Run Code Online (Sandbox Code Playgroud)
这个公共嵌套类和它自己的cpp文件中定义的常规B类之间有什么区别,除了必须在第一个选项中使用A :: B的事实?
我似乎无法理解数组或二维数组上的不同声明之间的区别.
例如:
void swap(char **a, char **b) {
char *t = *a;
*a = *b;
*b = t;
}
int main(int argc, char **argv) {
char a[] = "asher";
char b[] = "saban";
swap(&a,&b);
}
Run Code Online (Sandbox Code Playgroud)
这段代码没有编译,它输出:
warning: passing argument 1 of ‘swap’ from incompatible pointer type
test.c:10: note: expected ‘char **’ but argument is of type ‘char (*)[6]’
Run Code Online (Sandbox Code Playgroud)
是不是a指向char数组的第一个单元格的&a指针,是指向指针的指针?
另一个例子是:
char (*c)[3];
char (*d)[3];
swap(c,d);
Run Code Online (Sandbox Code Playgroud)
不编译.. char (*c)[3]与指针相同char a[] = "ab"?
但是这确实编译:
char …Run Code Online (Sandbox Code Playgroud) 以下是使用Q的承诺的简短示例.
这是test1.js:
function testDefer() {
var deferred = Q.defer();
fs.readFile("foo.txt", "utf-8", function (error, text) {
if (error) {
deferred.reject(new Error(error));
} else {
deferred.resolve(text);
}
});
return deferred.promise;
}
Run Code Online (Sandbox Code Playgroud)
这是test2.js
(function(){
'use strict';
var test1 = require('./test1');
test1.testDefer().then(
function(data){
console.log('all good');
},
function(err) {
//upon error i might want to throw an exception, however, it is not thrown / ignored.
throw new Error('I want to throw this exception');
}
);
})();
Run Code Online (Sandbox Code Playgroud)
我想在test2中抛出异常,以防止承诺被拒绝(或者在某些情况下它被解决).无论如何,异常被忽略,程序完成而不抛出异常.
我的问题是,如何从成功/失败函数中抛出错误?
谢谢
我想知道在c ++中是否有任何方法可以处理具有给定精度的double.例如,数字3.1345将被视为3.13,数字0.009将被视为0(点后的精度为2).
我需要将它应用于数学运算.例如:
double a = 0.009;
double b = 3.12345
//a should be considered as 0, b as 3.12
double c = a*b // should output 0.
double d = a+b // should output 3.12
Run Code Online (Sandbox Code Playgroud)
因为函数setprecision()用于std我想知道是否还有其他功能可以做到这一点.
谢谢
我正在尝试为旅行商问题(TSP)编写遗传算法.如需选择我正在实施轮盘赌选择:http://www.edc.ncl.ac.uk/highlight/rhjanuary2007g02.php/
它基本上意味着被选择用于交配的概率与适应度函数的值成比例.
TSP最常见的适应性功能是路线的长度.然而,路线越"越短"越好.
如何编写描述路线短缺的适应度函数?
或者我如何将每条路线的真实长度转换为概率?