base class should be explicitly initialized in the copy constructor

This is a discussion on base class should be explicitly initialized in the copy constructor within the C++ Programming forums, part of the General Programming Boards category; Hi, I have a question regarding the warning that I get when I compile a program using boost::ublas library. I ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    7

    base class should be explicitly initialized in the copy constructor

    Hi,

    I have a question regarding the warning that I get when I compile a program using boost::ublas library. I have to say that I'm not really experienced programmer so I might be doing something really stupid...

    Anyway, I'm using WinXP with Mingw (gcc 3.4.5) and boost 1.34-1.

    I want to create a simple function that returns a vector. I have it written like this:


    ===> Definition (file.h )
    Code:
    #include <boost/numeric/ublas/vector.hpp>
    typedef boost::numeric::ublas::vector<double> vec;
    
    
    vec testfunc (vec &v);
    ===> Implementation (file.cpp)
    Code:
    #include "file.h"
    
    vec testfunc (vec &v)
    {
        vec v1 = v/2;
        return v1;
    }

    When I try to compile, I get the following warning:

    base class `class boost::numeric::ublas::storage_array<boost::numeri c::ublas::unbounded_array<double, std::allocator<double> > >' should be explicitly initialized in the copy constructor

    What am I doing wrong and how can I fix this? The program works, but I'd like to get rid of the warning

    Thank you very much for your answers.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    Change:
    Code:
    vec v1 = v/2;
    to:
    Code:
    vec v1(v/2);
    The latter is the syntax that must be used when a constructor that takes one argument is declared explicit.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    Quote Originally Posted by laserlight View Post
    Change:
    Code:
    vec v1 = v/2;
    to:
    Code:
    vec v1(v/2);
    The latter is the syntax that must be used when a constructor that takes one argument is declared explicit.
    I changed as you said, but the warnings are still there. Could it be something else wrong?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    I changed as you said, but the warnings are still there. Could it be something else wrong?
    I have never used that library, so I do not know. I suggest you try a minimal example:
    Code:
    #include <boost/numeric/ublas/vector.hpp>
    typedef boost::numeric::ublas::vector<double> vec;
    
    int main()
    {
        vec x;
        vec y(x);
        x = y;
    }
    What warnings do you get?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    With the minimal example you've suggested:
    Code:
    1  #include <boost/numeric/ublas/vector.hpp>
    2  typedef boost::numeric::ublas::vector<double> vec;
    3 
    4  int main()
    5  {
    6      vec x;
    7      vec y(x);
    8      x = y;
    9      return 0;
    10 }
    I get completely the same warnings:

    Code:
    line 68: instantiated from `boost::numeric::ublas::vector<T, A>::vector(const boost::numeric::ublas::vector<T, A>&) [with T = double, A = boost::numeric::ublas::unbounded_array<double, std::allocator<double> >]'
    
    line 7: instantiated from here
    
    C:\Programs\MinGw\include\boost\numeric\ublas\storage.hpp: line 93: warning: base class `class boost::numeric::ublas::storage_array<boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >' should be explicitly initialized in the copy constructor
    :: === Build finished: 0 errors, 1 warnings ===

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,264
    hmm... I am still not sure what is wrong. Perhaps try an example from the Boost's page:
    Code:
    #include <boost/numeric/ublas/vector.hpp>
    #include <boost/numeric/ublas/io.hpp>
    
    int main () {
        using namespace boost::numeric::ublas;
        vector<double> v (3);
        for (unsigned i = 0; i < v.size (); ++ i)
            v (i) = i;
        std::cout << v << std::endl;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    This is really a bit weird. If I try the example from the boost, as you suggested, everything is OK. No warnings.

    Than I add the following:
    Code:
        vector<double> v1 = v/3;
    and I get the same warning.

    If I add, as you suggested in first post:
    Code:
        vector<double> v1(v/3);
    the compilation finishes without warnings.

    Weird...

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    Obviously the warning has to do something with the definition (or implementation) of a function. If I add some function (testfunc) to the program, as written below, I get warning. Despite the fact, that I used explicit declaration inside of the function "testfunc". Notice, that I don't use a function at all. I just define and implement it.

    The code that I wrote is:
    Code:
    #include <boost/numeric/ublas/vector.hpp>
    #include <boost/numeric/ublas/io.hpp>
    typedef boost::numeric::ublas::vector<double> vec;
    
    vec testfunc (vec &v)
    {
        vec v1(v/3);
        return v1;
    }
    
    int main () {
        vec v (3);
        for (unsigned i = 0; i < v.size (); ++ i)
            v (i) = i;
        std::cout << v << std::endl;
    }

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If it says that the "base class ... should be explicitly initialized in the copy constructor" could it be that it is the problem of the library, or is the header used in an unusual way. I wonder what it would take to generate this warning without boost?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You get the problem whenever you invoke (and thus cause to be instantiated) the copy constructor of an ublas vector. This causes the unbounded_storage copy constructor to be called and instantiated. And this copy constructor apparently fails to explicitly initialize its base object, which is of type storage_array.

    This makes me wonder: what version of Boost do you use? Both my Boost 1.33.1 and my Boost trunk (recently branched to become Boost 1.35) do not have this problem. A 1.33.0 I found lying around doesn't have it either.

    In any case, you can ignore the warning. It's simply telling you that some code in Boost is doing implicitly what would be better practice to do explicitly.

    anon: To generate the warning, I think this code will suffice. It also demonstrates the reason why the warning is there.
    Code:
    class A
    {
      int x;
    
    public:
      A() : x(0) {}
      void setX(int ax) { x = ax; }
      int getX() { return x; }
    };
    
    class B : public A
    {
    public:
      B() {}
      B(const B&o) {}
    };
    
    int main()
    {
      B b1;
      b1.setX(5);
      B b2(b1);
      std::cout << b2.getX() << '\n';
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    From the first post: 1.34-1.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    7
    Thank you very much for all the answers.

    I'm using Boost 1.34-1 from 7th of July 2007 that I got on sourceforge (http://sourceforge.net/project/showf...group_id=7586). To me it looked like this is a stable release. I have also asked the same question on the "boost-user" mailing list, but I got no answer so far.

    Anyway, I will ignore the warning, and when new official/stable version is released, I'll get that one.

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Ah, yes, 1.34.1 does indeed have the problem I suspected.

    If you want, you can fix it yourself, by inserting the following line in boost/numeric/ublas/storage.hpp between lines 92 and 93:
    Code:
                storage_array<self_type, ALLOC>(),
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by CornedBee View Post
    In any case, you can ignore the warning. It's simply telling you that some code in Boost is doing implicitly what would be better practice to do explicitly.
    I don't think you can ignore it. The behavior is different in the two cases. See this:

    Code:
    #include <iostream>
    
    class A
    {
      int x;
    
    public:
       A() : x(0) {std::cout << "Default constructor" << std::endl; }
       A(const A &o) : x(o.x) {std::cout << "Copy constructor" << std::endl; }
      void setX(int ax) { x = ax; }
      int getX() { return x; }
    };
    
    class B : public A
    {
    public:
      B() {}
      B(const B&o):A(o) {}
    };
    
    int main()
    {
      B b1;
      b1.setX(5);
      B b2(b1);
      std::cout << b2.getX() << '\n';
    }
    Try this with, and without, the highlighted part. You get two different results. In other words, if you do not explicitly initialize the base, the compiler will NOT use its copy constructor but rather its default constructor. In most situations, you want to use the copy constructor on the base in a derived copy constructor.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I think he meant that you can ignore the warning in the specific case of that boost class.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 06:11 PM
  2. Two conceptual questions
    By AntiScience in forum C++ Programming
    Replies: 3
    Last Post: 11-01-2007, 12:36 PM
  3. copy constructor prolbem(??) in a class Name
    By kantze in forum C++ Programming
    Replies: 15
    Last Post: 07-23-2007, 12:07 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Copy Constructor Help
    By Jubba in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 11:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21