0

I am porting some legacy code from MS visual studio to Clang and run into a problem with protected data members. In short, my issue is this:

template<typename T>
class Base : public SuperBase<T> {
public:
  Base(std::shared_ptr<Widget<T>> const& sb) : sb_(sb) {}

protected:
  std::shared_ptr<Widget<T>> sb_;
}

template <typename T>
class Derived : public Base<T>
{
public:
  Derived(std::shared_ptr<Widget<T>> const& sb) : Base<T>(sb) {}

  double method(void) const { return sb_->number(); }
}

This compiles fine under MSVC++, but not under Clang. Clang complains:

use of undeclared indentifier sb_.

Following Meyers Effective C++ I don't use protected a lot and can rewrite the code not to use, but I am still wondering why Clang complains here as the Derived class should be able to see the protected members of the Base class. What am I missing?

Mike
  • 3,775
  • 8
  • 39
  • 79
  • 1
    Can you please post the whole error message, or at least part of it? I suspect that the actual problem is that `sb_` does not have a function called `number`. – Rakete1111 May 29 '17 at 14:39
  • 1
    Shoud not be `sb_->number();`? – Amadeus May 29 '17 at 14:40
  • 1
    MCVE...........? – Karoly Horvath May 29 '17 at 14:43
  • I cant get this to compile in MSVC++ either – sp2danny May 29 '17 at 14:51
  • 2
    This code has many issues, but the *undeclared indentifier `sb_`* is dealt with in the dupe. – Walter May 29 '17 at 14:55
  • While MSVC had many issues, confusing `.` and `->` is not one I remember. I don't think this code ever compiled on MSVC, _even though it should_ ! The reason is that the template is not instantiated, and `sb_.number` depends on `T`. I.e. it should be looked up only in the seconds phase of two-phase name lookup. One of those MSVC bugs is that it doesn't have two-phase name lookup. I think it failed early on code like this. – MSalters May 29 '17 at 15:00

2 Answers2

2

Try this->sb_->number().

When should I make explicit use of the `this` pointer?

Kane
  • 5,595
  • 1
  • 18
  • 27
0

unique_ptrs are not copyable, you are trying to copy them in the constructor of Base that should be the first error you try and fix, everything after that might just get fixed automatically if you look at the very first error you get in the clang output and fix that

Further as pointed out in the comments, sb_ is a pointer and not a reference, you should use the -> operator on it, and not . so change sb_->number() to sb_->number()

Curious
  • 20,870
  • 8
  • 61
  • 146