473,322 Members | 1,398 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

initializing a vector

Hi,

If I have a class A without a default constructor, and I try to create an array in the fillowoing way:

A arr[0];

I get a compilation error.

why when I use

std::vector<A> vec;
or
std::map<int,A> myMap

I don't get a compilation error?

Amir
Oct 16 '07 #1
3 1652
Laharl
849 Expert 512MB
In order for that to work, class A must have a default constructor since there is no way to pass arguments to the constructors.

Also, A arr[0] initializes an array with no elements. I imagine your compiler has something to say about that...
Oct 16 '07 #2
In order for that to work, class A must have a default constructor since there is no way to pass arguments to the constructors.

Also, A arr[0] initializes an array with no elements. I imagine your compiler has something to say about that...
I think yu mis-understood. A arr[0] generete an error. std::vector<A> vec does not.

My question is why (why it does not for vector)

Amir
Oct 16 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
I think yu mis-understood. A arr[0] generete an error. std::vector<A> vec does not.

My question is why (why it does not for vector)
std::vector<A> vec does not create any A objects. It just creates a container capable of holding A objects. There is a vector constructor for that.

When you create an A object like this:
Expand|Select|Wrap|Line Numbers
  1. A obj;
  2.  
you will need a default constructor for A depending upon the data members of A.
Oct 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: hrmadhu | last post by:
Hi, I wish to declare a vector of deque of int, which I do as follows. #include<vector> #include<deque> #include<iostream> using namespace std; int main(int argc, char* argv) {
16
by: Emanuel Ziegler | last post by:
Hi, I am using the vector class from the STL. Normally, I initialize values element by element, which is very uncomfortable and sometimes impossible (e.g. when passing a constant vector to an...
6
by: Matthias | last post by:
Hi, say I have a vector v1: std::vector<SomeType> v1; and I need a vector v2 of pointers to v1's elements: std::vector<SomeType*> v2;
5
by: pmatos | last post by:
Hi all, I have a vector of vector of ints, I could use C approach by using int but I think C++ vector<vector<int> > would be easier to manage. So I have a function which creates and initializes...
9
by: Dennis Jones | last post by:
Hi, I have some old code that I am refactoring to use smart pointers and have run into a small problem. My original code looks something like this: class WorkerThread { std::map<int,...
3
by: eriwik | last post by:
I use the following structure to store filenames for one or more "sets" grouped together by a number: map<int, map<string> > fileSets; As arguments to the constructor I send a...
4
by: jayharris | last post by:
I'm having a ton of trouble initializing a multi-dimensional array inside a constructor, largely because I don't know the size of the array until runtime. I have a class that looks like this: ...
13
by: John | last post by:
Is this a valid C++ program that will not crash on any machine? #include <iostream> using namespace std; int main( void ) { int i; cin >i; double X; X = 1123;
4
by: fatgirl.brown | last post by:
Hi all, I am attempting to initialize a vector of a vector in a constructor with some clean-looking syntax and am not sure of how to go about this. For instance: double weight; vector<...
4
by: Peskov Dmitry | last post by:
class simple_class { int data; public: simple_class() {data=10;}; simple_class(int val) : data(val){} }; int main() {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.