Structure As构造函数参数(在main中声明)
Structure As constructor parameter (Declared in main)
我在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;
}
我有以下编译错误:
- Verification.h|33|错误:"args"未命名类型|(此错误适用于类_a中的私有成员)
- main.cpp|153|错误:调用"Verification::Verification(const char[7],std::vector&,double,args&)"时没有匹配的函数|
- Verification.h|24|错误:尚未声明"args"|(此错误是构造函数)
如何在类验证中使用main中声明的结构作为构造函数参数?
谢谢。
必须以验证类转换单元可见的方式定义结构。我建议您将结构移到它自己的头文件中,并#将其包含在主文件和Verification.h中。
第一个错误是编译class Verification
时,编译器必须首先看到struct args
。它不知道你稍后会定义struct args
。
简单的解决方案是将struct args
的定义移动到Verification.h
中。
修复它,您仍然会有其他错误(最明显的是,A
没有定义),但当您遇到这些错误时,我们可以处理它们。
您的第二个错误是因为字符串文字是const char[]
,而不是std::string
——在将其传递给需要字符串的函数之前,您需要创建一个string
。
此外,需要在此调用之前定义gt
和A
。
相关文章:
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- C++变量名(可以将 main 声明为变量,但对于其他函数名称则不然)
- 为什么在我的 main 函数中声明整数后我的整数数组中会出现错误?
- 函数未在作用域中声明 / 如何结合使用 header.h、header.cpp 和 main.cpp?
- "Main"已在当前范围内声明
- 在命名空间中声明变量,在 main 中定义它,使其对所有其他文件可见
- 如何从不同的类中获取函数来调用在 Main 中声明的对象?
- 我们可以在main函数中声明嵌套类对象吗
- 在函数 'int main(int, char**) 中,没有声明 'MIN'
- 内存浪费?如果main()应该只返回0或1,那么为什么main是用int而不是短int甚至char声明的
- 为什么在 main() 中声明的指针没有更改
- 如何在不指定 C++ 中的所有参数的情况下声明程序的 main() 入口点
- 未声明的标识符和类型"int"从 main 调用类时出现意外错误
- int main() 是否需要对C++进行声明
- 如何在另一个函数中初始化在 main 中声明的数组
- 我需要创建一个函数在用户下打印"=",但由于变量是使用 main() 声明的,因此函数看不到参数
- 为什么不使用函数try块声明main()?
- C 外部阵列在标头中声明,在main.cpp中不可用
- 析构函数删除在 main 中声明的动态数组
- Main 不能调用函数:函数未在此范围内声明