【发布时间】:2020-06-10 19:31:05
【问题描述】:
所以有一天我做了一个小程序来测试我在 Visual Studio 2019 上制作的一个更大的程序,它看起来像这样:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
string path;
cout << "Enter path and filename: ";
cin >> path;
ofstream writer();
writer.open(path);
writer << "hi\n";
writer.close();
return 0;
}
但最近我切换到 Ubuntu 并尝试了我的程序,但 g++ 向我抛出了这个:
error: request for member ‘open’ in ‘writer’, which is of non-class type ‘std::ofstream() {aka std::basic_ofstream<char>()}’ writer.open(path);
error: invalid operands of types ‘std::ofstream() {aka std::basic_ofstream<char>()}’ and ‘const char [3]’ to binary ‘operator<<’ writer << "hi";
error: request for member ‘close’ in ‘writer’, which is of non-class type ‘std::ofstream() {aka std::basic_ofstream<char>()}’ writer.close();
我真的不明白问题出在哪里,因为在 Visual Studio 上它编译得很好并且运行没有任何问题,所以如果有人可以帮助我并指出正确的方向,我将不胜感激。
【问题讨论】: