首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

宏定义有关问题

2012-03-12 
宏定义问题//Testing the allege() macro#include iostream#include ALLEGE.h#include fstream#defi

宏定义问题
//Testing the allege() macro

#include <iostream>
#include "ALLEGE.h"
#include <fstream>
#define NDEBUG //turn off asserts
using namespace std;

int main()
{
  int i = 1;
  allege(i,"value must be nonzero");
  void* m = malloc(100);
  allegemem(m);
  ifstream nofile("nofile.xxx");
  allegefile(nofile);
  return 0;
}


#ifndef ALLEGE_H_INCLUDED
#define ALLEGE_H_INCLUDED

//ALLEGE.H --error checking
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

inline void allege_error(int val,const char *msg)
{
  if(!val)
  {
  fprintf(stderr,"error: %s\n",msg);
#ifdef NDEBUG //turn off debug
  exit(1);
#endif
  }
}

#define allege(expr,msg) \
{ allege_error((expr)?1:0,msg); \
  assert(expr); }
#define allegemem(expr) \
  allege(expr,"out of memory")

#define allegefile(expr) \
  allege(expr,"could not open file")

#endif // ALLEGE_H_INCLUDED


为什么在主程序中加了#define NDEBUG,但还是会提示assert检查错误?

[解决办法]
宏定义和使用的先后位置顺序要清楚

lz 的使用明明在 define 之前,当然不起作用

C/C++ code
#define NDEBUG //turn off asserts#include "ALLEGE.h"#include <fstream>
[解决办法]
NDEBUG只能关闭ASSERT(全部大写)这个宏,不会关闭真正的assert
探讨
//Testing the allege() macro

#include <iostream>
#include "ALLEGE.h"
#include <fstream>
#define NDEBUG //turn off asserts
using namespace std;

int main()
{
int i = 1;
allege(i,"value ……

热点排行