C++ templates behaving as if they're not there?

StackMasher

Well-Known Member
OP
Member
Joined
Nov 29, 2016
Messages
136
Trophies
0
Age
21
XP
380
Country
So here's my code:
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);
    }
}
And here's the errors the compiler produces:
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);
                                           ^~~~~
So basically it acts as if the templates are not there before the definition, and I can't figure out why
 

bailli

Well-Known Member
Member
Joined
Oct 16, 2006
Messages
228
Trophies
1
Website
Visit site
XP
2,516
Country
Gambia, The
Hm not really sure...

Do you know that header files need to contain the full template's code and not only the forward declaration?

EDIT: Also: Maybe you are not referencing the correct namespace(s)?
 
Last edited by bailli,

bailli

Well-Known Member
Member
Joined
Oct 16, 2006
Messages
228
Trophies
1
Website
Visit site
XP
2,516
Country
Gambia, The
The first error is about not finding mbc.read - did you check that this template is accessible?

Also see my above edit. Did you check namespaces? There is an error "‘type’ is not a type" <- is it actually "GameBoy::type"?
(Namespaces sometimes confuse me - also they are generally a good idea...)
 

StackMasher

Well-Known Member
OP
Member
Joined
Nov 29, 2016
Messages
136
Trophies
0
Age
21
XP
380
Country
It's because I have a member variable called "type" in Gameboy::Cartridge aswell
I also haven't implemented mbc.read<type> and mbc.write<type> yet
Fixed now :)
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • BigOnYa @ BigOnYa:
    I'm doing good, just made my first drink of the day, so better now, Captain and Coke today. I had to cut grass all day, then get my hair cut. I messed with my wifey, she knew I was going to get hair cut, and I went and bought a purple dyed wig, and came home wearing it, she freaked out, thinking I dyed my hair purple, was funny.
    +1
  • SylverReZ @ SylverReZ:
    @BigOnYa, Hey BigOnYa
  • BigOnYa @ BigOnYa:
    Hey hope all is well
    +1
  • SylverReZ @ SylverReZ:
    @BigOnYa, And you too :)
    +1
  • S @ salazarcosplay:
    @BigOnYa how are you doing
  • S @ salazarcosplay:
    any mushroom trip adventures you can share
  • JakobAir @ JakobAir:
    Damn, I haven't been on here in while. My wife and I went on a mushroom trip like 10 years ago. A friend had a PS3TV with the glasses and some racing game. It hit when I was playing the game and I freaked out because I thought I was really driving and crashed.
  • K3Nv2 @ K3Nv2:
    Orange ocean Hawaiian punch is pretty good but @BigOnYa keeps putting vodka in it
    +1
  • Psionic Roshambo @ Psionic Roshambo:
    With enough Vodka everything is good
    +1
  • Xdqwerty @ Xdqwerty:
    Im back
  • Xdqwerty @ Xdqwerty:
    We are having a bbq
  • Sicklyboy @ Sicklyboy:
    Nice! Hopefully the weather is good today?
  • Xdqwerty @ Xdqwerty:
    @Sicklyboy, it's a bit cloudy but Yea it's good
    +1
  • K3Nv2 @ K3Nv2:
    80 for us all that rain then mother nature's like have some sun burn
  • Sicklyboy @ Sicklyboy:
    @Xdqwerty, good weather, good food, what's not to like about that? Hope you have a lovely day dude
  • K3Nv2 @ K3Nv2:
    Got some porkchops marinating in honey teriyaki hope it comes out good
    +1
  • Sonic Angel Knight @ Sonic Angel Knight:
    Porkchop was the name of that dog in that one cartoon, DOUG! :P
  • Xdqwerty @ Xdqwerty:
    @Sonic Angel Knight, never watched doug
  • Xdqwerty @ Xdqwerty:
    I might play Roblox again
  • BigOnYa @ BigOnYa:
    Nuh jump back to gdevelop
  • Xdqwerty @ Xdqwerty:
    @BigOnYa, i say "play Roblox" not "make games on Roblox"
    Xdqwerty @ Xdqwerty: https://youtu.be/wmNlFXJ_xdw?si=uB2Utv1xp9T16QRR