Static data member giving undefined reference errors.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
goku420
Member
Member
Posts: 51
Joined: Wed Jul 10, 2013 9:11 am

Static data member giving undefined reference errors.

Post by goku420 »

I am using the constructor/destructor code from Trion. Constructors are working, however when I attempt to use a static data member, it is giving me an undefined reference error. I don't think it's covered in the C++ wiki page. Note that I use constexpr so that I can use the array in an enum class and initialize it inside the class. However, the error persists if I don't use constexpr and initialize the elements manually.

Terminal.hpp

Code: Select all

class Terminal
{
	constexpr static uint32_t col_map[16] = {
		0x000000, 0x0000AA, 0x00AA00, 0x00AAAA,
		0xAA0000, 0xAA00AA, 0xAA5500, 0xAAAAAA,
		0x555555, 0x5555FF, 0x55FF55, 0x55FFFF,
		0xFF5555, 0xFF55FF, 0xFFFF55, 0xFFFFFF
	};
	
	enum class Color : uint32_t
	{
		Black = col_map[0],
		White = col_map[15]
	};
        // ...
};

static Terminal terminal;
Terminal.cpp

Code: Select all

void Terminal::drawcolormap()
{
// ...
     putpixel(n + i * 50, m + j * 40, col_map[col]);
// ...
Is there something extra that I have to do for static data members?

Notes:
  • I have tried to reproduce the undefined reference error in a test program using a regular compiler unsuccessfully.
  • Don't pay attention to "static Terminal terminal;" it seems unrelated to the error.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Static data member giving undefined reference errors.

Post by xenos »

You will need something like this in Terminal.cpp (either with or without constexpr, I'm not sure right now):

Code: Select all

constexpr uint32_t Terminal::col_map[16];
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
goku420
Member
Member
Posts: 51
Joined: Wed Jul 10, 2013 9:11 am

Re: Static data member giving undefined reference errors.

Post by goku420 »

XenOS wrote:You will need something like this in Terminal.cpp (either with or without constexpr, I'm not sure right now):

Code: Select all

constexpr uint32_t Terminal::col_map[16];
Thank you, that did it. Can you explain why?

Edit: Nevermind, I see now that it has to be defined outside the class as well as initialized inside the class.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Static data member giving undefined reference errors.

Post by xenos »

Basically, the declaration of the static data member does not reserve any space for it in any object file. For this to happen you need to define it in exactly one source file, so that it will be included in the corresponding object file and can be found by the linker.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
Post Reply