-3

I just recently made an app on Visual Studio, but now it's giving me this silly problem I can't make sense of. I've been doing this stuff in my other previously made apps and everything worked fine. I do it now and it seems to be giving me problems.

I tried starting a new app and this is what I get when I include I/O stream and use namespace std.

The code:

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include "stdafx.h"
using namespace std;

int main()
{
    cout << "Hi" << endl;

    return 0;
}

I really don't get what the problem is. Any help would be appreciated.

The errors:

'cout': undeclared identifier
'endl' : undeclared identifier
solarissmoke
  • 30,039
  • 14
  • 71
  • 73
Osama Kawish
  • 312
  • 1
  • 5
  • 15
  • 3
    The problem is very simple: all questions on stackoverflow.com must include all pertinent information in the question itself ***as plain text***. If you can't be bothered enough to cut and paste the actual code, and the compilation error message, to make it easier for everyone to read your question, why do you expect anyone to bother to spend time figuring out and helping you? – Sam Varshavchik Dec 16 '17 at 20:45
  • @SamVarshavchik To be honest, I kind of felt that the image would've expressed my problem a bit more clearly and efficiently. But added the code regardless. – Osama Kawish Dec 16 '17 at 21:41
  • Before reopening, OP should copy-paste the error message as text instead of an image. – zett42 Dec 16 '17 at 23:55
  • Added errors as text. – Osama Kawish Dec 17 '17 at 00:00

1 Answers1

0

This problem is very easy to fix, all you need to do is move the
"stdafx.h" to the very top of your file.

SigHound
  • 61
  • 5
  • 3
    Alternative: Move the `#include ` into stdafx.h to take advantage of precompiled headers. That's what stdafx.h is for. – user4581301 Dec 16 '17 at 20:52
  • @user4581301 That would be a rather poor advice. Relying on some other header to include stuff necessary for this particular file creates extremely fragile code. – user7860670 Dec 16 '17 at 21:03
  • 1
    @VTT regardless, that is what stdafx.h is for. It is a container for headers that are to be precompiled. Is it overkill given the size and scope of the asker's program? Yep. Is it better that the asker just turn precompiled headers off and remove stdafx.h entirely? Yep. Notes on how to do that here: https://stackoverflow.com/questions/7261707/how-to-avoid-precompiled-headers – user4581301 Dec 16 '17 at 21:08
  • If you don't mind me asking, do you mind explaining why this matters? – Osama Kawish Dec 16 '17 at 21:44
  • @user4581301 Well, technically I had the problem with a different app I was trying to make. I made a new one to see if the problem persisted. Turns out it did. (Will be homest: I didn't care to try understanding what that file was until now.) – Osama Kawish Dec 16 '17 at 21:53
  • @OsamaKawish The compiler ignores everything in the file before the line `#include "stdafx.h"`, that's just how it is. – zett42 Dec 16 '17 at 23:52
  • @OsamaKawish "I made a new one to see if the problem persisted." Exactly the right thing to do. You reduced what was probably a complicated program to a simple, easy to experiment and demonstrate one. The fact that you needed to know about this poorly advertised little foible of Visual Studio can't be helped. Here is a great write up on stdafx.h: https://stackoverflow.com/questions/4726155/whats-the-use-for-stdafx-h-in-visual-studio – user4581301 Dec 17 '17 at 05:06