c++ - Compilation error with readfile function in C++
问题描述
I am trying to make a C++ program which reads in a data from file with ifstream. This is the code of my program:
#include<iostream>
#include<fstream>
#include<string>
#include "sort.h"
std::vector<int> readfile(std::string &filename)
{
// Open the file
std::ifstream file(filename, std::ios::binary);
// Get the size of the file - seek to end
file.seekg(0, file.end);
std::streampos size = file.tellg();
// Seek back to start
file.seekg(0,file.beg);
// Number of elements is size / sizeof(int)
int elements = size / sizeof(int);
// Create an array of data to raead into
int *temp = new int[elements];
// Read in data
file.read((char*)temp, size);
// Close the file
file.close();
//Copy data into the vector
std::vector<int> data(temp, temp + elements);
// Delete the data
delete[] temp;
// Return the vector
return data;
}
int main(int argc, char **argv)
{
// Read in a vector
std::vector<int> data = readfile(std::string("numbers.dat"));
// Print the vector size
std::cout<<"Numbers read = "<<data.size() <<std::endl;
// Sort the vector
sort(data);
// Output first 100 numbers
for(int i = 0; i < 100; i++)
{
std::cout<<data[i]<<std::endl;
}
return 0;
}
The header file sort.cpp is :
#include "sort.h"
#include<iostream>
void sort(std::vector<int> &data)
{
// Iterate through each value
for( int i = 0; i< data.size(); ++i)
{
// Loop through values above index i
for(int j = 0; j < data.size() - (i + 1); ++j)
{
if(data[j] > data[j+1])
{
// Swap values
int temp = data[j+1];
data[j+1] = data[j];
data[j] = temp;
}
}
if(i % 1000 == 0)
{
std::cout<<((float)i / (float)data.size()) * 100.0f << "% sorted" << std::endl;
}
}
}
The error which I get is :
ifstream.cpp: In function ‘int main(int, char**)’:
ifstream.cpp:34:40: error: cannot bind non-const lvalue reference of type ‘std::__cxx11::string& {aka std::__cxx11::basic_string<char>&}’ to an rvalue of type ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’
std::vector<int> data = readfile(std::string("numbers.dat"));
^~~~~~~~~~~~~~~~~~~~~
ifstream.cpp:6:18: note: initializing argument 1 of ‘std::vector<int> readfile(std::__cxx11::string&)’
std::vector<int> readfile(std::string &filename)
I was wondering why this does not work. Is it the GCC that does not like anything or it's me, the non-experienced man with the stick for the C++. Thanks in advance !
解决方案
我相信 readfile 函数定义中的引用导致了问题。如果从函数声明和定义中的参数中删除引用,则代码编译得非常好。
std::vector<int> readfile(std::string filename)
推荐阅读
- javascript - ZXing.NET 从 HTML5 视频中解码 PDF417 条码
- windows - 在 Windows 8 上运行 rabbitMQ docker 映像时出现问题,其中包含用于保留容器数据的卷
- javascript - 如何向 Vue.js AWS-amplify 应用程序添加身份验证持久性
- html - 另一个元素下方的元素上的 CSS 阴影
- salesforce - SyntaxError: Cannot use import statement outside a module in JEST LWC
- azure - AzureAD 企业应用程序 SAML2.0 元数据文件不包含附加声明
- reactjs - react testing library render component conditionally
- python - 从本地 .whl 文件安装 Elastic Beanstalk Python requirements.txt
- for-loop - 在 Pine 脚本中编写 for 循环
- typescript - 将字符串传递给抽象类的构造函数