gpt4 book ai didi

c++ - 使用 ifstream 时出现语法错误

转载 作者:行者123 更新时间:2023-11-27 22:58:16 25 4
gpt4 key购买 nike

我已经很久没有接触过 C++ 了。这段代码有什么问题?

#include <iostream>
#include <fstream>

using namespace std;
main()
{
ifstream& ifs("foo.txt");
}

给予:

$ g++ foo.cc 
foo.cc: In function ‘int main()’:
foo.cc:7:25: error: invalid initialization of non-const reference of type ‘std::ifstream& {aka std::basic_ifstream<char>&}’ from an rvalue of type ‘const char*’
ifstream& ifs("foo.txt");

最佳答案

你在不该做的时候使用了 &

#include <iostream>
#include <fstream>

using namespace std;
int main()
{
ifstream ifs("foo.txt");
}

关于c++ - 使用 ifstream 时出现语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30404518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com