Quantcast

Copy Constructor; Programming Principles and Practice Using C++

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Copy Constructor; Programming Principles and Practice Using C++

Clayton Weaver
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}, elem{new double[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.

Sincerely,
Clayton Weaver

------------------------------------------------------------------------------
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
[hidden email]

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:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Copy Constructor; Programming Principles and Practice Using C++

KHMan
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
[hidden email]

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:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Copy Constructor; Programming Principles and Practice Using C++

DAVENPORT, MARC
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" <[hidden email]> 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
[hidden email]

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:[hidden email]?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
[hidden email]

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:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Copy Constructor; Programming Principles and Practice Using C++

Clayton Weaver
Thanks Marc. I was losing my mind as the only errata I could find for chapter 18 concerning copy was ones that said copy(arg.elem, arg.elem.sz, elem); and that gave a whole new list of errors which made me take to Google. Google found tons of questions for it, but most were doing the above so it didn't help me much.

I did have a couple of warnings that popped up, one I know how to fix the other I was seeking advice on because I found one fix, but read it was not recommended according to the C++11 standard.

The first warning, which I know how to fix, concerns std::vector and for statements. The unsigned int and int comparison: for (int i = 0;i < vec.size();...) being changed to for(unsigned int i = 0; i < vec.size();...).

The second warning I'm getting is a narrowing conversion:
Vector::Vector(initializer_list<double> lst) : sz{lst.size()}, elem{new double[sz]}
samp18_1.cpp:25:59: warning: narrowing conversion of 'lst.std::initializer_list<_E>::size<double>()' from 'std::initializer_list<double>::size_type {aka unsigned int}' to 'int' inside { } [-Wnarrowing]

Full code (per book):
Vector::Vector(initializer_list<double> lst) : sz{lst.size()}, elem{new double[sz]}
{
     std::copy(lst.begin(), lst.end(), elem);
}

I read that a fix was to change sz{lst.size()} to sz(lst.size()), but that it was not recommended. I was curious what was a recommended fix. I'm trying to get into the habit of removing warnings that spring up, but I'm also wanting to avoid any frowned on practices.

On Sat, Dec 17, 2016 at 11:57 PM, DAVENPORT, MARC <[hidden email]> wrote:
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" <[hidden email]> 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
[hidden email]

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:[hidden email]?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
[hidden email]

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:[hidden email]?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
[hidden email]

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:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Copy Constructor; Programming Principles and Practice Using C++

DAVENPORT, MARC
Sorry for the top post earlier. On my phone again.

On Sun, Dec 18, 2016 at 12:18 AM, Clayton Weaver <[hidden email]> wrote:
The second warning I'm getting is a narrowing conversion:
Vector::Vector(initializer_list<double> lst) : sz{lst.size()}, elem{new double[sz]}
samp18_1.cpp:25:59: warning: narrowing conversion of 'lst.std::initializer_list<_E>::size<double>()' from 'std::initializer_list<double>::size_type {aka unsigned int}' to 'int' inside { } [-Wnarrowing]

Full code (per book):
Vector::Vector(initializer_list<double> lst) : sz{lst.size()}, elem{new double[sz]}
{
     std::copy(lst.begin(), lst.end(), elem);
}

sz is an int, and lst.size() is an unsigned int. Just cast lst.size() to an int and move on.


 

------------------------------------------------------------------------------
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
[hidden email]

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:[hidden email]?subject=unsubscribe
Loading...