'[': is not a member of 'GwDynArray

My compiler says that at this point is an error:

1
2
3
4
T_& operator[](int nindex)
	{
		return *(T_*)&(GwDynArray::operator[](nindex));
	}


This is the whole block:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
template <class T_>
class GwDynTArray : public GwDynArray
{
public:
	GwDynTArray(int size = GwDA_DEFAULT_INCR, int incr = GwDA_DEFAULT_INCR)
		: GwDynArray(size, sizeof(T_), incr)
	{
	}

	GwDynTArray(const GwDynTArray<T_>& orig)
		: GwDynArray((const GwDynArray& )orig)
	{
	}

	~GwDynTArray()
	{
	}

	GwDynTArray<T_>& operator =(const GwDynTArray<T_>& orig)
	{
		return (GwDynTArray<T_>&)GwDynArray::operator =((const GwDynArray &)orig);
	}

	T_& operator[](int nindex)
	{
		return *(T_*)&(GwDynArray::operator[](nindex));
	}
	T_ operator[](int nindex) const
	{
		if (nindex == -1)
			return T_();

		return	*(T_ *)&(((GwDynTArray<T_> *)this)->access(nindex) );
	}
};


And this is the place where operator is defined:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class EXPORT GwDynArray {

	enum
	{
		ok = 0,
		overflow = 1,
		out_of_mem = 2
	};

	static void *far da_invalid;

	int da_elementsize; 		// size of one object
	int da_incr;				// increment for reallocation

protected:
	int da_count;				// number of objects
	int da_size;				// number of allocated object slots
	int da_state;				// state of the array
	char* da_list;

	virtual void resize(int newsize);
	char*& access(int nindex);// NOT virtual, derived classes may overload it

public:
	GwDynArray(int size, int elementsize = sizeof(char*),
			int incr = GwDA_DEFAULT_INCR);
	virtual ~GwDynArray();

	GwDynArray(const GwDynArray& orig);
	GwDynArray& operator =(const GwDynArray& orig);

	int size() const					{ return da_count; }
	char*& operator[](int nindex)		{ return access(nindex); }
	const char *operator[](int nindex) const
	{ return nindex == -1 ? 0 : (char *)((GwDynArray *)this)->access(nindex); }

	int position(const char* str) const;

	GwBoolean is_valid() const			{ return !da_state; }
	int state() const					{ return da_state; }
};


I was working with Visual Studio 2012 and migrated to 2019. Thats why some parts of the code broke.
Last edited on
I'm not using VS 2019, but rather MinGW. However, I don't get the same error as you.
You shouldn't have to worry about far pointers.

I had to do a few things to get that to compile:
1.
1
2
3
#define EXPORT __declspec(dllexport)
const int GwDA_DEFAULT_INCR = 1;
using GwBoolean = bool;
(you probably don't need to do (1.), I'm just letting you know what I did)

2. change
static void *far da_invalid;
to just
static void * da_invalid;

3. Define the base class before the derived class.

4. I also defined some dummy functions that you have only declared to make the compiler happy

I would suggest making a minimal, compileable reproducible example for your issue.
http://sscce.org/
https://stackoverflow.com/help/minimal-reproducible-example

Then again, maybe someone else is more familiar with legacy C++ code.
What I will say is that those are kinda some ugly casts. You're taking the address of a (char*), then casting it to some arbitrary (T*) type, then dereferencing it. I mean, that might work, but I would assume it's undefined behavior.
Last edited on
I mean, that might work, but I would assume it's undefined behavior.

No, its well defined, iff they bytes correctly define the target type. But its kinda old school/ Cish.

char h[] = "Hello";
uint64_t * i = (uint64_t*)h;
cout << *i; //some numeric value depending on endian and whatever


Last edited on
Yep, thanks. Your example seems okay based on the "type aliasing" rules in https://en.cppreference.com/w/cpp/language/reinterpret_cast
My compiler says that at this point is an error:
And the exact error(s) VS 2019 is spitting up is?

VS 2019 can make pre-C++11 code go nuts, the compiler defaults to C++14.
Is the access to operator correct? I commented the two operator functions GwDynArray and it shows me still the same error. My assumption is that the call is wrong. Do I have to call it in an other way because the class is exported?

EDIT: I have changed it to this line: return *(T_*)&(GwDynArray::operator=((nindex)));
And the error disappeared.
Last edited on
Topic archived. No new replies allowed.