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

Related Questions

#include <boost/regex.hpp> #include <string> #include <iostream> using namespace boost; static const regex regexp( std::vector<
#include <boost/regex.hpp> #include <string> #include <iostream> using namespace boost; static const regex regexp( std::vector<
#include<iostream> #include<vector> #include<stdexcept> #include<stdio.h> using namespace std; void keep_window_open() { cin.clear(); //clear badbit flag
I tested the following code: #include <iostream> #include <vector> class foo { public: int
I have the following code #include <iostream> using namespace std; void WaitForEnter() { while(1)
I am using Ubuntu-11.04 OS. i wrote a basic interactive kernel module mid.c #include<linux/kernel.h>
I'm using Ubuntu 10.10 So that's what I did. Hello.java : class Hello {
Using Ubuntu, when I run bundle install to set up my Rails environment, it
I am currently using Ubuntu 11.10 and java SE 1.6.0_26. I am trying to
I'm using Ubuntu 12.04 I tried to change my default apache2 port from 80

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.