Thread: deque::push_back()

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267

    deque::push_back()

    What's going on here?

    Code:
    instantiated from `void std::deque<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp = C_Projectile, _Alloc = std::allocator<C_Projectile>]'
    Code:
    error: no matching function for call to `C_Projectile::C_Projectile(const C_Projectile&)'
    when did I ever call this?

    Code:
    projectile.h:9: note: candidates are: C_Projectile::C_Projectile(C_Projectile&)
    projectile.h:12: note:                 C_Projectile::C_Projectile(C_Vector&, int)
    projectile.h:11: note:                 C_Projectile::C_Projectile()
    and the code:
    Code:
    C_Projectile p(TurretDirection, g_WeaponManager.WeaponInfo[this->CurrentWeapon].UsableProjectiles[0]);
    Projectiles.push_back(p);
    was previously
    Projectiles.push_back(C_Projectile(TurretDirection , g_WeaponManager.WeaponInfo[this->CurrentWeapon].UsableProjectiles[0]));
    but i split it up to find out which line was causing the error

    C_Projectile is a class

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh, looks like you did not write your copy constructor correctly. Your copy constructor should take a const reference as its argument, not a reference. std::deque() expects that your copy constructor would follow the norm, but it did not.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    That was simple...
    I've never had to write a copy constuctor b4, what am I supposed to have in it?

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    what am I supposed to have in it?
    It depends on your class, of course

    In this case, you want objects of your class to be stored in a standard container, you should provide a copy constructor if you need to do deep copying explicitly, e.g., because you have a dynamic array as a member. Consequently, you should also provide a copy assignment operator with the same semantics, and probably also implement the destructor.

    I would expect the copy constructor and copy assignment operator to be implemented along the lines of:
    Code:
    C_Projectile::C_Projectile(const C_Projectile& projectile)
    {
        // Code to make this new object a copy of projectile.
        // ...
    }
    
    C_Projectile& C_Projectile::operator=(const C_Projectile& projectile)
    {
        // Code to make this object a copy of projectile.
        // ...
        return *this;
    }
    Of course, if you use a standard container (or one by boost) instead of a dynamic array, then you would not need to write your own copy constructor, copy assignment operator, and destructor in the first place.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    Ok, got that fixed
    thanks

    Of course, if you use a standard container (or one by boost) instead of a dynamic array, then you would not need to write your own copy constructor, copy assignment operator, and destructor in the first place.
    I've always wanted to try out the boost library but I don't know how to set it up...

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Most of Boost is header-only. You download Boost, extract it, and set the include path of your compiler to look in the installation directory, too. E.g. if you install Boost to /usr/local/boost you pass -I/usr/local/boost to GCC on compiling to make it look there.
    Code::Blocks ought to have the setting somewhere in its options. I have no idea where.

    Getting the precompiled libraries is a bit trickier. I think Ubuntu ought to have a package for Boost. If not, though, it's mostly about getting bjam to run (download a binary separately, or compile one) and then invoking it at the root of the Boost tree.

    http://boost.org/more/getting_started/index.html
    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

Popular pages Recent additions subscribe to a feed