-2

I want to creat a C++ class which is incorporated only with static functions that I can use no matter what. I have create a .h file with the declarations and a .cpp file with the definitions. However, when I am using it inside my code I am receiving some weird error messages which I do not know how to solve.

Here is the content of my Utils.h file:

#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);
};

Here is the content of my Utils.cpp file:

#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;
}

And this is how I want to use it in my main function :

#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;
}

And here are the errors that I am receiving.

Can someone point me out what I am doing wrong? What am I missing?

Simon
  • 4,999
  • 21
  • 69
  • 97

3 Answers3

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

is how you would call a static function. the period is for member access (i.e. when you have an instance).

to illustrate this for completeness:

Utils utils; << create an instance
utils.drawPoint(img, 0, 255, 0, 20, 20);
     ^ OK here
justin
  • 104,054
  • 14
  • 179
  • 226
1

Think you're missing a semicolon after the class deceleration. Try,

class Utils
{
public:
    static void drawPoint(Mat &img, int R, int G, int B, int x, int y);
}; // <- Notice the added semicolon
PherricOxide
  • 15,493
  • 3
  • 28
  • 41
0

This is not a direct answer to your question but using a namespace scoped functions might suit your needs better. What I mean is:

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

The :: semantics are left the same but now:

  • You can't instantiate a Utils object, which is meaningless for "static class"
  • You could use using Utils to avoid the Utils:: prefix in chosen (and limited) scopes

For a deeper discussion of the pros and cons of static class member functions vs namespace scoped functions see: Namespace + functions versus static methods on a class

Community
  • 1
  • 1
Xyand
  • 4,470
  • 4
  • 36
  • 63