Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a fixed length vector in a struct - C++

Tags:

c++

I'm trying to do something like that:

//stack.h

#ifndef STACK_H_INCLUDED
#define STACK_H_INCLUDED
#include <vector>

struct CharStack {
    int sp;
    std::vector<char> data(87);
} S;

But I get some errors like:

error: expected identifier before numeric constant
error: expected ',' or '...' before numeric constant

Why does that happen? There seems to be no problem when I want to create a vector with dynamic length

like image 647
user2777080 Avatar asked Dec 05 '22 09:12

user2777080


1 Answers

To construct objects in a struct (or class) you need to write a constructor. Like this

struct CharStack {
    CharStack() : data(87) {}
    int sp;
    std::vector<char> data;
} S;

It's just how C++ syntax is.

like image 80
john Avatar answered Dec 09 '22 14:12

john