【问题标题】:C++ expected a type specifier for class objectC++ 期望类对象的类型说明符
【发布时间】:2019-06-26 21:32:50
【问题描述】:

我正在尝试使用头文件创建一个类对象,但我在主函数中不断收到此错误。

这是头文件:

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

这是cpp文件:

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

当我尝试创建实例时,现在在 ma​​in.cpp 中:

main.cpp

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

它显示了这个错误:

C2061 语法错误:标识符 'helper'

如何在 main 中定义此类的实例?我在 windows x64 上使用 Visual Studio 2015。

【问题讨论】:

  • 为变量使用不同的名称:helper *h = new helper();
  • 在标题中避免using namespace。尤其是当您实际上完全限定类型时。

标签: c++ class object constructor


【解决方案1】:

为变量使用不同的名称。

helper* obj = new helper;

当你使用变量名与类名相同时,类名会被变量名遮蔽。

【讨论】:

  • 谢谢,这解决了问题。但是,代码中还有其他类使用相同的名称(就像我在这里所做的那样),它们没有问题。我像他们一样添加了这个类,但是出现了这个错误。
  • @HadiGhahremanNezhad,如果没有看到代码,我无法回答它们为什么/如何工作。
  • @HadiGhahremanNezhad 也许其他例子有不同的大小写?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-27
相关资源
最近更新 更多