简体   繁体   中英

C++ source code compilation error in MinGW with std::copy function

I'm trying to compile the following C++ code and getting some errors. Am I missing some libraries? I'm using MinGW on Windows 7 64 bit.

//ch 18

#include <algorithm>

class vector{
    int sz;
    double* elem;
public:
    vector(const vector&);
};

vector:: vector(const vector& arg)
    :sz{arg.sz}, elem{new double[arg.sz]}
    {
        std::copy(arg,arg.sz,elem);
    }

Here is the error messages.

$ g++ ch18copy.cpp -std=c++11 -o ch18copy
ch18copy.cpp: In copy constructor 'vector::vector(const vector&)':
ch18copy.cpp:15:28: error: no matching function for call to 'copy(const vector&,
 const int&, double*&)'
   std::copy(arg,arg.sz,elem);
                            ^
ch18copy.cpp:15:28: note: candidate is:
In file included from c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\algorithm:61:0,

                 from ch18copy.cpp:3:
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algobase.h:450:5: note: temp
late<class _II, class _OI> _OI std::copy(_II, _II, _OI)
     copy(_II __first, _II __last, _OI __result)
     ^
c:\mingw\lib\gcc\mingw32\4.8.1\include\c++\bits\stl_algobase.h:450:5: note:   te
mplate argument deduction/substitution failed:
ch18copy.cpp:15:28: note:   deduced conflicting types for parameter '_II' ('vect
or' and 'int')
   std::copy(arg,arg.sz,elem);
                            ^

It looks like you aren't using the right parameters for std::copy.

From http://www.cplusplus.com/reference/algorithm/copy/ :

template <class InputIterator, class OutputIterator>  
  OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);

From their example:

int myints[]={10,20,30,40,50,60,70};
std::vector<int> myvector (7);

std::copy ( myints, myints+7, myvector.begin() );

So the first two parameters are the start and end of the range you want to copy, and the third parameter is where you want to copy to.

In your case, this would look something like this (disclaimer: didn't test this):

std::copy(arg.elem, arg.elem + arg.sz, elem);

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