Hadi GhahremanNezhad
Hadi GhahremanNezhad

Reputation: 2455

C++ expected a type specifier for class object

I am trying to create a class object with the header file, but I keep getting this error in the main function.

This is the header file:

helper.h

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace std;
using namespace cv;

class helper {
public:
    helper();
    ~helper();

    void setLabel(cv::Mat& im, const std::string label, const cv::Point & or , const cv::Scalar col);
};

and this is the cpp file:

helper.cpp

#include "helper.h"

helper::helper() {
}

void helper::setLabel(cv::Mat& im, const std::string label, const cv::Point & or , const cv::Scalar col)
{
    int fontface = cv::FONT_HERSHEY_SIMPLEX;
    double fontScale = 0.4;
    int thickness = 1;
    int baseline = 0;

    cv::Size text = cv::getTextSize(label, fontface, fontScale, thickness, &baseline);
    cv::putText(im, label, or , fontface, fontScale, col, thickness, CV_AA);
}

Now in main.cpp when I try to create an instance:

main.cpp

#include "helper.h"
int main(){
    helper* helper = new helper;
}

It shows this error:

C2061 syntax error: identifier 'helper'

How can I define an instance of this class in main? I am using Visual Studio 2015 on windows x64.

Upvotes: 0

Views: 944

Answers (1)

R Sahu
R Sahu

Reputation: 206667

Use a different name for the variable.

helper* obj = new helper;

When you use the variable name to be the same as the class name, the class name is shadowed by the variable name.

Upvotes: 2

Related Questions