5

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

Netorica
  • 18,523
  • 17
  • 73
  • 108
  • 1
    Don't set `std::string`s to null (an exception would be thrown had this run). `std::string word;` is an empty string. – chris Mar 28 '13 at 06:07
  • 3
    Woah woah... you can't null objects like that. This isn't Java. – Mysticial Mar 28 '13 at 06:07
  • @chris so its really impossible to set `NULL` values in C++? – Netorica Mar 28 '13 at 06:08
  • @Mahan You can set *pointers* to `NULL`. But not *objects*. – Mysticial Mar 28 '13 at 06:09
  • @Mahan, Set pointers to `NULL` (or better, `nullptr`). Types aren't nullable by default. If you want that, use a pointer or `boost::optional`. – chris Mar 28 '13 at 06:09
  • @Mysticial I told you before I worked already in other languages.. so surprised `NULL` doesn't work here :(( – Netorica Mar 28 '13 at 06:11
  • 3
    @Mahan Yeah, C and C++ are different in this respect. I suggest you read up on C++ objects first. Pointers can come later. (and you arguably don't need them much in modern C++) – Mysticial Mar 28 '13 at 06:13
  • 4
    Something that might help you: In some other languages you are probably familiar with, when you declare a variable, you are actually declaring a reference that might refer to a valid object, or it might refer to nothing (a null reference), and it can be moved from object to object. In C++, when you declare a variable, you are creating an object. The name you use is that object's name, so it can't refer to another object, and it can't refer to nothing. That's what pointers are for, though pointers themselves are actually objects too, and they refer to objects by storing their address. – Benjamin Lindley Mar 28 '13 at 06:21

2 Answers2

9

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

Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • a pointer? can you point me to a tutorial where I can learn how to use pointers? so I can set `NULL` values? – Netorica Mar 28 '13 at 06:09
  • 2
    @Mahan the point of c++ with stl is that you don't need pointers. – Fantastic Mr Fox Mar 28 '13 at 06:12
  • 1
    @Mahan: Please pick up a good book from the link marked inline to answer – Alok Save Mar 28 '13 at 06:13
  • oh yeah this works for now.. thanks, empty string not null string – Netorica Mar 28 '13 at 06:14
  • 1
    @Mahan, it's incredible, beginners love pointers, pointers are where all the mistakes happen, so beginners write lousy code. Ben's point is right on the money, you don't need pointers *most* of the time. And because in a language without garbage collection they are very tricky you should avoid them, at least until you get a bit more experience. – john Mar 28 '13 at 06:40
3

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;.

Community
  • 1
  • 1
didierc
  • 14,572
  • 3
  • 32
  • 52