0

In my Clock.h I have

class ClockConf {
public:
static int SCREEN_WIDTH;
static int SCREEN_HEIGHT;

In my ClockConf.cpp I have

#include "Clock.h"

void ClockConf::init(string conf_name) {
    ClockConf::SCREEN_WIDTH = 1024;
    ClockConf::SCREEN_HEIGHT = 768;

In my ClockRender.cpp I have

#include "Clock.h"

bool ClockRender::init() {
    gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED, ClockConf::SCREEN_WIDTH,
    ClockConf::SCREEN_HEIGHT, SDL_WINDOW_SHOWN);

In my main.cpp I have

#include "Clock.h"

ClockConf clockConf= ClockConf();
clockConf.init();
ClockRender cr = ClockRender();
cr.init();

But when build, I got undefined reference to ClockConf::SCREEN_WIDTH and ClockConf::SCREEN_HEIGHT at both ClockConf and ClockRender. Please help.

EDIT: In fact I've read the two dupe links before posting, but didn't recognise they are answering my question because I am from java and new to c++ (2 weeks). I've even tried their methods in my code but because mistakes here and there made me further confused and considered them not answering. However, my question made me understand a few things about c++ (below). These met the purpose of my project: to learn. Here are where I've acquired from it:

  1. Unlike java, c++ is a single pass compiler. So you have to either declare or model everything before using it. My code in header file is just a model, not declaration. With it, I still have to declare it somewhere.
  2. All those header files, includes and cpp coding are conceptually a "single" coding to the compiler. So duplicated names added confusions when debugging. Adding qualifier like ClockConf:: helps but then you code will be very difficult to read.

Hope these could help those with 1 week experience in c++.

senderj
  • 400
  • 1
  • 9

0 Answers0