G++ 编译器:无法写入文件
G++ compiler: cant write to file
所以有一天我做了一个小程序来测试我在 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 上它编译得很好并且 运行 没有任何问题所以如果有人能帮助我并指出正确的方向我会感激不尽
ofstream writer();
这是错误的。
你不小心声明了一个函数。
删除 ()
.
(您在 Visual Studio 下一定是 运行 不同的代码。)
所以有一天我做了一个小程序来测试我在 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 上它编译得很好并且 运行 没有任何问题所以如果有人能帮助我并指出正确的方向我会感激不尽
ofstream writer();
这是错误的。
你不小心声明了一个函数。
删除 ()
.
(您在 Visual Studio 下一定是 运行 不同的代码。)