-1

I have tried to add a new .cpp file into a C project in Visual Studio, with the following code:

#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
    cout << "Hello World!" << endl;
    return 0;
}

However, the #include <iostream> alone produced 100+ errors, so I had to comment that to see if that was the problem. After doing so, this new error popped up: identifier "using" is undefined. I am guessing it is either because the project was somehow configured for C, or it is because there are things prepended to the file by the preprocessor that I am unaware of. How may I solve this?

KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
TommyX
  • 83
  • 2
  • 12
  • 5
    As you say, your project is probably configured in C. Look at the project configuration in Visual Studio – Hugal31 Dec 22 '16 at 17:30
  • 5
    This is `c++` code. It's not going to build as `c` code. – drescherjm Dec 22 '16 at 17:31
  • Rename the file's extension for C++ or go to the file's properties and tell the compiler to compile it as C++. Some common C++ extensions: cpp, cc and C (capital letter on platforms that use case-sensitive filenames). – Thomas Matthews Dec 22 '16 at 17:32
  • It turns out I need to change one setting: C/C++ -> Advanced -> Compile As. Thanks. – TommyX Dec 22 '16 at 17:33
  • 1
    My advice is to select the correct file extension to avoid this issue. – drescherjm Dec 22 '16 at 17:42
  • 1
    I subscribe. By leaving the global settings blank, and giving the right extension to the file, you won't have to do this step every time when you add a new _.cpp_ file. [Here](https://msdn.microsoft.com/en-us/library/032xwy55.aspx)'s how it works. – CristiFati Dec 22 '16 at 17:57
  • 2
    @TommyX, I hope that indeed works. Be aware that C and C++ are distinct languages, each has features and syntax not supported by the other, and [***not all code that is valid in both has identical meaning in both***](http://stackoverflow.com/questions/12887700/can-code-that-is-valid-in-both-c-and-c-produce-different-behavior-when-compile). Be very careful, therefore, about compiling C code as if it were C++. – John Bollinger Dec 22 '16 at 18:02

1 Answers1

2

I have tried to add a new .cpp file into a C project in Visual Studio

You're aware that it's C++ code in a C project, good. There are differences between C and C++ that you should be aware of; C does not have using namespace, or <iostream>, or the << operator (for printing at least), or cout.

If you want C++ files to work in your C project, you can either configure the project to compile as C++, convert your C++ file(s) to C code, or simply ensure that all C++ files have the .cpp extension (which would only work if the C project was on default settings).

Random Davis
  • 6,662
  • 4
  • 14
  • 24
  • 4
    This is not quite true. There can be projects with mixed (_C_ / _C++_) files. By naming the files properly (in terms of extensions: _.c_ / _.cpp_), and not messing with _VStudio_ __default__ settings (this is a general rule for inexperienced ones) it should work fine, with no further user interventions. – CristiFati Dec 22 '16 at 17:47
  • 1
    Well, C does have the `<<` operator, but only in its original meaning :) – Christian Hackl Dec 22 '16 at 18:40
  • You should try to keep your language neutral - do not assume and do not ask questions inorder to answer question, otherwise I agree with what you state – serup Apr 20 '18 at 09:47