-
July 16th, 2021, 06:40 PM
#1
[RESOLVED] Problem with vector<vector<struct>>?
Hi,
This problem took me a long time to fix but it didn't work ..
I have a functions like this:
Code:
struct ST1 //define global outsize class;
{
///...int,bool,..
CString abc = L"";
int a = 1; //
int b = 1; //
int c=-410;
int d=-410;
};
class myclass{
public:
void myfunction(bool mycase, vector<vector<ST1>>&); //can't initialize this vector.
}
//cpp
void myclass::myfunction(bool mycase, vector<vector<ST1>>& vec){
for(int i=1;...)
{
vector<ST1> vec1;
for(int j=1;..)
vec1.pushback(ST1{..,..,..}); //ok
}
vec.pushback(vec1);// <-- error at this code
}
}
then I call by this code:
Code:
vector<vector<ST1>> vec;
myclass* cls;
//..initialize cls
cls->myfunction(true, vec); //error bad array.. at vec.pushback(vec1)
now, I get these error:
Update: this code runs fine if i use vector<vector<struct>> directly, but error occurs only after i use it through parameter in function.
How i can fix this error?
Thanks a lot!
Last edited by Dang.D.Khanh; July 17th, 2021 at 12:54 AM.
-
July 17th, 2021, 01:02 AM
#2
Re: Problem with vector<vector<struct>>?
Hi,
When I debug myfunction, the vector is automatically initialized like this image
Is this a problem?
-
July 17th, 2021, 04:11 AM
#3
Re: Problem with vector<vector<struct>>?
Is it now working?
What are the sizes of the vectors?
Which compiler version are you using?
Code:
for(int j=1;..)
vec1.push_back(ST1{..,..,..}); //ok
}
It's more efficient to do it like this:
Code:
for(int j=1;..)
vec1.emplace_back(..,..,..);
}
Last edited by 2kaud; July 17th, 2021 at 04:25 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 17th, 2021, 04:25 AM
#4
Re: Problem with vector<vector<struct>>?
This compiles and runs OK with VS2019:
Code:
#include <vector>
#include <string>
using namespace std;
struct ST1 {
string abc;
int a = 1;
int b = 1;
int c = -410;
int d = -410;
};
class myclass {
public:
void myfunction(bool mycase, vector<vector<ST1>>&);
};
void myclass::myfunction(bool mycase, vector<vector<ST1>>& vec) {
for (int i = 1; i < 5; ++i) {
vector<ST1> vec1;
for (int j = 1; j < 10; ++j)
vec1.emplace_back("foobar", i, j, i + j, i - j);
vec.push_back(vec1);
}
}
int main()
{
vector<vector<ST1>> vec;
myclass* cls {new myclass};
cls->myfunction(true, vec);
delete cls;
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
July 17th, 2021, 04:25 AM
#5
Re: Problem with vector<vector<struct>>?
Hi Sir,
This vector will be 12x3(rows/columns) after emplaceback .
When I debug, this param always initializes a lot of elements like the following image.
-
July 17th, 2021, 04:31 AM
#6
Re: Problem with vector<vector<struct>>?
Hi,
I'm using vs2019 -vs16.10
Tried declaring 2D vector bounds but still can't pass function
//setbound vector 12x3
vector<vector<properties>> vr{rows, vector<properties>(cols) }; //properites is struct
myfunction(tb, vr);
I'm not sure what affected it.
-
July 17th, 2021, 04:34 AM
#7
Re: Problem with vector<vector<struct>>?
Originally Posted by 2kaud
This compiles and runs OK with VS2019:
[/code]
Thanks sirs.
I'll check it again.
-
July 17th, 2021, 05:05 AM
#8
Re: Problem with vector<vector<struct>>?
Hi Sir,
I'm not sure but the class myclass is declared and implemented in another library .dll with dllexport,
If I use this class in the same calling project the code runs fine.
Can I use it as a function call from the dllexport library?
my header file from library like this:
Code:
struct dllexport ST1 {
string abc;
int a = 1;
int b = 1;
int c = -410;
int d = -410;
};
class dllexport myclass {
public:
void myfunction(bool mycase, vector<vector<ST1>>&);
};
Last edited by Dang.D.Khanh; July 17th, 2021 at 05:24 AM.
-
July 17th, 2021, 11:01 AM
#9
Re: Problem with vector<vector<struct>>?
Hi Sir,
my problem is probably by 2 projects setting different (maybe the wizard changed it without me knowing).
after matching MD and MDd now it works as i expected !.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|