I want to provide an subscript operator structure for the struct I am writing. I plan to do this with two struct
s foo and bar. Code is as follows:
#include <iostream>
struct foo;
struct bar{
uint32_t *mem;
uint32_t *opcode;
bar():mem(nullptr),opcode(nullptr){}
bar( foo *f, int index ){
this->mem = f->memory + (index%16);
this->opcode = f->instructions +(index%16);
}
operator bool(){ return (this->mem != nullptr) & (this->opcode != nullptr); }
};
std::ostream& operator<<( std::ostream& os, bar &t ){
if( t ){
return os << "bar:" << (*t.mem) << "\t-\t" << (*t.opcode);
}else{
return os << "bar: NOT INITIALIZED";
}
}
struct foo{
uint32_t *memory = new uint32_t[16]();
uint32_t *instructions = new uint32_t[16]();
foo(){}
~foo(){
delete[] this->memory;
delete[] this->instructions;
}
bar &operator[]( int index){
return bar( *this, index%16 );
}
};
std::ostream& operator<<( std::ostream& os, foo &f ){
for( int i =0 ; i < 16; i++ ){
os << f.memory[i] << "\t" << f.instructions[i] << "\n";
}
return os;
}
I am using CygWin[x86_64] and Notepad++ as main compiler and editor on Windows 7 64 bit.
I have tried a lot of permutations on my own to fix this problem, but I would like to show the following:
bar( foo *f, int index ){
this->mem = f->memory + (index%16);
this->opcode = f->instructions +(index%16);
}
and
bar( foo *f, int index ){
this->mem = f->memory[index%16];
this->opcode = f->memory[index%16];
}
'f' is incomplete type error, with note that I have used forward declaration.
bar( foo *f, int index ){
this->mem = f->memory[index%16];
this->opcode = f->memory[index%16];
}
two forward declarations notes, and two invalid use of incomplete type
struct foo
onthis->mem = f->memory[index%16]
andthis->opcode = f->memory[index%16];
I have tried a bunch of other stuff but it seems I have mostly an issue with incomplete type
. I have searched SO for answers, and one did explain what is incomplete type, other issue was about recursive definition and this one doesn't define how to make an incomplete type complete.
I am hung on this for past several days, going trough iterations for simple operator overloading. Maybe I am phrasing it wrong in questions, or searching for wrong answers.
But can someone point out my mistakes and/or write how to overload array subscript operator with code and not just body less functions?