2

I have just started C++ and I'm not really sure what I am doing, but so far I have some "hello world" code from a tutorial.

   #include <iostream> 

//i only put this first line from users telling me to, it wasn't part of the original post. edited..


int main () {
    std::cout << "Hello, World!\n";
    return 0;
}

The compiler (Xcode for mac) says that there is an 'unexpected expression' right before the cout part of the code. I have no idea how to fix this problem. Can anyone help?

Connor Pearson
  • 63,902
  • 28
  • 145
  • 142
austinstout
  • 1,180
  • 1
  • 11
  • 14

2 Answers2

7

You need to include the header that declares cout:

#include <iostream>

If you still encounter an error after fixing that, then it must be caused by something in "ViewController.h".

Update: Also make sure the compiler recognises this as C++ by making sure the file extension is one of .cc, .cxx or .cpp (or .mm for Objective-C++.)

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • When I put this, the compiler gives me a bug? – austinstout Mar 03 '14 at 15:23
  • @zingzing45 What does it say? – Joseph Mansfield Mar 03 '14 at 15:24
  • Should I take out the viewcontroller part? – austinstout Mar 03 '14 at 15:24
  • @zingzing45: So what does the compiler say? Which line does it refer to? Is the problem in the code you've posted, or `"ViewController.h"`? – Mike Seymour Mar 03 '14 at 15:24
  • It only gives me an error on the iostream and it says the file is not found – austinstout Mar 03 '14 at 15:25
  • Are you sure you are compiling C++ and not C? – dornhege Mar 03 '14 at 15:27
  • @zingzing45: Then either your installation is faulty, or the compiler thinks you're compiling something other than C++. What file extension did you give the source file? It should be `.cc`, `.cpp` or `.cxx` to indicate C++. – Mike Seymour Mar 03 '14 at 15:27
  • The file extension is .m? I am using xcode.. so confusing – austinstout Mar 03 '14 at 15:27
  • 1
    @zingzing45: `.m` is for Objective-C, not C++. Change it to one of `.cc`, `.cpp` or `.cxx`. (Or `.mm`, if you want to write Objective-C++.) – Mike Seymour Mar 03 '14 at 15:28
  • I think I managed to make it a .cpp file, thank you for that. However, now it tells me that there is a 'lexical or preprocessor issue'? The error is in the iostream line – austinstout Mar 03 '14 at 15:32
  • @zingzing45: Are you still including `"ViewController.h"`? It sounds like that might be an Objective-C header. If you don't need it, get rid of it. If you do, and it is Objective-C, then you'll need to specify Objective-C++ (with a `.mm` extension). (If there's still a problem, then I'm afraid I'm out of ideas; I do very little MacOS programming). – Mike Seymour Mar 03 '14 at 15:37
  • I got rid of the viewcontroller part, and updated the original question with this info – austinstout Mar 03 '14 at 15:39
  • @zingzing45: So what exactly does the error message say? There's usually something more specific than a category like "lexical or preprocessor issue". – Mike Seymour Mar 03 '14 at 15:41
  • Ok, I solved the problem. I made a completely new project ( I was using another project I made a month ago that apparently was C only or something) and when I opened it, it already had the hello world code I was trying to make. Anyways, thanks for your continued support, I don't think I would've made it out of this mess without you – austinstout Mar 03 '14 at 15:47
1

As mentioned by the earlier contributors, you will need to #include <iostream> which will provide the implementation for std::cout. For a simple hello world C++ program, you don't need #include "ViewController.h" (not sure what it is because it is not a standard include).

Why don't you remove the #include "ViewController.h" and then post the error that you are seeing? I'm sure folks here will be able to help you out.

ap-osd
  • 2,624
  • 16
  • 16
  • Ok, I deleted the viewcontroller part – austinstout Mar 03 '14 at 15:28
  • What's the error that you get? Please copy/paste the full error output by the compiler. – ap-osd Mar 03 '14 at 15:37
  • Code so far: #include int main () { std::cout << "Hello, World!\n"; return 0; } Error: ('iostream' file not found )on the first line – austinstout Mar 03 '14 at 15:41
  • Unfortunately, I am not familiar with how XCode sets up its include paths. – ap-osd Mar 03 '14 at 15:49
  • Yeah, neither do I, which appears to be the main problem here. Fortunately, I managed to solve the problem by scrapping the project I was using and making a completely new one. Thanks for the answer! – austinstout Mar 03 '14 at 15:50
  • 1
    `iostream` is C++ functionality. Unfortunately, I am not familiar with how XCode sets up its include paths or decides on the compilation (C, ObjectiveC, C++, ObjectiveC++). This post might give you some ideas. http://stackoverflow.com/questions/13857805/iostream-not-found-in-xcode – ap-osd Mar 03 '14 at 16:01