So here's my code:
And here's the errors the compiler produces:
So basically it acts as if the templates are not there before the definition, and I can't figure out why
Code:
//cartridge.h
#ifndef CARTRIDGE_INCLUDED
#define CARTRIDGE_INCLUDED
#include <cstdint>
#include <cstddef>
#include "type.h"
#include "../types.h"
#include "mbc.h"
#include "../util/smartArray.h"
namespace Gameboy {
class Cartridge {
public:
constexpr static Address header = 0x100;
constexpr static unsigned int romBankSize = 0x4000;
constexpr static unsigned int ramBankSize = 0x2000;
protected:
Util::SmartArray<Byte*> romBanks;
Util::SmartArray<Byte*> ramBanks;
unsigned int romBank0 = 0;
unsigned int romBank1 = 1;
unsigned int ramBank = 0;
int romSize = 0;
int ramSize = 0;
MBC mbc;
const char* name = NULL;
bool nameCopied = false;
CartType type;
public:
void clear();
void load(const char* romname);
void update();
template <typename type> type read(Address address);
template <typename type> void write(Address address, type toWrite);
const char* getName();
Cartridge(const char* romname = NULL);
~Cartridge();
};
}
#include "cartridgeIO.h"
#endif
//cartridgeIO.h
template <typename type> type Gameboy::Cartridge::read(Address address) {
if (address <= 0x3FFF) {
return *((type*)romBanks[romBank0][address]);
}
else if (address <= 0x7FFF) {
return *((type*)romBanks[romBank1][address-0x3FFF]);
}
else if (address >= 0xA000 && address <= 0xBFFF && mbc.getType()!=2) {
return *((type*)ramBanks[ramBank][address-0x9FFF]);
}
else {
return mbc.read<type>(address);
}
}
template <typename type> void Gameboy::Cartridge::write(Address address, type toWrite) {
if (address >= 0xA000 && address <= 0xBFFF && mbc.getType()!=2) {
*((type*)&ramBanks[ramBank][address-0x9FFF]) = toWrite;
}
else {
mbc.write<type>(address, toWrite);
}
}
Code:
g++-6 -g -c gb.cpp -o .build/gb.o
In file included from cartridge/cartridge.h:48:0,
from gb.h:6,
from gb.cpp:1:
cartridge/cartridgeIO.h: In member function ‘type Gameboy::Cartridge::read(Gameboy::Address)’:
cartridge/cartridgeIO.h:12:29: error: expected primary-expression before ‘>’ token
return mbc.read<type>(address);
^
cartridge/cartridgeIO.h: At global scope:
cartridge/cartridgeIO.h:16:74: error: ‘type’ is not a type
template <typename type> void Gameboy::Cartridge::write(Address address, type toWrite) {
^~~~
cartridge/cartridgeIO.h:16:31: error: prototype for ‘void Gameboy::Cartridge::write(Gameboy::Address, int)’ does not match any in class ‘Gameboy::Cartridge’
template <typename type> void Gameboy::Cartridge::write(Address address, type toWrite) {
^~~~~~~
In file included from gb.h:6:0,
from gb.cpp:1:
cartridge/cartridge.h:40:43: error: candidate is: template<class type> void Gameboy::Cartridge::write(Gameboy::Address, type)
template <typename type> void write(Address address, type toWrite);
^~~~~