-2

何があっても使える静的関数だけを組み込んだ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);
};

これが私の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;
}

そして、これは私が自分の関数でそれを使用したい方法です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;
}

そして、ここに私が受け取っているエラーがあります。

誰かが私が間違っていることを指摘できますか? 私は何が欠けていますか?

4

3 に答える 3

5
Utils::drawPoint(img, 0, 255, 0, 20, 20);
     ^^ (not period)

静的関数を呼び出す方法です。期間はメンバーアクセス用です(つまり、インスタンスがある場合)。

完全を期すためにこれを説明するには:

Utils utils; << create an instance
utils.drawPoint(img, 0, 255, 0, 20, 20);
     ^ OK here
于 2012-10-13T10:19:56.707 に答える
1

クラスの減速後にセミコロンが足りないと思います。試す、

class Utils
{
public:
    static void drawPoint(Mat &img, int R, int G, int B, int x, int y);
}; // <- Notice the added semicolon
于 2012-10-13T10:17:47.253 に答える
0

これはあなたの質問に対する直接的な答えではありませんが、名前空間スコープの関数を使用すると、ニーズにより適している場合があります。つまり、次のとおりです。

namespace Utils
{
    void drawPoint(Mat &img, int R, int G, int B, int x, int y);
}

::セマンティクスは同じままですが、次のようになりました。

  • Utils「静的クラス」にとって意味のないオブジェクトをインスタンス化することはできません
  • 選択した(および制限された)スコープでプレフィックスusing Utilsを回避するために使用できますUtils::

静的クラス メンバー関数と名前空間スコープ関数の長所と短所の詳細については、次を参照してください:名前空間 + 関数とクラスの静的メソッド

于 2012-10-13T10:42:43.197 に答える