Vector is empty after passing struct containing vector to method.

bryon 101 Reputation points
2023-01-16T05:00:46.6266667+00:00

Hello All,

I am trying to pass a struct that contains a vector into a function to perform some operations, however, when the struct is passed in, the vector is empty of the previously assigned data.

Here is the idea:

struct SomeOtherStruct
{
  int z;
  int c;
  CByteArray arr;
  //...
  //Some more implementation
  //...
}

struct SomeStruct 
{
 int x;
 int y;
 CString someString;
 std::vector<SomeOtherStruct> someOtherStruct = {};
 CByteArray Arr;
 SomeStruct ()
 {
  x = 0;
  y = 0;
  someString = "";
 }

 SomeStruct(const SomeStruct& s)
 {
  x = s.x;
  y = x.y;
  someString = s.someString;
  someOtherStruct = std::vector(s.someOtherVector.begin(), s.someOtherVector.end()); Arr.Copy(s.arr);
 }
}

I have a struct that contains a few primitive types, and a vector that is to contain a list of items in the form of another struct.

The problem occurs when I call a function in the class that is to operate on the struct., and when I get down into it, the vector that was populated with data before going into the procedure no longer

//Include files...
//Declare class
class MyClass
{
 void Exectute(SomeStruct s)
 {
   //...
   //Do work
   //...
 }
}

//Fill record with data, and pass to Execute procuecure.
SomeOtherStruct s;
s.z = 10;
s.c = 1;
SomeStruct somestruct;
somestruct.x = 11;
somestruct.y = 12;
somestruct.someString = "test";
somestruct.push_back(s);

MyClass myclass;
myclass.Execute(s); // <---At this point the data is intact. After making the call, the copy constructor is called as expected, and gets all the data, except the vector is empty inside the method Execute.


Any ideas? My C++ is rusty, and I am out of ideas here.

This is on an MFC app by the way. The odd thing is that I have seen similar code work just fine. Could this be a project setting?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,603 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,527 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. YujianYao-MSFT 4,271 Reputation points Microsoft Vendor
    2023-01-16T06:26:33.6633333+00:00

    Hi @bryon,

    There are several errors in your code, the code can run after modification, you could refer to the following code.

    struct SomeOtherStruct
    {
    	int z;
    	int c;
    	CByteArray arr;
    	//...
    	//Some more implementation
    	//...
    };
    
    struct SomeStruct
    {
    	int x;
    	int y;
    	CString someString;
    	std::vector<SomeOtherStruct> someOtherVector = {};
    	CByteArray Arr;
    	SomeStruct()
    	{
    		x = 0;
    		y = 0;
    		someString = "";
    	}
    
    	SomeStruct(const SomeStruct& s)
    	{
    		x = s.x;
    		y = s.y;
    		someString = s.someString;
    		someOtherVector = s.someOtherVector;
    		Arr.Copy(s.Arr);
    	}
    };
    class MyClass
    {
    public:void Exectute(SomeStruct s)
    	{
    		//...
    		//Do work
    		//...
    	}
    };
    
    //Fill record with data, and pass to Execute procuecure.;
    int main()
    {
    	SomeOtherStruct s;
    	s.z = 10;
    	s.c = 1;
    	SomeStruct somestruct;
    	somestruct.x = 11;
    	somestruct.y = 12;
    	somestruct.someString = "test";
    	somestruct.someOtherVector.push_back(s);
    
    	MyClass myclass;
    	myclass.Exectute(somestruct);
    }
    

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. RLWA32 40,286 Reputation points
    2023-01-16T10:45:35.7633333+00:00

    The code posted by the questioner suffers from the same problem as was mentioned in the earlier question https://learn.microsoft.com/en-us/answers/questions/1155002/(declared-implicitly)-cannot-be-referenced-it-is-a because MFC's CByteArray class does not have an accessible copy constructor.

    Adding a copy constructor to the SomeOtherStruct structure requires that a default constructor is also provided. An assignment operator was added for good measure.

    For example,

    struct SomeOtherStruct
    {
        int z;
        int c;
        CByteArray arr;
    
        SomeOtherStruct() = default;
    
        SomeOtherStruct(const SomeOtherStruct& rhs)
        {
            z = rhs.z;
            c = rhs.c;
            arr.Copy(rhs.arr);
        }
    
        SomeOtherStruct& operator=(const SomeOtherStruct& rhs)
        {
            if (this != &rhs)
            {
                z = rhs.z;
                c = rhs.c;
                arr.Copy(rhs.arr);
            }
    
            return *this;
        }
    
        //...
        //Some more implementation
        //...
    };
    
    0 comments No comments