Search code examples
compiler-errorsc++14shared-ptrstdlist

Compiler error related to expression involving list of shared_ptr


I have two template declarations as follows:

template<typename T>
class A{
public:
T& getData();
//rest of public
private:
T& data;
//rest of private
};

template<typename T1, typename T2>
class B{
public:
using typAContainter = class std::list<shared_ptr<A<T1>>>;
B(int, T2&);
//rest of public
private:
typeAContainer C;
//rest of private
};

Inside the constructor definition for B, I use the following expression (for debugging):

template<typename T1, typename T2> B<T1, T2>::B(int I, data& rData){

  #ifdef DEBUG
  auto iter = C.begin();
  std::cout << (*(iter).get())->getData() << std::endl;
  #endif

}

For the expression (*(iter).get())->getData() I get the following compilation error:

error: no member named 'get' in 'std::__1::                   
__list_iterator<std::__1::shared_ptr<A<std::__1::basic_string<char> > >,     void *>'

My understanding is that *iter should give me an expression of type shared_ptr<A<T1>>. Invoking the get() method on the same should fetch me an expression of type A<T1>* (Pointer to A<T1>), upon which I try to invoke the getData() method using the -> operator. I have also paid attention to the operator precedence in constructing the above expression.

Obviously, there is some flaw in my understanding. I would appreciate if some one could point this out.

PS


Solution

  • I think you misread some things about operator precedence and misplaced the parentheses in *(iter).

    The expression *(iter).get() is equal to *((iter).get()) (this in turn is equal to *(iter.get())). That is, you attempt to use the . member access operator on the iterator and then dereference what get() returns. Neither of that will normally work.

    Either do iter->get() or (*iter).get().