1

I need a child class to initialize but the debugger throws an error that the parent class static variables are undefined. It seems to work in every other scenario I can find. I can only imagine it is something small or obvious that I just can't seem to find. The program is lengthy I shortened it as much as I could think to to put it on here.

//events.h

#include<vector>

#ifndef EVENTS_H
#define EVENTS_H

class Events
{
public:

    static char const ALPHA_TEAM='@';
    static char const BRAVO_TEAM='&';
    static char const CHARLIE_TEAM='$';
    static char const EMPTY=' ';

    char * pAlpha=const_cast<char *>(&ALPHA_TEAM);
    char * pEmpty=const_cast<char *>(&EMPTY);
    //all static const char have these pointers just shortened the code

    Events(int s) : pegs (s, pEmpty){}//<~~ is where the undefined errors in debug
    Events(std::vector<char *> p);//, std::string win) : pegs(p), winner(win){}

protected:
    bool polymorph();
    std::vector<char *> pegs;// for some reason I can't make this private anymore

};

#endif


/events.cpp

#include "events.h"

bool Events::polymorph()
{
    return false;
}
 


//triatholon.h
#include "events.h"

#ifndef TRIATHOLON_H
#define TRIATHOLON_H

class Triatholon : public Events
{
public:
    Triatholon() : Events(3){};
    Triatholon(std::vector<char*> p);//, std::string winner) : Events(p, winner){}

private:
    bool polymorph();//just used to show where I believe the problem lies

};
#endif

//triatholon.cpp
#include "triatholon.h"


bool Triatholon::polymorph()
{
    if(*pegs[0] == EMPTY){
        return true;
    }
    else if(*pegs[0] == ALPHA_TEAM){
        return true;
    }
    return false;

}

I don't know if you need the manager that helps but I wanted to include all code to help get it to run

//event_coordinator.h

#include "events.h"
#include "triatholon.h"
#include<vector>
#include<string>
#include<iostream>
#include<utility>
#include<memory>

#ifndef EVENT_COORDINATOR_H
#define EVENT_COORDINATOR_H

using std::string;
using std::vector;

class EventCoordinator
{
public:

    void run();

private:

    std::unique_ptr<Events> event;
    vector<std::unique_ptr<Events>> games;

    void select_event();

};

#endif


//event_coordinator.cpp

#include "event_coordinator.h"
#include<iostream>

using std::cout;
using std::cin;

void EventCoordinator::run() {
    cout << "Welcome to the Event Coordinator Application!!\n";
    select_event();
    if(event) {
        cout << "It worked!!";
    }
    cout << "Goodbye!";

}

void EventCoordinator::select_event() {

    event = std::make_unique<Triatholon>();

};


//main.cpp
#include "event_coordinator.h"
#include <iostream>
int main()
{
    EventCoordinator manager;
    manager.run();
}

Errors are:

/events.h:19: undefined reference to Events::ALPHA_TEAM
/events.h:19: undefined reference to Events::EMPTY

Static const variable is not constant in child class is the closest thing I have found so far to what I believe my problem is. However they are able to get Parent::A to work. So in a way it's the complete opposite. They actually have an undefined where mine are defined..........which is the same for all the examples I have found so far.

Thank you again for any help!

The now linked answer does in fact answer the question as pax was saying define the static const in the cpp not in the header but initiate in the header

//events.h

#include<vector>

#ifndef EVENTS_H
#define EVENTS_H



class Events
{
public:

    static char const ALPHA_TEAM;
    static char const BRAVO_TEAM='&';
    static char const CHARLIE_TEAM='$';
    static char const EMPTY;

    char * pAlpha=const_cast<char *>(&ALPHA_TEAM);
    char * pEmpty=const_cast<char *>(&EMPTY);
    //all static const char have these pointers just shortened the code

    Events(int s) : pegs (s, pEmpty){}//<~~ is where the undefined errors in debug
    Events(std::vector<char *> p);//, std::string win) : pegs(p), winner(win){}

protected:
    virtual bool polymorph();
    std::vector<char *> pegs;// for some reason I can't make this private anymore

};



#endif

//events.cpp

#include "events.h"

char const Events::ALPHA_TEAM = '@';
char const Events::EMPTY = '@';

bool Events::polymorph()
{
    return false;
}

Thank you sooo much!!!

Roger
  • 107
  • 6
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/224882/discussion-on-question-by-roger-why-is-c-class-inheritance-static-const-class). – Samuel Liew Nov 21 '20 at 07:21

0 Answers0