【问题标题】:Where / How is null defined?null 在哪里/如何定义?
【发布时间】:2013-09-29 04:41:16
【问题描述】:

Null 没有声明?

我的代码:

// Include necessary libraries
#include <cstdlib> // Exits
#include <iostream> // I/O
#include <cstring> // String functions
using namespace std;

int main(int argc, char *argv[])
{
    //Declare local Constants and Variables
    const char SOKINP[19] = "23456789TtJjQqKkAa"; // Valid Input Characters
    char acCards [5]; // Array to hold up to five cards (user input)
    bool bErr;        // Loop on Error (Calculated)
    int  i,           // Loop variable (Calculated)
    iNbrCrd,          // Number of Cards 2-5 (user input)
    iNAces,           // Number of Aces (Calculated)
    iTS;              // Total Score (Calculated)

    ...

    for (i=0;i<iNbrCrd;i++){
       do {
           cout << "Enter Card #" << i << " (2-9,t,j,q,k or a)  >";
           cin  >> acCards[i];
           cout << endl;
           bErr = (strchr(SOKINP, acCards[i]) == null) ? true : false; // *ERROR*
       } while (bErr);
    }
    return EXIT_SUCCESS;
}

[错误] 'null' 未在此范围内声明

如何声明“null”?我尝试包括其他几个库。 我正在使用 Dev C++ v5.4.2

谢谢,~d

【问题讨论】:

  • 使用NULLnullptr
  • @ta.speot.is:你应该说:使用nullptr(或NULL)。顺序强调,强调很重要。
  • @ta.speot.is 在这种情况下,你应该说:“不要使用 Dev-C++,而是使用好的 C++ 编译器,使用 nullptr”。
  • @CareyGregory: this topic 给出了很多这样的例子。查看每个答案。
  • @Nawaz +1。重载示例令人信服。

标签: c++ null


【解决方案1】:

不是null。全部大写是NULL。如果写NULL不行,可以自己定义,使用

#define NULL 0

【讨论】:

  • @ValekHalfHeart 当您将其定义为(void *)0 时,您将完全收到类型不匹配警告。在 C 和 C++ 中,0任何类型 的空指针的有效文字,但由于 C++ 中严格的强类型化,(void *)0 仅适用于指向 void 的指针。因此,(void *)0 在 C 中比 0 更安全,但在 C++ 中是错误
  • 菜鸟错误。嗬!谢谢大家,答案是大写。
【解决方案2】:

使用NULL 而不是 Null。

如果您使用它来初始化指针并且您使用的是 C++11,请使用nullptr

虽然NULL 用于分配指针(尽管NULL 不是指针类型而是整数),但在以下情况下您可能会遇到问题:

void func(int n);
void func(char *s);

func( NULL ); // guess which function gets called?

更多详情请参考THIS

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 2016-02-19
    • 2016-12-31
    • 2011-10-08
    • 2010-09-30
    • 2013-04-04
    相关资源
    最近更新 更多