结构作为构造函数参数(在main中声明)

时间:2013-11-20 11:55:22

标签: c++ constructor struct

我在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&amp;,double,args&amp;)'|
  3. Verification.h | 24 |错误:'args'尚未声明| (此错误是构造函数)
  4. 如何在类验证中使用main中声明的结构作为构造函数参数?

    谢谢。

3 个答案:

答案 0 :(得分:2)

必须以Verification类翻译单元可见的方式定义结构。我建议你将结构移动到它自己的头文件,并在你的主文件和Verification.h中#include它。

答案 1 :(得分:2)

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

简单的解决方法是将struct args的定义移至Verification.h

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

答案 2 :(得分:1)

您的第二个错误是由于字符串文字是const char[]而不是std::string这一事实 - 您需要在将string传递给期望字符串的函数之前创建gt

此外,还需要在此次调用之前定义A和{{1}}。