skip to Main Content

As I am reading "Programming: Principles and Practices Using C++" book,
I tried the "The first classic program" as shown in this code:

// This program outputs the message "Hello, World!" to the monitor

import std; // gain access to the C++ standard library 
int main() // C++ programs start by executing the function main 
{ 
  std::cout << "Hello, World!n"; // output "Hello, World!" 
  return 0;

}

But when I compiled this code in vs code it returns a confusing error to me and I wonder why it isn’t valid.
What do you think?

I tried implementing this code in vs code, but I got this error:

Cpp.cpp:1:1: error: 'import' does not name a type
 import std;
 ^~~~~~
Cpp.cpp: In function 'int main()':
Cpp.cpp:4:9: error: 'cout' is not a member of 'std'
         std::cout<<"Hello,Worldn";
         ^~~

2

Answers


  1. from my view , i thinks the use of import std; may be the problem . If you use #include instead it will work perfectly fine.

    for use of import std; latest version of gcc will work ,i think

    Login or Signup to reply.
  2. #import is a Microsoft-specific thing, apparently for COM or .NET stuff only.

    For your code you just need to add iostream header file and need to remove import std.

      #include <iostream> // we are using cout(output stream) so need to add this header file
        
    int main() // C++ programs start by executing the function main 
    { 
      std::cout << "Hello, World!n"; // output "Hello, World!" 
      return 0;
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search