56

I have read other similar posts but I just don't understand what I've done wrong. I think my declaration of the vectors is correct. I even tried to declare without size but even that isn't working.What is wrong?? My code is:

#include <vector> 
#include <string>
#include <sstream>
#include <fstream>
#include <cmath>

using namespace std;

vector<string> v2(5, "null");
vector< vector<string> > v2d2(20,v2);

class Attribute //attribute and entropy calculation
{
    vector<string> name(5); //error in these 2 lines
    vector<int> val(5,0);
    public:
    Attribute(){}

int total,T,F;

};  

int main()
{  
Attribute attributes;
return 0;
}
user1484717
  • 877
  • 3
  • 9
  • 11
  • What are you trying to do with `5` and `0` in your calls to the `vector` constructor? Here is what the arguments mean: http://www.cplusplus.com/reference/stl/vector/vector/ – Michael Graczyk Jul 15 '12 at 09:57
  • I was simply trying to use: explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() ); as your link shows: vector second (4,100); – user1484717 Jul 15 '12 at 10:03
  • 3
    @Ajay: terrible notion, the `std` library is part of C++, you should certainly learn to use them together. – KillianDS Jul 15 '12 at 10:11
  • 1
    @KillianDS, Libraries aren't part of language (IMO), they are just standardized. `printf` and `cout` are not language concepts, they come from library. And I meant to OP that he/she must learn the basics first! – Ajay Jul 15 '12 at 15:04

3 Answers3

100

You cannot do this:

vector<string> name(5); //error in these 2 lines
vector<int> val(5,0);

in a class outside of a method.

You can initialize the data members at the point of declaration, but not with parenthesis (()). Instead, use curly braces ({}):

class Foo {
    vector<string> name = vector<string>(5);
    vector<int> val{vector<int>(5,0)};
};

Before C++11, you need to declare them first, then initialize them e.g in a contructor

class Foo {
    vector<string> name;
    vector<int> val;
 public:
  Foo() : name(5), val(5,0) {}
};
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Which compiler supports that "C++11" feature ? – Ajay Jul 15 '12 at 20:48
  • @Ajay GCC since 4.7, see [here](http://gcc.gnu.org/projects/cxx0x.html), probably the latest clang. Not sure what the VS release schedule is but I heard the newest version would support all features. – juanchopanza Jul 16 '12 at 05:17
  • Alright, thanks! I have GCC 4.6 on Windows, which doesn't support this. VC11 also doesn't support this. – Ajay Jul 16 '12 at 06:04
  • Could you also explain this: "you cannot do this, in a class OUTSIDE of a method" ? It sounds like we could ONLY IF we do it INSIDE of a method in a class, isn't it? @juanchopanza – Jason Jul 09 '18 at 23:14
  • 1
    @Jason Inside a method in a class is one of the places where you can do it. You can also do it in a non-member function, or when declaring, for example, a global variable. – juanchopanza Jul 10 '18 at 04:49
  • 1
    @juanchopanza Got it thank you! May I please also ask why is it not possible to do it for a class variable in a class, while we could for a global variable – Jason Jul 10 '18 at 20:37
  • 3
    @Jason The idea was to make it impossible to have a situation where declaring a data member could be parsed like a function. Loop up "the most vexing parse". – juanchopanza Jul 10 '18 at 20:42
  • Solution worked great for me! In my case, it was an Arduino project where I needed to instantiate an object of a class as a private object of another class, outside a method. I was getting an error with `SoftWire sw (3,4);` although this is the usual way of doing it. Based on the answer given, I tried `SoftWire sw = SoftWire(3, 4);` and it is working great now! Thanks!!! – Jeromy Adofo Sep 04 '20 at 22:48
11

Initializations with (...) in the class body is not allowed. Use {..} or = .... Unfortunately since the respective constructor is explicit and vector has an initializer list constructor, you need a functional cast to call the wanted constructor

vector<string> name = decltype(name)(5);
vector<int> val = decltype(val)(5,0);

As an alternative you can use constructor initializer lists

 Attribute():name(5), val(5, 0) {}
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
2

Since your compiler probably doesn't support all of C++11 yet, which supports similar syntax, you're getting these errors because you have to initialize your class members in constructors:

Attribute() : name(5),val(5,0) {}
slartibartfast
  • 4,348
  • 5
  • 31
  • 46
  • The provided syntax wouldn't work in `C++11` either, only `brace-or-equal` initialization is allowed when declaring a variable. – KillianDS Jul 15 '12 at 10:13