declare std::vector with N size in struct member

Hi, how could i do this?
thanks.

using sv = struct { any sym; any val; };
using svv = std::vector<sv>;

svv C(f(x)); // works fine here. // int f(any x);

struct
{
svv B(f(x)); //error C2061: syntax error: identifier 'f'

//sv *B;
}A;

// A.B = (sv*)malloc(...) // works fine.
Last edited on
svv C(f(x)); // works fine here. // int f(int x);
This is creating an object. This code gets executed and when it's finished, an svv object exists.

1
2
3
4
struct
{
   svv B(f(x)); //error C2061: syntax error: identifier 'f'
...


This is describing the inside of a class. No code gets executed here. This is a description of what's inside the class (a.k.a. struct). Putting f(x) there makes no sense. You're saying that B is a function that returns an object of type svv, and accepts an object of type f(x)? That doesn't make any sense.

If you want to specify that upon creation, this struct's object "B" should be initialised with whatever comes back from the function call f(x), you're going to need to write the constructor.
Last edited on
1
2
3
4
struct
{
	svv B{f(x)};
} A;

This should work with a modern compiler, assuming x is in scope and f(x) returns an svv object.
Hi, Peter87, your code also not worked with vs2015 c++.

struct
{
svv B{f(x)};
} A;

i tested it in routine any doRun(any x){} in flow.cpp
you can review my porting code is here:

https://github.com/sailfish009/minipicolisp (currently works fine, but i want more).

the compile error message is :

// reference to local variable of enclosing function is not allowed. // this is popup error message within vs2015 IDE.

error C2326: 'doRun::<unnamed-type-f>::<unnamed-type-f>(void)': function cannot access 'x'
note: This diagnostic occurred in the compiler generated function 'doRun::<unnamed-type-f>::<unnamed-type-f>(void)'


Last edited on
You'd have to post a *minimal* program that produces that message: there is no "doRun" in the code you're showing, and flow.cpp on that github is a wall of code

To fix the code you've shown in the original post (and to complete it to a minimal example)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <vector>
#include <iostream>
using sv = struct { int sym; int val; };
using svv = std::vector<sv>;
int x = 8;
svv f(int sz) { return svv(sz); }
struct 
{
//  svv B(f(x)); // Error: can't use parentheses here
  svv B{ f(x) }; // OK: braces allowed
  svv C = f(x);  // OK: equals sign allowed
} A;

int main() {
    std::cout << "A.B.size = " << A.B.size() << '\n'
              << "A.C.size = " << A.C.size() << '\n';
}

live demo: https://wandbox.org/permlink/F5HRn4fOTSHTuCUX
(and it works in a copy of VS 2015 I have, too)

PS: I think your main problem here is attempting to define complex classes inside a function. Those "local classes" have a number of limitations, one of which is that references to a local variable in the enclosing function are not allowed, exactly as the compiler said.
Define classes at file (namespace) scope, and/or pass that x in as a constructor argument.
Last edited on
Topic archived. No new replies allowed.