vscode keeps telling me nullptr is undefined - c++

I'm writing C++ in VSCode. The nullptr is marked by red cursive line and showing identifier "nullptr" is undefined. Though, I can run this code perfectly with code-runner extension. But how do I get rid of this false alert in VSCode?

As is pointed in the comments, in the project folder, just find .vscode/c_cpp_properties.json file and change the C++ version to C11. Problem fixed.

I had same issue previously. I tried only adding these two lines:
#include <iostream>
#using std::cout; using std::endl; using std::cin;
warning disappeared

Related

identifier "DDRB" is undefined - VS code / Visual studio

I am getting the following error when using the identifier DDRB:
identifier "DDRB" is undefined
But, when I click “go to definition”, the IDE does shows that it can find them. The code also compiles without any problem. I was using VScode first and setting intellisense to "tag parser" did work, but it also got rid of the error checking. So, I switched over to Visual Studio, but the issue remains. In both cases I included the AVR library.
I have googled quite a bit and found some solutions, but most were outdated or did not work. What can I do to resolve this issue?
"minimal reproducible example:"
#include <avr\io.h>
int main() {
DDRB |= (1 << DD3);
}
I can reproduce same issue in VS2017, and this one can be resolved by adding the #define __AVR_ATmega32U4__ above the #include <avr\io.h> like this:
#define __AVR_ATmega32U4__
#include <iostream>
#include <avr/io.h>
int main()
{
DDRB |= (1 << DD3);
}
After adding the macro definition, VS Intellisense option can recognize them well and the issue goes away. More details refer to Kissiel's reply. Thanks to him!
If you don't want to paste this definition into almost every file:
press f1
find C/C++; Edit configurations (UI)
paste your mcu name in Defines section e.g __AVR_ATmega32U4__
It worked for me in vs code.

cin and cout operator error C++

I am doing an assignment for school and at school we use codeblocks for our IDE, but I wanted to use visual studio at home. The problem is when I run my program on visual studio I continue to get an operator error on my cin and cout but only before a string. I tried #include and while the error lines went away when I build I get the same error before. I am new to C++ and rather confused any help is appreciated.
picture of code below
The #include <string> you tried would be correct.
Your main problem is the #include "stdafx.h". It's a Precompiled header. Visual studio puts it in by default although it isn't required. It has to be the first include if you use it:
Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.
Just put it as the first #include (or get rid of it completely) to fix your issue. Then the rest should start working normally.

use C++11 features in Eclipse

I have this piece of code :
#include <iostream>
#include <type_traits>
using namespace std;
int main(){
cout << std::is_same<int,int>::value; // this line is underlined (as a error)
return 0;
}
I am not quite used to code C++ in Eclipse.
So I have a Eclipse luna-SR2-32 (latest version), and I use tdm-gcc-4.9.2 as a compiler.
So the issue is that Eclipse underlines that line (look at the code) (is_same and value couldn't be resolved), however he has no problem including type_traits, I can even open it from eclipse Editor, and see that 'is_same' is the file.
When I compile and run it, it works fine but it stills underlined which is bothering me... It doesn't do this with some other c++11 features as tuple ...
I know this needs probably a simple configuration in Eclipse, but I tried many things already ... I added -std=c++11 in the compiler options (but nothing changed).
EDIT :
I tried the 3 first answers of this, but none of them worked ...
But I tried using instead and now it is not underlined anymore ... Why is that ? I know that some librairies are in tr1 because they were added later, but why does he recognize and can run it, but still underlines my lines when I use it ?

C++ cout gives undeclared identifier

So, I have this question. Why does cout throws
error C2065: 'cout' : undeclared identifier
I am using Visual Studio 2012 as an IDE and I am writing a school project. I have everything done except an example file. So I am trying to write something on the screen like this:
#include "iostream"
#include "stdafx.h"
using namespace std;
int main()
{
cout<<"example";
return 0;
}
So the problem is with cout... printf works fine, but I want to use cout.
EDIT:
I've changed "" to <> but it is not helping. Also I am using this code only for example... This is not the whole project.
stdafx.h shall be the first include directive in your source file.
Switch files and convert the second include to <>, as other suggested.
#include "stdafx.h"
#include <iostream>
See this post for more information.
First of all:
#include <iostream>
instead of #include "iostream"
Secondly, it is generally considered bad practice to write using namespace std;, even though most courses start with that. It is better to only use what you actually need, in your case:
using std::cout;
#include "iostream"
should be
#include <iostream>
Quoting from this post:difference-between-iostream-and-iostream-quotes-in-include
By courtesy of #Jerry Coffin's answer:
When you use < >, the compiler only looks in the system-designated directory/directories (e.g., whatever you've set in the include environment variable) for the header.
When you use " ", the compiler looks in the local directory first, and if that fails, re-searches just like you'd used < >. Technically, (i.e., according to the standard) that doesn't have to be the "local" directory, but that's how it works in essentially every compiler of which I'm aware).
EDIT:
However, the root cause is that stdafx.h is a precompiled header. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled. However, it is still better to use <> with iostream not to confuse reader of the code.
If you use #include <iostream> with the <> instead of "" then it should work. Right now, the compiler doesn't know where to find the iostream library.
Also, you might want to change cout<<"example"; to cout<<"example"<<endl; for a new line so that it formats correctly.
Came across this issue while trying to build a Dynamic Linked Library. Make sure that instead of the #include stdafx.h you specify the following include on the first line of your .cpp file:
#include "pch.h"
This should also be the case for VS2017 or earlier.
This error also occurred in the Visual Studio 2017 IDE. Moving stdafx.h to the top solved the error.
For more on stdafx.h, see What's the use for "stdafx.h" in Visual Studio?

Qwt missing includes

When typing the following line:
QwtPointSeriesData* myData = new QwtPointSeriesData;
It says to me that identifier is undefined. But I have typed the following includes in which, it was mentioned that it cannot open those opens pource files.
#include <qwt_plot_curve.h>
#include "qwt_series_data.h"
Any ideas how to solve and find out how to get them?
To answer my question :
It seems that I was having an very old version of QWT!