Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 567
Netorica Avatar asked Mar 28 '13 06:03

Netorica


1 Answers

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

like image 78
Alok Save Avatar answered Oct 16 '22 08:10

Alok Save