1 Replies - 691 Views - Last Post: 11 January 2015 - 10:59 AM Rate Topic: -----

#1 knils   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 109
  • Joined: 20-June 13

default argument for parameter of type ‘bar<int>&’ has typ

Posted 10 January 2015 - 05:23 PM

#include <iostream>
using namespace std;

template <typename T>
class bar
{
 public:
 bar(){cout << "bar" <<"\n";}
};

template <typename T, typename ctor = bar<T>>
class foo
{
 T i;
public:
 explicit foo(ctor& c = ctor());
private:
 ctor mctor;


};

template <typename T, typename ctor>
foo<T,ctor>::foo(ctor& c):mctor(c)
{
 cout << "foo" <<"\n";
}

int main()
{
 foo<int> f;
 return 0;
}





while compiling this code I am getting error:

ctor_call.cpp: In constructor ‘foo<T, ctor>::foo(ctor&) [with T = int; ctor = bar<int>]’:
ctor_call.cpp:31:11: error: default argument for parameter of type ‘bar<int>&’ has type ‘bar<int>’

I want to compile the code pls help me with this.

Thanks,

Is This A Good Question/Topic? 0
  • +

Replies To: default argument for parameter of type ‘bar<int>&’ has typ

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2768
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: default argument for parameter of type ‘bar<int>&’ has typ

Posted 11 January 2015 - 10:59 AM

You can't assign a temporary to a non-const reference. What do you expect to happen when you assign to the reference in that case?

You need to either make the reference const, not make it a reference at all or use an lvalue as the default value.

This post has been edited by sepp2k: 11 January 2015 - 10:59 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1