2

Why do I get this error:

test.cpp:11:28: error: no match for ‘operator=’ in ‘*(((Test*)this)->Test::a_list + ((unsigned int)(((unsigned int)i) * 20u))) = Test::foo2()’

When I compile the below code (via g++ test.cpp -o test)

test.cpp:

#include "test.h"

Test::Test () {}

void Test::foo1 ()
{
   int i;
   a_list = ( A* ) malloc ( 10 * sizeof ( A ) ); 

   for ( i = 0; i < 10; i++ )
      a_list [ i ] = foo2 ();
   }
}

A* Test::foo2 ()
{
   A *a;

   a = ( A* ) malloc ( sizeof ( A ) ); 

   return a;
}

Test.h:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

typedef struct
{
   double x;
   double y;
   string z;
} A;

class Test
{
   public:
      Test ();
      void foo1 ();
   private:
      A* foo2 ();
      A *a_list;
};
puk
  • 16,318
  • 29
  • 119
  • 199

1 Answers1

3
a_list [ i ] = foo2 ();

foo2() returns a pointer to A, but a_list[i] is an object of type A.

Also, it would be better if you used new to allocate dynamic memory instead of malloc.

See What is the difference between "new" and "malloc" and "calloc" in C++?

Instead of:

a_list = ( A* ) malloc ( 10 * sizeof ( A ) ); 

You can have:

a_list = new A[10];

And for deallocating memory, use

delete [] a_list; 

An even better option is to use std::vector<A> . In this you do not have to manage memory allocations, de-allocations yourself as these are done automatically.

EDIT 2:

When you call new A[10], then 10 objects of struct A are created dynamically on the heap and their constructors are called.

If you do not want to 'construct' 10 objects at this time, then I would suggest you use std::vector<A>.

You can just push_back( object_of_type_A ) to the vector as you create it.

http://en.cppreference.com/w/cpp/container/vector

Community
  • 1
  • 1
Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
  • Can I just swap `new` in for `malloc` =P Can you please demonstrate a small script that would do this for me? – puk Dec 13 '13 at 06:28
  • I was not aware that `new` worked with structs as well – puk Dec 13 '13 at 06:36
  • @puk - The only difference between struct and class is the default visibility: public for structs, private for classes. – David Hammen Dec 13 '13 at 06:38
  • @puk In C++, struct and class are identical, the only difference being that in struct, if you do not mention any access type, then the members are public by default, whereas in a class if you do not mention any access type, the members are private by default. – Abhishek Bansal Dec 13 '13 at 06:38
  • Wait I'm confused again, when you call `a_list = new A[10]` that initializes all the entries, when all I want to do is allocate space at the moment. – puk Dec 13 '13 at 06:39
  • Mother of god, this is so complex. How about creating a default constructor, creating the array via `new A[10]` then going around and reinitializing everything, but this time with the 3 parameter constructor? I ask, because I am reading values from a file and creating new objects based on those values – puk Dec 13 '13 at 06:51
  • @puk Ofcourse you can do that, except that the second 'constructor' that you call shall not be technically called a constructor, it will be a member function that changes the values of the variables of the object to new values. – Abhishek Bansal Dec 13 '13 at 06:54
  • Actually, you may very well call the constructor again to create a single temporary `A` object from those 3 parameters, and then assign this single object to the array: `array[5] = A{p1, p2, p3}`. – MSalters Dec 13 '13 at 10:35