简体   繁体   中英

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 !

I believe that the reference in the readfile function definition is causing the problem. If you remove the reference from the argument from the both function declaration and definition, the code compiles perfectly well.

std::vector<int> readfile(std::string filename)

The keyword const is missing in the function arguments. The reason for this error being, C++ doesn't want users to change the values of temporary variables since they can be removed from memory at any time. This is done so as to avoid the issue of temporary variables being passed around and then causing issues. The function definition can be changed to:

std::vector<int> readfile(const std::string &filename)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM