Structure As构造函数参数(在main中声明)

Structure As constructor parameter (Declared in main)

本文关键字:main 声明 As 构造函数 参数 Structure      更新时间:2023-10-16

我在main中声明了以下结构(NEVERMIND the MEMBERS!)

struct args
{
    std::vector<string> names;
    std::vector<std::shared_ptr<RegularExpression>>vreg;
    std::vector<string> stopFile;
    std::vector<string> groundTruth;
    int debug;
};

我有一个classe Verification,它将args作为构造函数参数

#ifndef VERIFICATION_H
#define VERIFICATION_H
class Verification
{
    public:
        Verification(std::string, std::vector<double>,double,args);
    private:
         args _A; 
}
#endif // VERIFICATION_H

现在主要是:

struct args
    {
        std::vector<string> names;
        std::vector<std::shared_ptr<RegularExpression>>vreg;
        std::vector<string> stopFile;
        std::vector<string> groundTruth;
        int debug;
    };
    int main()
    {
      Verification v("res.cr",gt, 0.75,A);
      return 0;
    }

我有以下编译错误:

  1. Verification.h|33|错误:"args"未命名类型|(此错误适用于类_a中的私有成员)
  2. main.cpp|153|错误:调用"Verification::Verification(const char[7],std::vector&,double,args&)"时没有匹配的函数|
  3. Verification.h|24|错误:尚未声明"args"|(此错误是构造函数)

如何在类验证中使用main中声明的结构作为构造函数参数?

谢谢。

必须以验证类转换单元可见的方式定义结构。我建议您将结构移到它自己的头文件中,并#将其包含在主文件和Verification.h中。

第一个错误是编译class Verification时,编译器必须首先看到struct args。它不知道你稍后会定义struct args

简单的解决方案是将struct args的定义移动到Verification.h中。

修复它,您仍然会有其他错误(最明显的是,A没有定义),但当您遇到这些错误时,我们可以处理它们。

您的第二个错误是因为字符串文字是const char[],而不是std::string——在将其传递给需要字符串的函数之前,您需要创建一个string

此外,需要在此调用之前定义gtA