CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2021
    Posts
    51

    [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:

    Name:  bug1.png
Views: 299
Size:  2.5 KB

    Name:  bug2.jpg
Views: 202
Size:  7.5 KB

    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.

  2. #2
    Join Date
    Jun 2021
    Posts
    51

    Re: Problem with vector<vector<struct>>?

    Hi,
    When I debug myfunction, the vector is automatically initialized like this image
    Is this a problem?

    Name:  bug1.jpg
Views: 253
Size:  25.6 KB

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Problem with vector<vector<struct>>?

    a long time to fix
    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)

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  5. #5
    Join Date
    Jun 2021
    Posts
    51

    Re: Problem with vector<vector<struct>>?

    Hi Sir,
    This vector will be 12x3(rows/columns) after emplaceback .

    Name:  bug2.jpg
Views: 218
Size:  6.2 KB

    When I debug, this param always initializes a lot of elements like the following image.

    Name:  bug3.png
Views: 149
Size:  4.9 KB

    Name:  bug4.png
Views: 224
Size:  5.4 KB

  6. #6
    Join Date
    Jun 2021
    Posts
    51

    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.

  7. #7
    Join Date
    Jun 2021
    Posts
    51

    Re: Problem with vector<vector<struct>>?

    Quote Originally Posted by 2kaud View Post
    This compiles and runs OK with VS2019:

    [/code]
    Thanks sirs.
    I'll check it again.

  8. #8
    Join Date
    Jun 2021
    Posts
    51

    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.

  9. #9
    Join Date
    Jun 2021
    Posts
    51

    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
  •  





Click Here to Expand Forum to Full Width

Featured