为什么C++无法识别我的对象实例化?
Why is C++ not recognizing my object instantiation in main?
我在主函数中收到错误 C2228。更具体地说,它是说空的左需要一个类/结构/联合。我很困惑,因为它似乎在某些情况下无法识别我的解析对象,并且不知道为什么会这样。据我所知,我已经实例化了一个对象,所以我应该能够使用 dot 运算符访问所有公共方法。
parse_arguments.h
class parse_arguments:
public:
std::string get_model_file();
private:
std::string m_model_file;
parse_arguments.cpp
argument_parser::argument_parser()
: m_output_file()
, m_model_file()
, m_norm_mode(false)
, m_learn_mode(false)
, m_input_files()
, m_nbins(16)
{}
std::string argument_parser::get_model_file()
{
return m_model_file;
}
std::string argument_parser::get_output_file()
{
return m_output_file;
}
主.cpp
int main(int argc, char** argv)
{
normalizer_tool::argument_parser parse;
if (!parse.parse_arguments(argc, argv)) //no error here...
{
return 1;
}
/// Load the data model
if (!parse.get_model_file.empty()) //error here c222
{
//do stuff here
}
for (auto it = parse.get_input_files.begin(); success && it !=
parse.get_input_files.end(); it++) //error here c222
{ //do more stuff here }
代码有很多我不知道从哪里开始的错误。
首先,类定义的冒号 ":" 太多,它需要大括号 "{" 和 "}"。校正:
class parse_arguments
{
...
};
其次,方法调用中缺少括号"(("。校正:
if (!parse.get_model_file().empty())
和:
for (auto it = parse.get_input_files().begin(); success && it !=
parse.get_input_files().end(); it++) ...
第三,在迭代过程中多次重新创建input_files列表,这可能有效,也可能无效。仅当 input_files(( 方法是 const 方法时,这才是安全的。为安全起见,请改用:
const auto input_files = parse.get_input_files();
for (auto it = input_files.begin(); success && it !=
input_files.end(); it++) ...
。等等。我希望这能帮助你回到正确的轨道上。
相关文章:
- 对象实例化调用构造函数的次数太多
- 为什么C++无法识别我的对象实例化?
- 使用大括号和 : 符号的对象实例化
- 对象实例化与类型C++
- cpp 静态对象实例化
- 用户创建的类对象实例化的未申请标识符
- 对象实例化后不输出
- 限制对象实例化堆
- 未知的 C++ 对象实例化语法
- 由于命名空间而导致的对象实例化错误
- C 在对象实例化时如何分开声明和构造函数
- JNI Java对象实例化期间的Segfault
- C++ 类文件中的对象实例化
- C++ 中的对象实例化
- 无法理解浮点数和对象实例化之间比较的原因
- C++对象实例化与赋值
- C++:用友元类限制对象实例化
- 将类中的对象实例化数限制为给定数量
- 使用 Sizeof、malloc 和 cast 的 C++ 对象实例化
- 通用成员函数定义,可从 'const' 和 'non-const' 对象实例化