-1

I stumble on the following compilation error in C++ with g++ compiler:

error on line line 1 with message:
invalid preprocessing directive #a

(with a caret above the character a) which is followed by another,probably consequent, error on line 4 with message:

cout was not declared in this scope. 

The editor i am using is Code blocks 10.05 with mingw.I tried removing .h extension from the iostream file include statement;switching among different File encoding options;and replacing the angular bracket with single quotes and double quotes as well.i am stuck on it.Pardon if it is a duplicate(although i went through several already asked questions in relevance). The following code illustrates the problem:

#‪include ‬<iostream.h>
int main()
{
cout<< "abc"+8;
cout<< "def"+4;
cout<< "ha";
return 0;
}
Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
Chandan Kumar
  • 303
  • 1
  • 3
  • 11

5 Answers5

3

cout exists within the namespace std

So either

#‪include‬<iostream>
//...
std::cout << "abc" << 8;
//...

or

#‪include‬<iostream>
using namespace std;

//...

or

#‪include‬<iostream>
using std::cout;

//...

I tend to prefer the 1st if I'm only using it once or twice, The second if I'm using a lot of different pieces from a namespace (and only in a cpp file), or the third if I'm only using a piece or 2 from a namespace but using the same couple many times.

Additionally as stated in the comments, don't use the 2nd one in headers. See: "using namespace" in c++ headers

Also, you have an invalid character in your #include. You can see it in a hex editor or Note how stackoverflow doesn't highlight them the same:

#‪include‬<iostream>
#include<iostream>

Fully working code:

#include<iostream>

using std::cout;

int main()
{
    cout << "abc" << 8;
    cout << "def" << 4;
    cout << "ha";
    return 0;
}

Produces the following output abc8def4ha after I corrected for trying to add 8 to a char*

Community
  • 1
  • 1
McAden
  • 13,714
  • 5
  • 37
  • 63
  • 4
    But you should _never_ put that second one in a header, and a lot of people hate it for similar reasons. – Mooing Duck Aug 08 '13 at 21:19
  • @McAden,in the first case of your suggested solution,the error message on line 1 remains the same.However, the error message for line 4 and the rest cout invocations is:'cout' is not a member of 'std';In the second case, the error list persists as stated in the question;The third case also generates the following errors:Line 1 with message:invalid preprocessing directive #a(with a caret on a);Line 2 with message 'std::cout' has not been declared;Line 5 with message 'cout' was not declared in this scope. – Chandan Kumar Aug 08 '13 at 22:01
  • 1
    Your problem is an invalid character in your #include. I'm guessing you copy/pasted from someplace – McAden Aug 08 '13 at 22:09
  • Thanks! Retyping the code(mentioned in the question)all over again, with addition of statement 'using std::cout' works correctly and gives the following supposed output hahaha -to my surprise!! (because i do not know how??). – Chandan Kumar Aug 08 '13 at 23:05
  • 1
    You had an invalid character. See above where StackOverflow's syntax highlighter doesn't even treat them the same. I'm glad the answer was helpful. – McAden Aug 08 '13 at 23:21
2

You have to use std::cout, which means that the "cout" keyword is part of the standard library.

atmaere
  • 345
  • 1
  • 8
  • 18
2

The "invalid directive" error is caused by some invisible Unicode characters in the #include directive; perhaps you copied this from a website that embedded some formatting characters in the code. They can be seen in the question, if you look at the source in a hex editor. That error should be fixed by deleting and retyping the #include line.

You'll probably have other errors, since the code is fifteen years out of date; most modern compilers don't provide pre-standard libraries. These days, the standard library headers don't have a .h extension:

#include <iostream>

and nearly all the names they declare are scoped inside the std namespace:

std::cout << "ha";

Finally, "abc"+8 doesn't do anything sensible. The string literal is an array of four characters, and +8 tries to give you a pointer to the ninth character, which doesn't exist. The result is undefined behaviour.

If you want to print "abc" followed by "8", then you want:

std::cout << "abc" << 8;
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • i have already mentioned in the question that i tried without a .h extension in declaration of iostream as well to bring no difference to the error list.And even if i write 'std::cout << "ha";' only, inside main-i get the same two errors:Line 1.'invalid preprocessing directive #a(with a caret above character a)';Line 2:'cout' is not a member of 'std'.And i also wonder why i should receive negative voting if i am trying to learn something... – Chandan Kumar Aug 08 '13 at 22:30
  • 1
    @ChandanKumar: Sorry, I didn't notice that you'd already tried correcting it. I've no idea what the "invalid directive" error's about; it sounds like you might have somehow ended up with non-ASCII characters in the source file. Try deleting and retyping the `#include` line. – Mike Seymour Aug 08 '13 at 22:32
  • Thanks! it was really encouraging. And yes,i copied the code from somewhere else.Let me try and see if retyping it manually solves the problem. – Chandan Kumar Aug 08 '13 at 22:50
  • 1
    @ChandanKumar: Indeed, if I look at your code in a hex editor I can see some weird Unicode formatting characters around `include`. That's likely to break the compiler. I've updated the answer with the real problem. – Mike Seymour Aug 08 '13 at 22:59
  • Thanks again to all of you! Retyping the code (mentioned in the question) all over again works correctly and gives the following supposed output: hahaha ;to my surprise (because i do not know how :)). – Chandan Kumar Aug 08 '13 at 23:07
  • 1
    @ChandanKumar: "hahaha" will be the result of undefined behaviour: if you get that, it's because your compiler happens to put the string literals in memory one after the other, so that the dodgy arithmetic (`"abc"+8`) happens to give you a pointer to `"ha"`. Other compilers might print something else, or crash, or reformat your hard drive, so don't try to write code like that yourself. – Mike Seymour Aug 08 '13 at 23:11
  • Got it.The Discussion reaches a conclusion. – Chandan Kumar Aug 08 '13 at 23:27
1

Try using it like this:-

#‪include ‬<iostream>
using std :: cout;

cout is the part of std library

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

If you've got caret above a, try retyping your #include. You might accidentally type alternative i which looks similar but has different code. Suggestions about std:: are only relevant for the second error you're getting. I also didn't fully understand what you were trying to achieve with "abc"+8.

Karadur
  • 1,226
  • 9
  • 16