By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,589 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,589 IT Pros & Developers. It's quick & easy.

"could not deduce template argument" error

P: n/a
I have a problem that's driving me crazy involving Microsoft's ability
to deduce template parameters. I am using Visual Studio .NET (aka
VC7?), and it gives an error compiling the following code.

template <int x, int yclass M {
public:
template <int xRet, int yRet, int xR, int yR>
M<xRet, yRetoperator *(const M<xR, yR&b) const {
M<xRet,yReta;
return a;
}
};

int main() {
M<1,2a;
M<3,4b;
M<5,6c;
c = a * b;
return 0;
}

The error I get is

error C2783: 'M<xRet,yRetM<x,y>::operator *(const M<xR,yR&) const'
: could not deduce template argument for 'xRet'
with
[
x=1,
y=2
]

Can somebody please explain to me what is syntactically wrong with
this? I'm not looking for advice or lectures about why ints shouldn't
be used in template parameters, so please don't respond with such. I'm
just wondering what part of this C++ syntax is invalid. To me it seems
like it should be fine. Thanks.

Nov 10 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
if u look at your code .. forget about 'c=a*b' just look at 'a*b' ..
you know what x is, what y is, what xR is and what yR is ... but how is
the compiler supposedd to work out what xRet and yRet is ?

when 'a*b' takes place .. c is not in the picture .. so it can't work
it from c
I hope that helps
gr*****@comcast.net wrote:
I have a problem that's driving me crazy involving Microsoft's ability
to deduce template parameters. I am using Visual Studio .NET (aka
VC7?), and it gives an error compiling the following code.

template <int x, int yclass M {
public:
template <int xRet, int yRet, int xR, int yR>
M<xRet, yRetoperator *(const M<xR, yR&b) const {
M<xRet,yReta;
return a;
}
};

int main() {
M<1,2a;
M<3,4b;
M<5,6c;
c = a * b;
return 0;
}

The error I get is

error C2783: 'M<xRet,yRetM<x,y>::operator *(const M<xR,yR&) const'
: could not deduce template argument for 'xRet'
with
[
x=1,
y=2
]

Can somebody please explain to me what is syntactically wrong with
this? I'm not looking for advice or lectures about why ints shouldn't
be used in template parameters, so please don't respond with such. I'm
just wondering what part of this C++ syntax is invalid. To me it seems
like it should be fine. Thanks.
Nov 10 '06 #2

P: n/a
gr*****@comcast.net wrote:
...
template <int x, int yclass M {
public:
template <int xRet, int yRet, int xR, int yR>
M<xRet, yRetoperator *(const M<xR, yR&b) const {
M<xRet,yReta;
return a;
}
};

int main() {
M<1,2a;
M<3,4b;
M<5,6c;
c = a * b;
return 0;
}

The error I get is

error C2783: 'M<xRet,yRetM<x,y>::operator *(const M<xR,yR&) const'
: could not deduce template argument for 'xRet'
with
[
x=1,
y=2
]

Can somebody please explain to me what is syntactically wrong with
this?
Argument deduction for function templates is always based on the types of actual
function arguments and _only_ on these types.

In this case the arguments are 'a' and 'b'. The type of 'b' allows compiler to
deduce 'xR' and 'yR' arguments. But there's absolutely no way the compiler can
deduce 'xRet' and 'yRet' from types of 'a' and/or 'b'. That's what the compiler
is trying to tell you.

It appears that you expect the compiler to take 'c' into account. That not how
it works in C++. Template argument deduction does not take into account the
"expected" return type of the function. It works with function arguments only.

--
Bets regards,
Andrey Tarasevich
Nov 10 '06 #3

P: n/a
Andrey Tarasevich wrote:
gr*****@comcast.net wrote:
...
template <int x, int yclass M {
public:
template <int xRet, int yRet, int xR, int yR>
M<xRet, yRetoperator *(const M<xR, yR&b) const {
M<xRet,yReta;
return a;
}
};

int main() {
M<1,2a;
M<3,4b;
M<5,6c;
c = a * b;
return 0;
}

The error I get is

error C2783: 'M<xRet,yRetM<x,y>::operator *(const M<xR,yR&) const'
: could not deduce template argument for 'xRet'
with
[
x=1,
y=2
]

Can somebody please explain to me what is syntactically wrong with
this?

Argument deduction for function templates is always based on the types of actual
function arguments and _only_ on these types.

In this case the arguments are 'a' and 'b'. The type of 'b' allows compiler to
deduce 'xR' and 'yR' arguments. But there's absolutely no way the compiler can
deduce 'xRet' and 'yRet' from types of 'a' and/or 'b'. That's what the compiler
is trying to tell you.

It appears that you expect the compiler to take 'c' into account. That not how
it works in C++. Template argument deduction does not take into account the
"expected" return type of the function. It works with function arguments only.
Thanks.

I'm wondering though -- do you think I can make the compiler take "c
into account" by overloading an assignment operator? It sounds like
that might be possible, since c is an argument to operator = in this
case.

Nov 10 '06 #4

P: n/a

I don't think that will work ... its to late ....a*b doesn't get to
see c at all
gr*****@comcast.net wrote:
Andrey Tarasevich wrote:
gr*****@comcast.net wrote:
...
template <int x, int yclass M {
public:
template <int xRet, int yRet, int xR, int yR>
M<xRet, yRetoperator *(const M<xR, yR&b) const {
M<xRet,yReta;
return a;
}
};
>
int main() {
M<1,2a;
M<3,4b;
M<5,6c;
c = a * b;
return 0;
}
>
The error I get is
>
error C2783: 'M<xRet,yRetM<x,y>::operator *(const M<xR,yR&) const'
: could not deduce template argument for 'xRet'
with
[
x=1,
y=2
]
>
Can somebody please explain to me what is syntactically wrong with
this?
Argument deduction for function templates is always based on the types of actual
function arguments and _only_ on these types.

In this case the arguments are 'a' and 'b'. The type of 'b' allows compiler to
deduce 'xR' and 'yR' arguments. But there's absolutely no way the compiler can
deduce 'xRet' and 'yRet' from types of 'a' and/or 'b'. That's what the compiler
is trying to tell you.

It appears that you expect the compiler to take 'c' into account. That not how
it works in C++. Template argument deduction does not take into account the
"expected" return type of the function. It works with function arguments only.

Thanks.

I'm wondering though -- do you think I can make the compiler take "c
into account" by overloading an assignment operator? It sounds like
that might be possible, since c is an argument to operator = in this
case.
Nov 10 '06 #5

P: n/a
The other thing you should take into account is that all the template
parametres, the ints are compile time known constants, the
multiplication, how ever its defined maybe be computed at compile time.

template<int i, int j>
struct A {

enum {first = i, second =j};
};

template<class A1,class A2>
struct Mult;

template<int i1,int j1, int i2, int j2>
struct Mult< A<i1,j1, A<i2,j2 {
typedef A<i1 * i2 , j1* j2 type;
};
then to compute the result of Multiplication I do this

Mult<A<1,2>, A <2,3::type res;

res is now the correct M<i,j>

or maybe something like this.

Nind...@yahoo.co.uk wrote:
I don't think that will work ... its to late ....a*b doesn't get to
see c at all
gr*****@comcast.net wrote:
Andrey Tarasevich wrote:
gr*****@comcast.net wrote:
...
template <int x, int yclass M {
public:
template <int xRet, int yRet, int xR, int yR>
M<xRet, yRetoperator *(const M<xR, yR&b) const {
M<xRet,yReta;
return a;
}
};

int main() {
M<1,2a;
M<3,4b;
M<5,6c;
c = a * b;
return 0;
}

The error I get is

error C2783: 'M<xRet,yRetM<x,y>::operator *(const M<xR,yR&) const'
: could not deduce template argument for 'xRet'
with
[
x=1,
y=2
]

Can somebody please explain to me what is syntactically wrong with
this?
>
Argument deduction for function templates is always based on the types of actual
function arguments and _only_ on these types.
>
In this case the arguments are 'a' and 'b'. The type of 'b' allows compiler to
deduce 'xR' and 'yR' arguments. But there's absolutely no way the compiler can
deduce 'xRet' and 'yRet' from types of 'a' and/or 'b'. That's what the compiler
is trying to tell you.
>
It appears that you expect the compiler to take 'c' into account. That not how
it works in C++. Template argument deduction does not take into account the
"expected" return type of the function. It works with function arguments only.
Thanks.

I'm wondering though -- do you think I can make the compiler take "c
into account" by overloading an assignment operator? It sounds like
that might be possible, since c is an argument to operator = in this
case.
Nov 10 '06 #6

P: n/a
What EXACTLY are you trying to do ?

Nind...@yahoo.co.uk wrote:
The other thing you should take into account is that all the template
parametres, the ints are compile time known constants, the
multiplication, how ever its defined maybe be computed at compile time.

template<int i, int j>
struct A {

enum {first = i, second =j};
};

template<class A1,class A2>
struct Mult;

template<int i1,int j1, int i2, int j2>
struct Mult< A<i1,j1, A<i2,j2 {
typedef A<i1 * i2 , j1* j2 type;
};
then to compute the result of Multiplication I do this

Mult<A<1,2>, A <2,3::type res;

res is now the correct M<i,j>

or maybe something like this.

Nind...@yahoo.co.uk wrote:
I don't think that will work ... its to late ....a*b doesn't get to
see c at all
gr*****@comcast.net wrote:
Andrey Tarasevich wrote:
gr*****@comcast.net wrote:
...
template <int x, int yclass M {
public:
template <int xRet, int yRet, int xR, int yR>
M<xRet, yRetoperator *(const M<xR, yR&b) const {
M<xRet,yReta;
return a;
}
};
>
int main() {
M<1,2a;
M<3,4b;
M<5,6c;
c = a * b;
return 0;
}
>
The error I get is
>
error C2783: 'M<xRet,yRetM<x,y>::operator *(const M<xR,yR&) const'
: could not deduce template argument for 'xRet'
with
[
x=1,
y=2
]
>
Can somebody please explain to me what is syntactically wrong with
this?

Argument deduction for function templates is always based on the types of actual
function arguments and _only_ on these types.

In this case the arguments are 'a' and 'b'. The type of 'b' allows compiler to
deduce 'xR' and 'yR' arguments. But there's absolutely no way the compiler can
deduce 'xRet' and 'yRet' from types of 'a' and/or 'b'. That's what the compiler
is trying to tell you.

It appears that you expect the compiler to take 'c' into account. That not how
it works in C++. Template argument deduction does not take into account the
"expected" return type of the function. It works with function arguments only.
>
Thanks.
>
I'm wondering though -- do you think I can make the compiler take "c
into account" by overloading an assignment operator? It sounds like
that might be possible, since c is an argument to operator = in this
case.
Nov 10 '06 #7

P: n/a

Ni*****@yahoo.co.uk wrote:
What EXACTLY are you trying to do ?
I want to define a matrix type that can be sized dynamically (e.g., by
SetNumRows and SetNumCols member functions) but is limited to a maximum
size by the template parameters.

You may be wondering why, so the reasons are as follows:
1. The matrix cannot use new and delete in its implementation.
2. The dynamic sizing allows us to save memory.

By defining the appropriate assignment operator, I got it to work.

Thanks for the responses.

Nov 10 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.