Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 983797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:54:52+00:00 2026-05-16T04:54:52+00:00

//Using g++ and ubuntu. #include <vector> using namespace std; Define a class: class foo(){

  • 0
//Using g++ and ubuntu.
#include <vector>
using namespace std;

Define a class:

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

Constructor:

foo::foo(int arg1, double arg2){ 
(...) //arrays whose length depend upon arg1 and arg2
} 

I would like to do something like this:

vector<foo> bar(10); //error: no matching function for call to 'foo::foo()'
bar[0] = new foo(123, 4.56);
(...)

An alternative method (which I like less) is to use push_back:

vector<foo> bar; //works
bar.push_back(new foo(123, 4.56)); //throws similar error.
//Omitting the "new" compiles but throws a "double free or corruption (fasttop)" on runtime.

I want different elements of the vector to be constructed differently, so I don’t want to use the “Repetitive sequence constructor”.
What should be done?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:54:53+00:00Added an answer on May 16, 2026 at 4:54 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 488k
  • Answers 488k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer While you can hash a user_id and secret_key, anyone who… May 16, 2026 at 8:47 am
  • Editorial Team
    Editorial Team added an answer This is probably coming a little bit late, but I've… May 16, 2026 at 8:47 am
  • Editorial Team
    Editorial Team added an answer OK, so I solved this, but it's really not pretty...… May 16, 2026 at 8:47 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

#include<iostream> using namespace std; int main() { int hash, opp, i, j, c =
I'm trying to load binary file using fstream in the following way: #include <iostream>
When I try to include a file using include_once in php which shows warning
I am trying to get PhysX working using Ubuntu. First, I downloaded the SDK
I'm developping a very simple app on my Ubuntu using QtCreator. It's a console
I'm using CodeLite on Ubuntu and for some bizzare reason GCC keeps throwing this
I've been using virtualenv on Ubuntu and it rocks, so I'm trying to use
I'm setting up a C++ project, on Ubuntu x64, using Eclipse-CDT. I'm basically doing
I am using VMWare tools for Ubuntu Hardy, but for some reason vmware-install.pl finds
I'm using GCC 4.3.3 on Ubuntu 9.04 64-bit and was getting errors using C++-style

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.