2

When I compile the following templated C++ code with GCC 4.8.3

template <typename dtype> class Base {
public:
    dtype base;
    dtype ceiling;
    Base() { };
    virtual ~Base() { };
};

template<typename dtype> class Building : public Base<dtype> {
public:
    dtype wall;
    Building(dtype concrete) { 
        Base<dtype>::base=concrete;
        ceiling=concrete; 
        wall=concrete;
    };

    ~Building() { };
};

int main (int argc, char* argv[]) {

    Building<float>* building=new Building<float>(2.0);

    std::cout << building->base << std::endl;
}

I get the error

error: ‘ceiling’ was not declared in this scope
ceiling=concrete; 

So it appears that

Base<dtype>::base=concrete;

works, but

ceiling=concrete;

does not. Is there any way I can mogrify this templated code so that, in the derived class constructor I can just reference "ceiling" from the templated base class without having to clarify which class it is from?

Thanks in advance

Toby
  • 21
  • 1

1 Answers1

1

You can use this->ceiling.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – RiggsFolly Jan 19 '15 at 09:18
  • @RiggsFolly The question (the only question in the entire post) was: "Is there any way [...] I can just reference `ceiling` [...] without having to clarify which class it is from?" My answer is to use `this->ceiling`, which works, is the correct way to do it, and exactly answers the author's question. I could go on about this not being the write way to do it (because `Base` should have constructor parameters and do the initializing in its own constructor), but I answered the question asked. – Sebastian Redl Jan 19 '15 at 10:03