C++ vectors of classes with constructors

前端 未结 3 558
后悔当初
后悔当初 2021-01-06 00:20
//Using g++ and ubuntu.
#include 
using namespace std;

Define a class:

class foo(){
(...)
foo(int arg1, double arg2);         


        
相关标签:
3条回答
  • 2021-01-06 01:00

    Why are you using new when no dynamic memory needs to be created? Of course using new will fail, it results in a foo* when push_back accepts a foo. (That's what you have a vector of, after all.)

    What's wrong with push_back? If you want to reserve memory up front, use reserve(); providing a number in the constructor of vector makes that many copies of the second parameter (which is implicitly foo(), which won't work hence your errors), which isn't the same as simply reserving memory.

    If doing things correctly (no new) crashes, the fault is in your code and not vector. You probably haven't written a proper class that manages resources.* (Remember The Big Three, use the copy-and-swap idiom.)

    *I say this because you say "//arrays whose length depend upon arg1 and arg2 ", which I suspect means you have new[] in your class somewhere. Without the Big Three, your resource management will fail.

    You shouldn't be managing resources anyway, classes have one responsibility. That means it should either be a dynamic array, or use a dynamic array, but not both manage and use a dynamic array. So factor out the resources into their own class, and then make another class (yours) which uses them. A dynamic array is a std::vector, so you are already done with that. Any time you need a dynamic array, use a vector; there is never a reason not to.

    0 讨论(0)
  • 2021-01-06 01:00
    vector<foo> bar(10); //error: no matching function for call to 'foo::foo()'
    

    This is failing because the std::vector constructor you're calling is

    explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );
    

    As you can see, it is trying to fill the vector with 10 calls to the default constructor of foo which does not exist.

    Also, all your examples featuring new will fail because the vector is expecting an object of type foo, not foo *. Furthermore, changing to vector<foo *> will fail too unless you manually delete every member before clearing the vector. If you really want to go the dynamic memory allocation route create a vector< shared_ptr< foo > >. shared_ptr is available in the Boost libraries or if your compiler includes TR1 libraries it'll be present in the <memory> header within the std::tr1 namespace or if your compiler has C++0x libraries it'll be available in the std namespace itself.

    What you should probably do is the following:

    vector<foo> bar;
    bar.reserve(10);
    bar.push_back( foo( 1, 2 ) );
    ...
    ...
    bar.push_back( foo( 10, 20 ) ); //10 times
    
    0 讨论(0)
  • 2021-01-06 01:10

    std::vector always create elements based on the default constructor which you haven't define in snippet above.

    the push_back method is facing a double free issue because you did not handle the copy constructor.

    0 讨论(0)
提交回复
热议问题