cout 和endl需要同一个头文件
#include <iostream>
还需要
using namespace std;
最后贴上完整的使用
#include "pch.h"
#include <iostream>
#include <opencv2/opencv.hpp>
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat M(2, 2, CV_8UC3, Scalar(0, 0, 255));
cout << "M= " << endl << " " << M << endl << endl;
}