-5

i receive an error that the size of vector wasn't define. I cannot understand where I'm doing wrong.

#include <iostream>
using namespace std;

class A
{
    int valoare;
    int vector[];

public:
    A(int param1=3):valoare(param1) {}
    int get_Valoare() {return this->valoare;}
};

int main()
{
    A vector[]=(*(new A(3)), *(new A(4)), *(new A(5)), *(new A(6)));
    cout<<vector[2].get_Valoare();
    //cout << "Hello world!" << endl;
    return 0;
}
Post Self
  • 1,471
  • 2
  • 14
  • 34
Madalina
  • 85
  • 2
  • 15
  • 1
    you don't have a `std::vector`, you are trying to name an array `vector` (and that's definitely not how you initialize an array either) – UnholySheep Sep 18 '17 at 13:25
  • 2
    `int vector[];` is not legal C++. – nwp Sep 18 '17 at 13:26
  • 3
    Also what is the point of `*(new A(3))`?? It seems you should start with [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – UnholySheep Sep 18 '17 at 13:27
  • 2
    Unless your type is specifically designed to accommodate it, `*new T` is almost certainly not the right thing to do. – François Andrieux Sep 18 '17 at 13:27
  • `vector` is a **container class** and you are creating an array of name vector. This is not correct according to C++ standards. Read more about [vectors](http://www.cplusplus.com/reference/vector/vector/) – Ishpreet Sep 18 '17 at 13:37
  • Your initialization is equivalent to `A vector[] = *(new A(6));` and a bunch of additional memory leaks. – molbdnilo Sep 18 '17 at 13:47

1 Answers1

1

If you want to declare a vector of the type A, which has a constructor that takes a single int, refer to the example below:

#include <vector>

std::vector<A> my_vector{3, 4, 5, 6};
Post Self
  • 1,471
  • 2
  • 14
  • 34