为什么这段代码无法编译?

Why this code doesn't compile?

本文关键字:编译 代码 段代码 为什么      更新时间:2023-10-16

这些代码有什么区别吗?

std::string dirName = argv[1];
MyRecordDatabaseType myDB(Selector<std::string>((std::string)dirName));

std::string dirName = argv[1];
MyRecordDatabaseType myDB(Selector<std::string>(dirName));

我不知道为什么第二个版本不能编译。

编译器告诉我

error: request for member ‘createGroupWriter’ in ‘myDB’, which is of non-class type ‘main(int, char**)::MyRecordDatabaseType(Selector<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >)’

:

MyRecordDatabaseType::writer_type myWriter(myDB.createGroupWriter(groupName));

对不起,我不能给你看Selector或其他类的代码。

也许你不用那个就能帮我?

是的,区别在于这一行

MyRecordDatabaseType myDB(Selector<std::string>(dirName)); 

也可以写成

MyRecordDatabaseType myDB(Selector<std::string>  dirName); 

and是函数myDB的声明,该函数返回MyRecordDatabaseType

参见c++ most vexing parse