Compiler error with cout object
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Compiler error with cout object

  1. #1
    Join Date
    May 2011
    Posts
    41

    Compiler error with cout object

    Hello, I am asp.net C# developer. I decided to tackle C++, so I started today. This is probably something simple I am sure but I cannot figure out what the problem is. I would appreciate if someone could show me what is wrong and why.

    Code:
            srand(static_cast<unsigned int>(time(0)));
    	int choice = rand() % NUM_WORDS;
    	string theWord = WORDS[choice][WORD];
    	string theHint = WORDS[choice][HINT];
    
    	string jumble = theWord;
    	int length = jumble.size();
    	for(int i = 0; i < length;i++)
    	{
    		int index1 = (rand() % length);
    		int index2 = (rand() % length);
    
    		char temp = jumble[index1];
    		jumble[index1] = jumble[index2];
    		jumble[index2] = temp;
    	}
    
    	cout << "\t\t\tWelcome to Word Jumble!\n\n";
    	cout << "Unscramble the letters to make a word.\n";
    	cout << "Enter 'hint' for a hint.\n";
    	cout << "Enter 'quit' to quit the game.\n\n";
    	cout << "The jumble is: " << jumble;
    The error is happening on the last output operator, just before the jumble variable on the last line.The error is:

    Code:
      Intellisense: no operator"<<" matches these operands
    operand types are: std::basic_ostream<char, std::char_traits<char> <<std::string
    I understand what its saying, but jumble is a std::string

    Here are my preprocessor directives and using statements
    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    using namespace std;
    Thanks in advance!

  2. #2
    Join Date
    Apr 1999
    Posts
    27,433

    Re: Compiler error with cout object

    Quote Originally Posted by remojr76 View Post
    Hello, I am asp.net C# developer. I decided to tackle C++, so I started today. This is probably something simple I am sure but I cannot figure out what the problem is. I would appreciate if someone could show me what is wrong and why.
    Recognize the correct header files to include for each class that you use in your program.

    The std::string class requires <string>, but you did not include it.

    Second, when you post code that doesn't compile, it is always best to post the smallest complete code, without breaks, as to what doesn't compile. In other words, one edit window, where it just requires a copy and paste of the entire code in the compiler to see the error. To duplicate your error, I would have to rearrange the code, introduce #define constants, add ending curly braces, etc (and I still may not be able to duplicate your error after all of that work).

    Last, C++ is much different than C# in many ways. Try to access an array out-of-bounds in C++, and do the same thing in C#. Run the program and tell us what happens with each. Don't be surprised if the C++ program just goes on its merry way as if nothing is wrong. That is the major difference between the two languages at runtime -- C++ allows wrong programs to run hapharzardly, while C# will automatically tell you that you've done something wrong.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 15th, 2012 at 10:54 PM.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,433

    Re: Compiler error with cout object

    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include <string>
    
    using namespace std;
    #define NUM_WORDS 10
    int WORD, HINT;
    string WORDS[10][10];
    
    int main()
    {
        srand(static_cast<unsigned int>(time(0)));
        int choice = rand() % NUM_WORDS;
        string theWord = WORDS[choice][WORD];
        string theHint = WORDS[choice][HINT];
        string jumble = theWord;
        int length = jumble.size();
        for(int i = 0; i < length;i++)
        {
    	int index1 = (rand() % length);
    	int index2 = (rand() % length);
    	char temp = jumble[index1];
    	jumble[index1] = jumble[index2];
    	jumble[index2] = temp;
        }
        cout << "\t\t\tWelcome to Word Jumble!\n\n";
        cout << "Unscramble the letters to make a word.\n";
        cout << "Enter 'hint' for a hint.\n";
        cout << "Enter 'quit' to quit the game.\n\n";
        cout << "The jumble is: " << jumble;
    }
    Excluding the precompiled header "stdafx.h" (which is not required if all you're doing is learning C++), the code above compiles correctly. The code is an example of a complete program. Whether it runs correctly or not isn't important if the goal is to show what compiles and what doesn't compile correctly.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    May 2011
    Posts
    41

    Re: Compiler error with cout object

    Thanks.

    Sorry I posted that way because I thought I was logically explaining my issue, but I see your point!

    Quit a difference with the array, instead of an out of bounds exception, I got an error saying I was trying to use a variable that had not been initialized.

    A couple of other questions if you do not mind.

    I am assuming that when I use a using statement I am making a reference to a dll. correct?
    I am also assuming that when I use an #include its a reference to a class in that dll.?

    if my assumptions are correct why do you need to use an #include when you are using the namespace that the class is in?

    thanks in advanced.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,433

    Re: Compiler error with cout object

    Quote Originally Posted by remojr76 View Post
    Quit a difference with the array, instead of an out of bounds exception, I got an error saying I was trying to use a variable that had not been initialized.
    In reality, there is no guarantee if you receive any message whatsoever. Compile a release version and you will see that message doesn't show up.

    Again, C++ is not C#. C++ has something called undefined behaviour, where if you make a mistake, there is no guarantee how your program will run -- it may even "work" on your machine and fail on another machine.
    I am assuming that when I use a using statement I am making a reference to a dll. correct?
    No. The using clause introduces a namespace to the current source file.
    I am also assuming that when I use an #include its a reference to a class in that dll.?
    No.

    There is no "dll" in C++. C++ is a general purpose language that knows nothing about DLL's. What you have is a text file that contains C++ source code, and the compile checks the syntax, and if it's OK, produces an intermediate object file. It is then the linker that links these object files into a final executable.
    if my assumptions are correct why do you need to use an #include when you are using the namespace that the class is in?
    Your assumptions are so far off, I highly suggest you get a book on C++ programming instead of guessing and equating parts of C++ to C#. The biggest mistake you would make is to assume that C++ and C# are the same, but with only different syntax. C++ is a language you cannot learn on assumptions or learn based on another language you may have used.

    You must start with the basics, otherwise you'll just produce applications that are either so buggy and/or coded so poorly that it is obvious to a seasoned C++ programmer that the code was produced by someone who doesn't know the language.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; December 16th, 2012 at 01:34 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center