C++:如何使用静态函数定义类

Sim*_*mon -2 c++ opencv static-methods

我想创建一个C++类,它只与静态函数合并,无论如何都可以使用.我创建了一个.h包含声明的.cpp文件和一个包含定义的文件.但是,当我在我的代码中使用它时,我收到一些奇怪的错误消息,我不知道如何解决.

这是我的Utils.h文件的内容:

#include <iostream>
#include <fstream>
#include <sstream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

#include <vector>
#include <opencv/cv.h>
#include <opencv/cxcore.h>

class Utils
{
public:
    static void drawPoint(Mat &img, int R, int G, int B, int x, int y);
};
Run Code Online (Sandbox Code Playgroud)

这是我的Utils.cpp文件的内容:

#include "Utils.h"

void Utils::drawPoint(Mat &img, int R, int G, int B, int x, int y)
{
img.at<Vec3b>(x, y)[0] = R;
img.at<Vec3b>(x, y)[1] = G;
img.at<Vec3b>(x, y)[2] = B;
}
Run Code Online (Sandbox Code Playgroud)

这就是我想在我的main函数中使用它的方式:

#include <iostream>
#include <fstream>
#include <sstream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

#include <vector>
#include <opencv/cv.h>
#include <opencv/cxcore.h>

#include "CThinPlateSpline.h"
#include "Utils.h"

int main()
{
Mat img = imread("D:\\image.png");
if (img.empty()) 
{
    cout << "Cannot load image!" << endl;
    system("PAUSE");
    return -1;
}
Utils.drawPoint(img, 0, 255, 0, 20, 20);
imshow("Original Image", img);
waitKey(0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

以下是我收到的错误.

有人能指出我做错了吗?我错过了什么?

jus*_*tin 5

Utils::drawPoint(img, 0, 255, 0, 20, 20);
     ^^ (not period)
Run Code Online (Sandbox Code Playgroud)

是你如何调用静态函数.期间是成员访问(即,当您有一个实例).

为了完整性来说明这一点:

Utils utils; << create an instance
utils.drawPoint(img, 0, 255, 0, 20, 20);
     ^ OK here
Run Code Online (Sandbox Code Playgroud)