简体   繁体   中英

Comparing to a Null in C++

Ok please have a close inspection on my code below, its just a part of a function

void RepeatWord(){
    system("cls");
    string word = NULL;
    string newword = NULL;
    int repeats = 0;
    while(true){
        if(word == NULL){
            cout<<"Currently no word is stored..";
            cout<<"\n\nPlease enter a word: ";
            cin>>word;
        }
....

well I been working with other programming languages and I am always doing a comparison with a NULL value but in C++... it seems to be a different situation. The error says..

error: no match for 'operator==' in 'word == 0'

well I was wondering I am just comparing to a NULL and I really don't know why this is wrong. Is comparing a value to a NULL to C++ is different? please teach me. Thanks

Note: I know more ruby than java

You are trying to compare a object with NULL , You cannot compare objects with a NULL . You need to compare a pointer. In doing so you check if the pointer is pointing to something that is not a valid object.

In your case you want to check if std::string is empty. You can use member functions provided by std::string , std::string::empty() .

Considering Your questions, I am stressing the need for a good learning book:

The Definitive C++ Book Guide and List

If you are coming from the Java world, you will indeed discover that object in C++ are not handled the same way. In C++, unless explicitely stated, data are manipulated directly. In Java, object are completely managed by the VM, and because of this, it makes sense to let programs access them by reference only. Hence, whenever you write a Java program, you must tell the VM to allocate an object using new , and until then assign null to variable of class instances. Following the same principle, if you assign an object variable to another, both will refer the same object, and calling a method on either will call that method on the same underlying instance.

In C++, there are 2 different mechanisms to obtain the same result:

  • using pointers, whose syntax for the type T is

     T *t; 

    so that std::string *word; will define a variable named word holding a pointer to std::string .

  • using a reference:

     T &t; 

In C++, both of these are actually types, ie. std::string , std::string * , std::string & are three different types. The first one is really denoting the structured value itself, while the two other denotes an indirection to a value of the type std::string .

The difference between these types indirections are explained by this SO entry .

In your code, you will have to either replace all occurences of NULL by the empty string literal "" , or use pointers and allocate the instances using a new statement before trying to use the object - like in the line cin >> word; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM