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

  • SEARCH
  • Home
  • 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 499k
  • Answers 499k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Make sure you are using the latest EndlessAdapter, as it… May 16, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer I found out how: I hided the line below then… May 16, 2026 at 12:33 pm
  • Editorial Team
    Editorial Team added an answer There is no clear answer because it depends on your… May 16, 2026 at 12:33 pm

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

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 have a mbp, but I've been lazy and have been using ubuntu for
Can I connect to a Ubuntu server, that is running MySQL using ADO.NET or
I wanna write a little c++ program using libcurl. It's for a school project
I've started working with my first makefile. I'm writing a roguelike in C++ using
I created a simple .so library containing definition of a C++ class which should
I'm trying to setup CutyCapt on my Ubuntu 10.4 server. I generate a thumbnail
I recently just installed Eclipse onto a Ubuntu 10.04 machine. Now, I'm reading Getting

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.