// stream from file.
ifstream file;

int main (int argc, char * argv[]) {

// get argument passed from command line
// This is file name
if (argc != 2 ) {
    cout << "use:  ./executable <filename>";

}else {
    //cout << "You are using filename: " << argv[1];

    // start the file stream
    file (argv[1]);
}

是否有任何理由file(argv[1])会被给了一个错误?我能有一个ifstream的一个全局变量?

有帮助吗?

解决方案

您正在试图调用ifstream()运营商(不存在),当你要使用的 file.open(argv[1])

除此之外,没有什么违法关于有一个全球性的ifstream

其他提示

您可以有ifstream的一个全局变量(这是很好的风格是不同的问题)。

这个问题似乎是,你试图使用构造函数:file(argv[1])

全局变量将已被构造(使用默认构造)在这一点上,你将代替需要使用open方法。

file.open( argv[1] );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top