Does anyone know how to change the settings for the automatic creation of the main method in Netbeans (for C++) at least?
The problem is that the main method that is automatically created forgets to
#include <iostream>
So when I do a simple "helloworld" program, even though it does "using namespace std", I get an error on the line with the cout and the endl.
# include <cstdlib>
using namespace std;
int main(int argc, char** argv) {
cout << "Hello!" << endl;
return 0;
}
I believe you can change it in the menu:
tools->models, choose the template that you want to change and click in "open in editor"
I never try it, but you can try and see if it works
Related
I'm using the following application
#include <iostream>
using namespace std;
void print(char* s)
{
cout << s << endl;
}
int main(int argc, char* argv[])
{
print("a");
print("b");
print("***");
print("c");
print("d");
}
When I run it with the debugger under VSCode (using "Native Debug" extension), it never pass the second print and this is my output:
a
b
It never pass print("***), even if I put a breakpoint after that line it doesn't reach it.
if I comment out print("***"), the application finish with the correct output:
a
b
c
d
The only thing I managed to figure out is that there is and issue printing "*" character and if I replace it with any other character all works fine. Why do I see this behavior and how can I fix this without having to change the code?
#molbdnilo, seem to be correct. It looks like a bug in "Native Debug" extension. The problem is not reproduced when another extension is used (I tried with Microsoft's "C/C++" extension)
I am using visual studio 2013 to build a C++ console application.This is my code which gives an error. I need to write to the console for every function in my application.
// RAT.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Process started";
return 0;
}
It gives the following error,
Error:namespace "std" has no member "cout"
I dont understand why this basic "cout" gives a such error :(
Please help me I am totally confused....
You need to place #include <iostream> as your header.
When I include the required library, the "#include.." line doesn't show any warning. But when I use the functions in that library, I find the Vim shows that "..use of undeclared function...". It seems that the library is not correctly included. So I want to know how to figure out this problem?
The screenshots for this question are attached as follows:
Try including it as follows:
#include <stdlib.h> //use <> instead of ""
Also, the "printf" function comes from the "cstdio" library so try implementing that library as well,
#include <stdio.h>
UPDATED
The easiest way to fix that problem is;
Include the stdio.h library
#include <stdio.h>
Then, instead of typing;
printf('s');
you do,
printf("s");
Now, if you really want to print a character 's', then use,
printf("%c", 's'); // Tells the printf function that 's' is a character
The final code would look like;
#include <stdio.h>
int main(int argc, char** argv) {
printf("s");
printf("%c", 's');
return 0;
}
Now, your comment was that "cout" does not work. In order for "cout" to work you need to include the iostream library:
#include <iostream>
Then, you can use "cout" in your code;
std::cout << 's';
std::cout << "s";
Or you can include "namespace std" and the "iostream" library to avoid using std:: before "cout"
include <iostream>
using namespace std;
Thereafter, use cout without std::
cout << 's';
cout << "s";
The final code would be;
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
cout << 's';
cout << "s";
return 0;
}
If you want to learn more about what is in the iostream library and how to use it I recommend using this site:
http://www.cplusplus.com/reference/iostream/
Also, for the stdio.h,
http://www.cplusplus.com/reference/cstdio/
When I started to use VS2013, I created just very basic application like this.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello!";
return 0;
}
It crashed and when I commented out the #include <iostream> its no longer crash. I did several research on this error but nothing is suitable for my situation. This is the error :
Thanks for all your helps.
Once you create a new project, if you create it as an empty project I don't think you will face this issue. Then, you start it from scratch and you use int main() instead of that _tmain(...) and DO NOT EVER use using namespace std;
start a new EMPTY project and use something like this:
#include <iostream>
int main()
{
std::cout << "Hello World";
return 0;
}
Well, I just wrote a simple logger program with log4cxx lib, It is working fine so far, but then I realized the exception handling doesnt work as I expected:
#include <iostream>
#include <log4cxx/logger.h>
#include <log4cxx/xml/domconfigurator.h>
#include <log4cxx/helpers/exception.h>
using namespace std;
using namespace log4cxx;
using namespace log4cxx::xml;
using namespace log4cxx::helpers;
int main(int argc, char *argv[])
{
/* some code here ... */
try
{
DOMConfigurator::configure("/path/to/logcfg.xml");
}
catch (Exception&)
{
cout << "error: problem in reading log config file" << endl;
/* here I want to free up some objects! */
exit(EXIT_FAILURE);
}
}
So, now lets say the logcfg.xml does not exist, the program will print out this message and exit:
log4cxx: Could not open file [/wrong/path/to/logcfg.xml].
Which seems to me, it never reached my exception handler, but raised and handled in library itself. Could you please tell me what is proper way to handle such case ?
Thanks