Problem compiling json with visual studio - c++

I have another program that uses json but I do not manage to make it compile. I think that the problem is related to the library I am using to parse a Json file, so, I coded a small code to try to understand better how it works.
When I am compiling the program using visual Studio 2019 in Windows the following errors appear (I have translated the errors to english so some inaccuracy may have been introduced ):
Error C4996 'Json::Reader::parse': Use CharReader and CharReaderBuilder instead. ConsoleApplication5 C:\Users\S510U\source\repos\ConsoleApplication5\ConsoleApplication5\ConsoleApplication5.cpp Line:21
Warning C26812 The numeration type "Json::CommentPlacement" has no scope. Prefer "enum class" to "enum" (Enum.3). ConsoleApplication5 C:\Users\UserName\source\repos\ConsoleApplication5\packages\JsonCpp.Windows.1.9.2\build\native\include\json\value.h 589
Warning C26812 The numeration type "Json::ValueType" has no scope. Prefer "enum class" to "enum" (Enum.3). ConsoleApplication5 C:\Users\UserName\source\repos\ConsoleApplication5\packages\JsonCpp.Windows.1.9.2\build\native\include\json\value.h 618
Warning C26812 The numeration type "Json::PathArgument::Kind" has no scope. \UserName\source\repos\ConsoleApplication5\packages\JsonCpp.Windows.1.9.2\build\native\include\json\value.h 732
Waring C4275 It has been used an interface not of the DLL class 'std::exception' as a base for the interface DLL class 'Json::Exception' ConsoleApplication5 C:\Users\UserName\source\repos\ConsoleApplication5\packages\JsonCpp.Windows.1.9.2\build\native\include\json\value.h 57
Error C4996 'Json::Reader': Use CharReader and CharReaderBuilder instead. ConsoleApplication5 C:\Users\UserName\source\repos\ConsoleApplication5\ConsoleApplication5\ConsoleApplication5.cpp 19
Error C4996 'Json::Reader::Reader': Use CharReader and CharReaderBuilder instead ConsoleApplication5 C:\Users\UserName\source\repos\ConsoleApplication5\ConsoleApplication5\ConsoleApplication5.cpp 19
However, when I try to run it in Linux using g++ it works.
#include <iostream>
#include <vector>
#include <fstream>
#include <cmath>
#include <math.h>
#include "string.h"
#include <cstdlib>
#include "json/json.h"
using namespace std;
int main(void){
string namedir = "fichero.json";
ifstream ifs(namedir);
Json::Value root;
Json::Reader reader;
Json::Value obj;
reader.parse(ifs, obj);
return 0;
}

It seems like you are using an old interface of this library: https://github.com/open-source-parsers/jsoncpp ?
There is this error reported e.g. : https://github.com/open-source-parsers/jsoncpp/issues/815
This is a warning that says you should not use the old interface but instead the newer one. The warning is handled as an error from MSVC (visual studio) because your warning level is set to high. GCC does not throw an error because it lets pass the warning as it is (This is an assumption).
To resolve the warning you need to reduce the warning level in the project properties, but in my eyes this is a bad idea.
Better use the new interface of the library and always have a look at the warnings which the compiler is giving you. It will reduce a lot of headaches you coudl get while ignoring them ;) (like that problem)

Related

Why might the “fatal error C1001” error occur intermittently when using openmp?

My code works well without #openmp
but I got this error when I added #openmp compiler
1>c:\users\hdd amd ali\documents\v studio 10 projects\visual studio 2010\projects\escaledesvols2 - copy\escaledesvols2\djikstra.cpp(116): fatal error C1001: An internal error occurred in the compiler.
1> (compiler file 'f:\dd\vctools\compiler\utc\src\p2\wvm\mdmiscw.c', ligne 1098)
note:
i use many different libraries (like #boost)
#include <string>
#include <iostream>
#include <stdio.h>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <msclr\marshal_cppstd.h> // for unmanaged piece of code
#include <vcclr.h>
I had this issue recently; I was compiling with visual studio 2015. I tried it with visual studio 2017 and I still got the internal compiler error. Then I tried it with visual studio 2013 and it told me that I can't have a "return" statement inside an openMP section. when I removed the return from VS 2013 and VS 2105 the compiler was able to successfully compile. So, it make sense to try it with VS 2013 and it will give you a better error description. You could also be having return statements inside openMP sections and that could be the reason for c1001 error.
In my case it was a return function from the OpenMP loop. Removing a "return" line solved the problem.
You should simply report it.
In terms of workarounds, it is likely related to memory/resource consumption. Usual tricks to lower consumption are
disable debug information
split up compilation units to smaller size (this might be key here: "I'm using many libraries" should not be an issue unless you're including all the headers in a single translation unit
try to reduce template instantiations
Alternatively
reduce system load (close other programs, such as your Stackoverflow browser that might tatke valuable resources :))

error C2976: 'std::tr1::array' : too few template arguments in MS C++ express 2010

I'm working on a Windows forms project in C++ Express 2010. Made some changes to the the Form.h and now get an error when compiling the program. Note the compiler suggests the error is in the main program - open gl test 2 - that #includes the form. All compiled fine for days as I developed the code, then changed something last night and now I get the error.
The main program in open gl test 2 is identical to that of any other forms based project I have created - I've compared to check [except for the obvious changes of namespace etc cause its a different project]
// open gl test 2.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace opengltest2;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
The error is:
1>open gl test 2.cpp(9): error C2976: 'std::tr1::array' : too few template arguments
1> D:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\array(18) : see declaration of 'std::tr1::array'
1>open gl test 2.cpp(9): error C3699: '^' : cannot use this indirection on type 'std::tr1::array'
1> compiler replacing '^' with '*' to continue parsing
line that compiler is complaining about is
int main(array<System::String ^> ^args)
So the fault cant be in where the compiler claims it is - and I can not find a fault in the code in the included files - they compile correctly. I assume the error MUST be in form.h cause that's what I modified that causes this error, but I can not see it. And I'm pretty sure I did not change some project settings.
I ain't including the code for form1.h cause it's toooooo long. Guess what I'm looking for is peoples experience of searching for the error. What strategies can I use to resolve this? If I had hair to pull out I would be surrounded by it. Please help me save my non-existant hair.
It looks like the compiler is getting confused between the managed array type and the array type in the std library. This is probably because somewhere you're doing using namespace std - you should ideally explicitly reference the correct namespace (std::array or cli::array), or remove the include of <array> if it's not needed.
Note that the constant size std::array isn't suitable for use with command-line arguments - you'd need to use a variable size container like cli::array anyway.

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?

Is it possible to force visual studios to throw an error if an identifiers is used when its library isn't directly included in the source?

When compiling the following source:
int main()
{
exp(1.0);
return 0;
}
the copiler gives the following error: error C3861: 'exp': identifier not found because I didn't have the line: #include <iostream> above main()
However, visual studios won't display the error if a library is indirectly included. For example, the following code compiles without a problem even though the dependency of exp is in <cmath>.
#include <istream>
int main()
{
exp(1.0);
return 0;
}
This is because <iostream> includes <istream> which include <ostream> which includes <ios> which includes <xlocnum> which includes <cmath>.
Is there a way to make visual studios throw an error if I don't explicitly include a library yet try to use one of its identifiers?
You may want to have a look at include-what-you-use. It is a clang-based tool trying to detect missing and superfluous include directives.

Difference between <cstring> and <string>

Earlier today (actually yesterday due to my time-zone) I was attempting a programming interview using Visual Studio 2012 for C++ on Interview Street (which uses g++).
To be brief, I came across several compilation errors1 when I was using
#include <cstring>
which was provided by the skeleton code in one of the question, and after turning to
#include <string>
all compilation errors magically disappeared.
However, upon submission to Interview Street, I had to add c back; otherwise I got compilation errors.
It was the first time I was bitten by non-standardization....
My question is: what inside <string> and <cstring> took me (precious) more than half an hour?
1 For anyone who is curious:
One error by Visual Studio 2012 if using <cstring> is:
error C2338: The C++ Standard doesn't provide a hash for this type.
in
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef
possibly for string as key in unordered_map
One error by g++ if using <string> is:
'strlen' was not declared in this scope
The cstring header provides functions for dealing with C-style strings — null-terminated arrays of characters. This includes functions like strlen and strcpy. It's the C++ version of the classic string.h header from C.
The string header provides the std::string class and related functions and operators.
The headers have similar names, but they're not really related beyond that. They cover separate tasks.
<cstring> has the C string code from the C header string.h. C++ has a convention where C headers have the same base name, except for a leading c and no trailing .h. All the contents are available under the std:: namespace.
<string> has the standard library std::string and related functions
In C++, you wouldn't use #include <somefile.h>, but instead #include <somefile>. Now C++ has its string classes in <string>, but the c-string functions are also available, which would be in <string.h>. C++ uses for 'traditional' c- include files. Therefore, <cstring> and <string>
http://www.cplusplus.com/reference/clibrary/cstring/