473,387 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C2666 error with functions/c'tors taking variable parameters

Hello, all.
The following code results in a C2666 error (2 overloads have similar
conversions).

class FSVec2D
{
public:
FSVec2D()
{ // code omitted
}
FSVec2D(double first, ...)
{
va_list args;
va_start(args, first);
double d = first;
for (int i = 0; i < 2; ++i)
{
_data[i] = d;
d = va_arg(args, double);
}
va_end(args);
}
FSVec2D(const FSVec2D& _r, double _w)
{
_data[0] = _r[0];
_data[1] = _w;
}
double& operator[](int i)
{ return _data[i]; }
const double& operator[](int i) const
{ return _data[i]; }
private:
double _data[2];
};

FSVec2D v2(1.1, 2.2); // Error here

The two overloads that it sties are the default, compiler-generated
copy constructor and the constructor that takes a FSVec2D reference
and a double as parameters. Apparently, the intended constructor with
the variable parameters isn't even considered. This appears to me to
be a compiler bug. How can it convert a double to a class reference?
Am I making an erroneous assumption?

BTW, this code is a simplification of the original code. The
constructor above taking the reference and a double, of course,
doesn't make much sense. The original class is a parameterized class
as follows (notice the reference to a vector one size smaller). I
guess I could write an explicit specialization of the class for _Size
of 2 and eliminate the last constructor, since I have no use for a
vector of size 1. However, correct me if I'm wrong, I would have to
duplicate all of the rest of the functions in the class. Right?

template <size_t _Size, class _Type>
class FSVector
{
// Fixed-size vector
public:
FSVector();
FSVector(_Type first, ...);
FSVector(const FSVector<_Size - 1, _Type>& _r, _Type _w);
// ...
private:
_Type _data[_Size];
};

Other observations:
This error doesn't just happen with constructors. It also happens with
member functions and static member functions if one has the variable
parameter list and the other has the reference and primitive
parameters, as long as the reference is to a 'similar' class. By that
I mean, for the simplified sample (non-template) class above, it has
to be the same class. In the template class, the class reference isn't
to the exact same class, but uses the same template.

Interestingly, if I supply a copy-constructor (or in the case of
member functions), the compiler says there are 2 overloads that have
similar conversions, but only provides reference to the one with the
non-varible parameters. That is, it says "could be 'blah blah'" but
omits the "or 'blah blah blah'".

Thanks for any help or comments.

-John
Jan 18 '07 #1
3 2079
On Thu, 18 Jan 2007 14:55:05 -0500, John Shell <no****@forme.comwrote:
>Hello, all.
The following code results in a C2666 error (2 overloads have similar
conversions).

class FSVec2D
{
public:
FSVec2D()
{ // code omitted
}
FSVec2D(double first, ...)
{
va_list args;
va_start(args, first);
double d = first;
for (int i = 0; i < 2; ++i)
{
_data[i] = d;
d = va_arg(args, double);
}
va_end(args);
}
FSVec2D(const FSVec2D& _r, double _w)
{
_data[0] = _r[0];
_data[1] = _w;
}
double& operator[](int i)
{ return _data[i]; }
const double& operator[](int i) const
{ return _data[i]; }
private:
double _data[2];
};

FSVec2D v2(1.1, 2.2); // Error here

The two overloads that it sties are the default, compiler-generated
copy constructor and the constructor that takes a FSVec2D reference
and a double as parameters. Apparently, the intended constructor with
the variable parameters isn't even considered. This appears to me to
be a compiler bug. How can it convert a double to a class reference?

Am I making an erroneous assumption?
It's considering these ctors:

FSVec2D(double first, ...) // 1
FSVec2D(const FSVec2D& _r, double _w) // 2

Ctor (1) is an exact match on the first argument but an ellipsis match on
the second. Ctor (2) is an exact match on the second argument, but the
first one requires creation of a temporary FSVec2D via (1). Neither ctor is
considered better than the other for overload resolution, so the attempted
creation of v2 is ambiguous. To fix this, you can label (1) "explicit".

--
Doug Harrison
Visual C++ MVP
Jan 19 '07 #2
Doug,

Thanks. That works. I thought I had tried that, but I guess I had put
the explicit on the other constructor.

However, I now have a problem with my template class when I try to
make explicit instantiations of it for exporting from a library. As
stated in my original post (modified by adding the 'explicit'), I have
the following class declaration:

template <size_t _Size, class _Type>
class FSVector
{
// Fixed-size vector
public:
FSVector();
explicit FSVector(_Type first, ...);
FSVector(const FSVector<_Size - 1, _Type>& _r, _Type _w);
// ...
};

I then have the following in the .h file for the DLL containing the
above class:

#ifdef TPILINEARMATH_EXPORTS
#define TPILINEARMATH_API __declspec(dllexport)
#else
#define TPILINEARMATH_API __declspec(dllimport)
#endif

#include "FSVector.h"

// Explicit instantiation of common FSVector classes.
template class TPILINEARMATH_API FSVector<2, float>;
template class TPILINEARMATH_API FSVector<2, double>;
template class TPILINEARMATH_API FSVector<3, float>;
template class TPILINEARMATH_API FSVector<3, double>;
template class TPILINEARMATH_API FSVector<4, float>;
template class TPILINEARMATH_API FSVector<4, double>;

I get the following warning for each of the explicit instantiations:
warning C4661: 'FSVector<_Size,_Type>::FSVector(const
FSVector<1,_Type&,_Type)' : no suitable definition provided for
explicit template instantiation request

accompanied by a:
see declaration of 'FSVector<_Size,_Type>::__ctor'
referring to the 3rd c'tor.

However, if I have a static member function like this, instead of the
3rd c'tor:

static FSVector<_Size, _Typeconstruct(const FSVector<_Size - 1,
_Type>& _r, _Type _w);

the compiler doesn't have a problem with this. I don't understand why
it would have a problem with the constructor and not a static
function, but I guess I can use that instead. So, in an importing
application, I could have something like this:

FSVector<2, doublev2(1.1, 2.2);
FSVector<3, doublev3 = FSVector<3, double>::construct(v2, 3.3);

Unless you or someone else has a solution to the above problem, I'll
just use it that way.

Thanks again.

-John

On Thu, 18 Jan 2007 20:01:35 -0600, "Doug Harrison [MVP]"
<ds*@mvps.orgwrote:
>
It's considering these ctors:

FSVec2D(double first, ...) // 1
FSVec2D(const FSVec2D& _r, double _w) // 2

Ctor (1) is an exact match on the first argument but an ellipsis match on
the second. Ctor (2) is an exact match on the second argument, but the
first one requires creation of a temporary FSVec2D via (1). Neither ctor is
considered better than the other for overload resolution, so the attempted
creation of v2 is ambiguous. To fix this, you can label (1) "explicit".
Jan 22 '07 #3
Never mind. This was my bad. (Had some code commented out. Doh!)

Thanks again.

-John

On Mon, 22 Jan 2007 14:55:06 -0500, John Shell <no****@forme.com>
wrote:
>Doug,

Thanks. That works. I thought I had tried that, but I guess I had put
the explicit on the other constructor.

However, I now have a problem with my template class when I try to
make explicit instantiations of it for exporting from a library. As
stated in my original post (modified by adding the 'explicit'), I have
the following class declaration:

template <size_t _Size, class _Type>
class FSVector
{
// Fixed-size vector
public:
FSVector();
explicit FSVector(_Type first, ...);
FSVector(const FSVector<_Size - 1, _Type>& _r, _Type _w);
// ...
};

I then have the following in the .h file for the DLL containing the
above class:

#ifdef TPILINEARMATH_EXPORTS
#define TPILINEARMATH_API __declspec(dllexport)
#else
#define TPILINEARMATH_API __declspec(dllimport)
#endif

#include "FSVector.h"

// Explicit instantiation of common FSVector classes.
template class TPILINEARMATH_API FSVector<2, float>;
template class TPILINEARMATH_API FSVector<2, double>;
template class TPILINEARMATH_API FSVector<3, float>;
template class TPILINEARMATH_API FSVector<3, double>;
template class TPILINEARMATH_API FSVector<4, float>;
template class TPILINEARMATH_API FSVector<4, double>;

I get the following warning for each of the explicit instantiations:
warning C4661: 'FSVector<_Size,_Type>::FSVector(const
FSVector<1,_Type&,_Type)' : no suitable definition provided for
explicit template instantiation request

accompanied by a:
see declaration of 'FSVector<_Size,_Type>::__ctor'
referring to the 3rd c'tor.

However, if I have a static member function like this, instead of the
3rd c'tor:

static FSVector<_Size, _Typeconstruct(const FSVector<_Size - 1,
_Type>& _r, _Type _w);

the compiler doesn't have a problem with this. I don't understand why
it would have a problem with the constructor and not a static
function, but I guess I can use that instead. So, in an importing
application, I could have something like this:

FSVector<2, doublev2(1.1, 2.2);
FSVector<3, doublev3 = FSVector<3, double>::construct(v2, 3.3);

Unless you or someone else has a solution to the above problem, I'll
just use it that way.

Thanks again.

-John
Jan 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

99
by: David MacQuigg | last post by:
I'm not getting any feedback on the most important benefit in my proposed "Ideas for Python 3" thread - the unification of methods and functions. Perhaps it was buried among too many other less...
1
by: BCC | last post by:
Hi, I have a large mfc project that has been working fine. A while ago, I added an enum and I want to make a vector of these enums: class MyClass { public: MyClass() {}; ~MyClass() {}; ...
5
by: wongjoekmeu | last post by:
Hello All, I am trying to poort a code which is written in Visual C++ 6.0 to Visual C++ in VIsual Studio .NET I receive a problem which I simply do not understand. I was wondering if anyone could...
6
by: Peter Frost | last post by:
Please help I don't know if this is possible but what I would really like to do is to use On Error Goto to capture the code that is being executed when an error occurs. Any help would be much...
30
by: Will Pittenger | last post by:
Does C# inline functions? I do not see a inline keyword. Is there an implicit inline? Can the compiler select functions for auto-inlining? I am more used to C++ where all these things are...
4
by: Troy | last post by:
We recently installed the .Net framework on a windows 2000 server. Shortly after that we experienced intermitant problems running a web based program that accesses an Access 2002 database. The...
32
by: paul | last post by:
HI! I keep on getting this error and I have tried different things but I am not sure how to send the expiring date. The error that I am getting in Firefox 1.5 is "Error: expires.toGMTString is...
6
by: Saurabh | last post by:
Hi All, I want to know something. Apart from Constructor and Destructor are there any other functions which get called automatically without user calling them? If yes,please tell me which are...
5
by: Gene | last post by:
Hello all. Is the initializing assignment below ANSI standard- conforming? Is there a way to have the prefix parameters to ... type checked in such an assignment? E.g. in this case int x in the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.