CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2022
    Posts
    14

    [RESOLVED] Returning a 2D Vector from a function through a pointer to a different class

    I am making a game that spawns animated 'pixel art' cats and other animals.... These are your typical in-game pets, which get spawned next to the player.

    I am using the Meow class to hold sprite data for the cat's in-game object. I'm sending that data (through a pointer ->) to favorite_pet::initialize(), which loads the sprite data for the pet.

    Unfortunately, using the debug features are of no help. Everything checks out until the following line...

    Code:
    vec_cat_data_input_2D = point_kitty->cat_data(vec_cat_sprite_ids);
    ...then BOOM! The system crashes and all of the cats stop meowing.

    I suspect sending that 2D vector though a function call in a pointer to a class is the problem.

    However, if I do this same thing with a 1D vector and a variable, everything is fine.

    Code:
    vec_cat_data_input_1D = point_kitty->cat_data(variable_cat_sprite_ids); // Is okay!
    The 2D vector being received could be of any size (rows or columns). This setup is going to be the prototype for a group of classes that are not going to be children of the class holding the data. The pets are children of physics and api image ctrl features. Thus, I don't want that data being stored in the objects that ctrl the in-game sprites.

    With that same idea in mind, I am not trying to superglue a dozen pointers together (two per class) so I can predict (n,m) ahead of time. Likewise, I realize that I could be using pointer functions or void functions with pointer vectors to do the processing and data tranfer. I'm trying to avoid any unecessary memory allocations, pointer cleanup or global data. I am hoping for something more memory effient.

    However, I do use ClassObject* point_blah; to call functions rather than calling objects directly.

    Code:
    #include "favorite_pet.h"
    void main()
    {
        favorite_pet NewPet;
        NewPet.initialize();
    }

    Code:
    #pragma once
    // favorite_pet.h
    #include "meow.h"
    class favorite_pet
    {
    private:
    
        Meow* point_kitty;
    
        // do not want to create two more pointers just so I can predict what the (n,m) sizes might be...
        std::vector<std::vector<float>> vec_cat_data_input_2D;
    
    public:
        // constructor
        favorite_pet();
    
        void initialize();
    
        std::vector<int> vec_cat_sprite_ids;
    };

    Code:
    #include "favorite_pet.h"
    
    // default constructor
    favorite_pet::favorite_pet()
    {
        point_kitty = new Meow();
    }
    
    void favorite_pet::initialize()
    {
        // some rando value
        vec_cat_sprite_ids.push_back(0);
        vec_cat_sprite_ids.push_back(1);
        vec_cat_sprite_ids.push_back(2);
    
        // grab data from other class, that is not a parent or a friend
        vec_cat_data_input_2D = point_kitty->cat_data(vec_cat_sprite_ids);
    
        // blah blah blah
        // stuff and jumk
    
        delete point_kitty;
        point_kitty = NULL;
    }
    Code:
    #pragma once
    // meow.h
    #include <vector>
    
    class Meow
    {
    private:
    
    public:
        // constructor
        Meow();
    
        std::vector<std::vector<float>> cat_data(std::vector<int>&);
    
        std::vector<std::vector<float>> vector_cat
        {
            //
            { 0, 1, 2, 3, 4},
            { 5, 6, 7, 8, 9},
            { 10, 11, 12, 13, 14},
            { 15, 16, 17, 18, 19}
        };
    
        std::vector<std::vector<float>> vec_cat_sprite_data_output_2D;
    };

    Code:
    #include "meow.h"
    #include <vector>
    
    Meow::Meow()
    {
    
    }
    
    std::vector<std::vector<float>> Meow::cat_data(std::vector<int>& vec_id_list)
    {
        for (int var_incoming_list = 0; var_incoming_list < vec_id_list.size(); var_incoming_list++)
        {
            vec_cat_sprite_data_output_2D.emplace_back();
            for (int var_output = 0; var_output < vec_id_list.size(); var_output++)
            {
                vec_cat_sprite_data_output_2D[var_output].push_back(vector_cat[vec_id_list[var_incoming_list]][var_output]);
            }
        }
        return vec_cat_sprite_data_output_2D;
    }
    Last edited by maninthemiddle; January 31st, 2023 at 01:13 PM.

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

    Re: Returning a 2D Vector from a function through a pointer to a different class

    Please provide a minimal compilable example program that demonstrates the problem.
    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)

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,402

    Re: Returning a 2D Vector from a function through a pointer to a different class

    Quote Originally Posted by maninthemiddle View Post
    Unfortunately, using the debug features are of no help. Everything checks out until the following line...

    Code:
    vec_cat_data_input_2D = point_kitty->cat_data(vec_cat_sprite_ids);
    }
    Did you try to step in the method cat_data(vec_cat_sprite_ids) while debugging?
    Victor Nijegorodov

  4. #4
    Join Date
    Nov 2022
    Posts
    14

    Re: Returning a 2D Vector from a function through a pointer to a different class

    ****, I am sorry. I should have done that.

    Forgive me, I didn't mean to waste your time.

    Sometimes I forget that VS has indepth debugging features.

  5. #5
    Join Date
    Nov 2022
    Posts
    14

    Re: Returning a 2D Vector from a function through a pointer to a different class

    How do I mark this thread as 'solved'?

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,402

    Re: Returning a 2D Vector from a function through a pointer to a different class

    Quote Originally Posted by maninthemiddle View Post
    How do I mark this thread as 'solved'?
    Well, how did you solve it? What was the problem?
    Victor Nijegorodov

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