为什么C++无法识别我的对象实例化?

Why is C++ not recognizing my object instantiation in main?

本文关键字:对象 实例化 我的 识别 C++ 为什么      更新时间:2023-10-16

我在主函数中收到错误 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++) ...

。等等。我希望这能帮助你回到正确的轨道上。