Apr 18, 2018 at 7:29pm UTC
Hello all,
I have a problem in using "auto" declaration. I write a program in Visual Studio 2017 as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
class MyClass
{
public :
struct mystruct {
vector<int > vi;
};
vector<mystruct> TheStructV;
void AddStructV() {
TheStructV.push_back(mystruct());
};
};
int main()
{
MyClass MyObj[3];
for (int a = 0; a < 3; a++) {
MyObj[a].AddStructV();
for (int i = 1; i <= 5; i++) {
MyObj[a].TheStructV[MyObj[a].TheStructV.size() - 1].vi.push_back(i * 10 + idx);
}
idx++;
}
for (int b = 0; b<3; b++) {
cout << "MyObj[" << b << "] struct vector size:" << MyObj[b].TheStructV.size() << endl;
cout << "MyObj[" << b << "] struct vi size:" << MyObj[b].TheStructV[0].vi.size() << endl;
}
for (int i = 0; i < 3; i++) {
cout << "MyObj[" << i << "].vi:" ;
for (int j = 0; j < 5; j++) {
cout << MyObj[i].TheStructV[0].vi[j] << "-" ;
}
cout << endl;
}
return 0;
}
It works as expected and the output is:
MyObj[0] struct vector size:1
MyObj[0] struct vi size:5
MyObj[1] struct vector size:1
MyObj[1] struct vi size:5
MyObj[2] struct vector size:1
MyObj[2] struct vi size:5
MyObj[0].vi:11-21-31-41-51-
MyObj[1].vi:12-22-32-42-52-
MyObj[2].vi:13-23-33-43-53-
However, if I change to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
class MyClass
{
public :
struct mystruct {
vector<int > vi;
};
vector<mystruct> TheStructV;
void AddStructV() {
TheStructV.push_back(mystruct());
};
};
int main()
{
MyClass MyObj[3];
int idx = 1;
for (auto M : MyObj) {
M.AddStructV();
for (int i = 1; i <= 5; i++) {
M.TheStructV[M.TheStructV.size() - 1].vi.push_back(i * 10 + idx);
}
idx++;
}
for (int b = 0; b<3; b++) {
cout << "MyObj[" << b << "] struct vector size:" << MyObj[b].TheStructV.size() << endl;
cout << "MyObj[" << b << "] struct vi size:" << MyObj[b].TheStructV[0].vi.size() << endl;
}
idx = 1;
for (auto MC : MyObj) {
cout << "MyObj[" << idx - 1 << "].vi:" ;
for (auto thisStruct : MC.TheStructV) {
cout << thisStruct.vi[0] << "-" ;
cout << thisStruct.vi[1] << "-" ;
cout << thisStruct.vi[2] << "-" ;
cout << thisStruct.vi[3] << "-" ;
cout << thisStruct.vi[4] << "-" ;
}
cout << endl;
idx++;
}
return 0;
}
It compile without problem but got error when run and the output is:
MyObj[0] struct vector size:0
Program stuck here.
Seems that I am missing something, anyone help? I tried replace auto M with MyClass M and still the same. Thanks a lot.
Regds
LAM Chi-fung
Last edited on Apr 19, 2018 at 5:12am UTC
Apr 18, 2018 at 7:50pm UTC
Read the sticky on how to code tag.
Make your autos a reference like: auto& this will prevent unnecessary copying.
When you call M.AddStructV();
you are actually adding it to a copy.
Apr 18, 2018 at 7:51pm UTC
I can't get the "format" work, anyone point me the right way ? thx
Apr 18, 2018 at 8:12pm UTC
Oh, sorry I had to read what I said before understanding what you said!
I didn't know the sticky doesn't tell how to format.
You need to press the <> button in the edit page.
and re-copy and paste your code with spaces.
Last edited on Apr 18, 2018 at 9:32pm UTC
Apr 19, 2018 at 5:14am UTC
@poteto
thanks for your help, eventually I got it works (both the program and format).