0

I defined a structure as follows.

extern int x;
a = 1;
b = 2;
x = a*b;
struct  bStruct
{
    unsigned long   Cycles;                     
    unsigned long   Time;                                           
    std::vector<unsigned long> Chunks(x);
};

but x in the Chunks definition is noted as an error with a red line underneath it.

The error message is:

variable "x" is not a type name.

Why did this error happen, and how do I fix it?

Baum mit Augen
  • 49,044
  • 25
  • 144
  • 182
goosli
  • 25
  • 5
  • With this string `std::vector Chunks(x);` what do you want to do? set the size of vector to x? if you use C++98 there is no constructor with one `int` argument try this `std::vector Chunks(x, 0);` or you can init this vector in constructor of bStruct. – Mike Minaev Oct 29 '14 at 09:29
  • 3
    Get a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). You can't learn C++ via StackOverflow. – Jonathan Wakely Oct 29 '14 at 09:55
  • Thanks for your helpful input! I will check out the books. – goosli Oct 29 '14 at 11:39

2 Answers2

4

Your compiler thinks that Chunks is a function, parsing it like so:

std::vector<unsigned long> Chunks( x );
            |               |      |
            |               |      +---Wait a sec huh ? what type is 'x' ?? 
            |               |
            |               +---------Function name Chunks
            +----------Return Type std::vector<unsigned long>

You want Chunks to be a data member, which you need to initialize using:

struct bStruct
{
    unsigned long Cycles;                     
    unsigned long Time;                                           
    std::vector<unsigned long> Chunks;

    bStruct() : Chunks(x)
    {
      // Constructor
    }
};
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
P0W
  • 46,614
  • 9
  • 72
  • 119
  • Thanks for your answer. It works! I am very new to C++, so please bear with me. I have some more questions: 1 Why is there brackets in bStruct(): Chunks? Why it is not bStruct(). Chunks? 2 Why std::vector(x) instead of just x? 3 What is the usage of the curly brackets in the end? Thanks! – goosli Oct 29 '14 at 09:40
  • The last line here defines the `bStruct`'s constructor, and this constructor initializes `Chunks` with `std::vector(x)` using an initialization list (see [here](http://en.cppreference.com/w/cpp/language/initializer_list), though there might be some clearer explanation for a beginner out there) – Dettorer Oct 29 '14 at 09:50
  • 2
    it is a default constructor(without arguments). after `:` it is an initializing list, this means that variable `Chunks` of `bStruct` inits with std::vector(x), and `{}` it is empty body of constructor. it same to `bStruct(){Chunks = std::vector(x); }` – Mike Minaev Oct 29 '14 at 09:52
  • Thanks a lot for explaining in such details. I am really glad I found Stackoverflow today! – goosli Oct 29 '14 at 11:40
  • 1
    @goosli: _"Why std::vector(x) instead of just x"_ Good question. In fact, just `x` is fine. I've edited this answer a bit. – Lightness Races in Orbit Oct 29 '14 at 11:53
  • @MikeMinaev: It's not the "same". One is an initialisation; the other is a default initialisation followed by an assignment. Try it with a `const` member and you'll soon see the difference. – Lightness Races in Orbit Oct 29 '14 at 11:54
  • There are so many smart people here! I have one more dumb question please. (I will go to read books to learn C++ afterwards) I tried to use this method to define a few arrays with with variable array sizes. The first one works fine, but the following ones got an error at bStruct() saying "invalid redeclaration of member function bStruct::bStruct()". How did this happen, and how to fix it? Many thanks! – goosli Oct 29 '14 at 13:03
  • @goosli Please search what _re-declaration_ means, you'll find plenty of answers on SO, and if you're **really** lost, come with a new post, thanks ! – P0W Oct 29 '14 at 13:12
1

std::vector<unsigned long> Chunks(x); the compiler will treat Chunks as a function not a variable. the initialization of the struct member will not allowed in the defination of struct(class). you can fix it using construction function:

struct  bStruct
{
    unsigned long   Cycles;                     
    unsigned long   Time;                                           
    std::vector<unsigned long> Chunks;
    bStruct(int x):Chunks(x) {};
};
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
wangsquirrel
  • 135
  • 2
  • 12
  • Thanks! I am very new to C++, so please bear with me. I have some more questions: 1 What does bStruct():Chunks mean or what it does? 2 Why the size of Chunks is std::vector(x) instead of just x? 3 What is the usage of the curly brackets in the end? Thanks a lot! – goosli Oct 29 '14 at 09:51
  • @goosli the initialization list is more useful than assignment initialization. – wangsquirrel Oct 30 '14 at 07:55