C++ Syntax Header File Errors

2020-02-15 15:37发布

问题:

I am using opencv and gdal with visual studio 2019 community version, I writte some sample test to know if opencv and gdal works fine on my computer, but then some error with header file raises, all these error didn't affect the compiling of the program and the running or output of the program, all is fine except those header file syntax error, which makes me really confused. my code is down below:

/*opencv_test*/
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    Mat image = Mat::zeros(300, 600, CV_8UC3);
    circle(image, Point(250, 150), 100, Scalar(0, 255, 128), -100);
    circle(image, Point(350, 150), 100, Scalar(255, 255, 255), -100);
    imshow("Display Window", image);
    waitKey(0);
    return 0;
}

the error list is like these:

Warning C26451  Arithmetic overflow: Using operator '*' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '*' to avoid overflow (io.2). opencv_test C:\MSVC_Library\OpenCV\opencv\build\include\opencv2\core    C:\MSVC_Library\OpenCV\opencv\build\include\opencv2\core\mat.inl.hpp    550 

Warning C6294   Ill-defined for-loop:  initial condition does not satisfy test.  Loop body not executed.    opencv_test C:\MSVC_Library\OpenCV\opencv\build\include\opencv2\core    C:\MSVC_Library\OpenCV\opencv\build\include\opencv2\core\matx.hpp   553 

Warning C26812  The enum type 'cv::AccessFlag' is unscoped. Prefer 'enum class' over 'enum' (Enum.3).   opencv_test C:\MSVC_Library\OpenCV\opencv\build\include\opencv2\core    C:\MSVC_Library\OpenCV\opencv\build\include\opencv2\core\mat.hpp    66      

I hope someone could answer my question,sincerely.

回答1:

As others have pointed out, these are warnings, not errors - and your code will compile and run. You can get information on what these warnings mean from the in-built, Visual Studio "Help" (if you have that installed) by typing, e.g. "C26451" in the "search" field; alternatively, you can Google the same.

For the C26812 warning, the use of unscoped enum types is discussed on Stack Overflow, here; but attempting to 'fix' this by modifying the OpenCV header(s) will very likely cause you far more problems! I would suggest that all three warnings can here be safely ignored, as they are as much about suggesting good coding "style" rather than pointing out possible errors (working on the assumption that the folks who wrote the OpenCV package do know what they're doing).

However, as you most likely don't want to modify the code in the OpenCV headers, you can, instead, temporarily disable the warnings before including them, then restore them after all the relevant #include statements.

In your case, for the warnings you have cited, this should work:

#ifdef _MSC_VER // These #pragma lines are MSVC-specific!
#pragma warning(disable:26451)  //
#pragma warning(disable:6294)   // Disable specified warning numbers
#pragma warning(disable:26812)  //
#endif // _MSC_VER

/*opencv_test*/
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>

#ifdef _MSC_VER
#pragma warning(default:26451)  //
#pragma warning(default:6294)   // 'Default' will reset to your project's settings
#pragma warning(default:26812)  //
#endif // _MSC_VER

#include <iostream>
//...


回答2:

Largely those warnings tell you exactly what to do, and where to do it.

The reason why the program runs fine is because they aren't errors - a warning and an error are very different things, with the former generally allowing compilation and an error meaning the program will not compile. You should try to fix warnings as much as possible though as it can cause unexpected results from your program.

For further help on how to fix the warnings if you cannot implement the suggestions then you would have to provide the code which it highlights.