-1

I am aware there are multiple questions regarding this problem but none provides a solution under my constraints.

I am working on a project where a header file mips_cpu.h with a certain API is given. I am intended to develope its implementation without altering the header file. This API includes a structure declared in a file mips_cpu.cpp as:

struct mips_cpu_impl; typedef struct mips_cpu_impl *mips_cpu_h;

I have then defined this structure in mips_cpu.cpp as:

struct mips_cpu_impl{
//Program Counter
uint32_t pc;
uint32_t pcN;

//General Purpose Registers
uint32_t GPReg[32];

//Special registers for MUL / DIV instructions
uint32_t LO;
uint32_t HI;
----- more code ---

};

The problem comes when using this structure in another file mips_cpu_instruction.cpp. When I have this code:

mips_error ADDI(mips_cpu_h state, uint8_t rs, uint8_t rt, uint16_t imm){
uint64_t check = state->GPReg[rs] + imm;
uint32_t tmp = state->GPReg[rs] + imm;
          ...
     more code and appropriate return
}

state then gives the error: use of undefined type 'mips_cpu_imps'

Including the declaration of the structure in the header file solves the problem but I am not supposed to change the header files. Also the header files contain guards, which I dont fullly understand but might be relevant?

gorilon
  • 424
  • 3
  • 12
  • Is the file `mips_cpu_instruction.cpp` coded by you? If so, why would you use `mips_cpu_impl`? You probably don't need to. It's a *pimpl* for sure – Paolo M Oct 22 '15 at 16:11
  • Structure definitions don't have to be in headers. – molbdnilo Oct 22 '15 at 16:12
  • @molbdnilo You are right in saying that structure definitions don't have to be in headers. However, when a type is used in a file it must be visible ie to that file. [Struct Def](http://stackoverflow.com/questions/228684/how-to-declare-a-structure-in-a-header-that-is-to-be-used-by-multiple-files-in-c). – Mohamad Elghawi Oct 22 '15 at 16:26
  • @PaoloM yes but it needs to simulate a MIPS cpu using the given structure declaration – gorilon Oct 22 '15 at 16:30
  • Which language are you using? C or C++? – Lightness Races in Orbit Oct 22 '15 at 16:31

2 Answers2

1

The problem is that the file mips_cpu_instruction.cpp doesn't know anything about struct mips_cpu_imps, because it probably includes the .h file that you can't touch (mips_cpu.h), but certainly not the .cpp file where that struct is defined (mips_cpu.cpp). You can add the declaration to mips_cpu_instruction.cpp, but then you'd have a problem if there were other files in the project that use this struct, because you'd have to define it there as well, and when linking you'd get multiple declarations of the same struct.

The best solution would be to add it to the file that you can't modify, but since you can't modify it, you need a workaround. I would create another .h file, using include guards to protect the code from multiple inclusion, and then I would safely #include this new .h file at the top of every .cpp file that needs it. Then it would be defined everywhere, and just once.

In the end, this would mean that to use these APIs you'd have to include 2 .h files: the one you can't touch, and the new one. If you don't want to include 2 files every time, you could even decide to #include mips_cpu.h inside your new file, and then you'd just have to #include the new one, and you could pretty much forget about your unmodifiable file.

0

Your structure isn't fully defined. It's defined enough for people to use the functions, but not enough to implement the functions. I suspect this is homework and your assignment is to implement these functions, including this structure.

To allow you to get past the programming issue without helping you do your homework directly, put:

struct mips_cpu_impl
{
    unsigned GPReg[1];
};

at the top of your mips_cpu_instruction.cpp file, and you'll see these compiler errors go away. You'll need to properly size the array and add whatever other state things you need to model the MIPS core.

p.s. TAs exist for a reason.

Russ Schultz
  • 2,545
  • 20
  • 22
  • I get that far, I have obviously already done so, Ill update the question to make it clear, – gorilon Oct 22 '15 at 16:20
  • you can either copy/paste your exact structure definition into the other file, or (preferably) move your structure definition to something like "my_mips_internal.h" and include that in both implementation files to ensure they're always in lock/step. – Russ Schultz Oct 22 '15 at 16:25