Web lists-archives.com

Re: [Mingw-users] Copy Constructor; Programming Principles and Practice Using C++




The code is incorrect. It should be

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

copy takes iterators, that's why it's giving you type mismatch errors.


On Dec 17, 2016 8:59 PM, "KHMan" <keinhong@xxxxxxxxx> wrote:
On 12/18/2016 10:21 AM, Clayton Weaver wrote:
> I've been slowly going through Bjarne Stroustrup's Programming
> Principles and Practice Using C++ 2nd Edition (covering C++11 and
> C++14). The code I'm having issues with is in 18.3.1 Copy
> Constructor. The book code is
>
> class Vector{
>       int sz;  // size
>       double *elem;   // pointer to the element
>    public:
>         Vector(const Vector& arg);
> };
>
> Vector::Vector(const Vector& arg) : sz {arg.sz <http://arg.sz>},
> elem{new double[arg.sz <http://arg.sz>]}
> {
>        std::copy(arg, arg + sz, elem);
> }
>
> Upon compiling it, I get:
> samp18_1.cpp:30:16: error: no match for 'operator+' (operand types
> are 'const Vector' and 'int')
> samp18_1.cpp:30:18: note:   mismatched types 'const
> std::reverse_iterator<_Iterator>' and 'int'
> samp18_1.cpp:30:18: note:   mismatched types 'const
> std::move_iterator<_Iterator>' and 'int'
> etc.
>
> What do I need to change in order to get it to compile? Every
> Google search I've done states the above code is correct and
> valid, but not really finding the same error to get a solution for.

In order to diagnose sensibly, folks here will need the complete
source code that you tried and the exact command line.

--
Cheers,
Kein-Hong Man (esq.)
Selangor, Malaysia


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
MinGW-users@lists.sourceforge.net

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:mingw-users-request@lists.sourceforge.net?subject=unsubscribe

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most 
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
MinGW-users@xxxxxxxxxxxxxxxxxxxxx

This list observes the Etiquette found at 
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:mingw-users-request@xxxxxxxxxxxxxxxxxxxxx?subject=unsubscribe