C++模板和无符号长问题

发布于 2024-10-19 23:05:15 字数 786 浏览 2 评论 0原文

我打算

const  madness::Vector<double,3> kvec(0.0, 0.0, 1.0);

通过调用以下代码

template <typename T>
Vector<T,3> vec(T x, T y, T z) {
   Vector<T,3> r; r[0] = x; r[1] = y; r[2] = z;
   return r;
}

来创建一个常量 Vector不幸的是,我收到一个错误,似乎说

extra.cc:216: error: no matching function for call to ‘madness::Vector<double, 3ul>::Vector(double, double, double)’
note: candidates are: madness::Vector<T, N>::Vector(const madness::Vector<T, N>&) [with T = double, long unsigned int N = 3ul]
note:                 madness::Vector<T, N>::Vector() [with T = double, long unsigned int N = 3ul]

我忍不住注意到错误消息中的 3ul 。为什么编译器会认为我无辜的小三是unsigned long?

提前致谢, 缺口

I am intending to make a constant Vector

const  madness::Vector<double,3> kvec(0.0, 0.0, 1.0);

By calling the following code

template <typename T>
Vector<T,3> vec(T x, T y, T z) {
   Vector<T,3> r; r[0] = x; r[1] = y; r[2] = z;
   return r;
}

Unfortunately, I get an error which seems to say

extra.cc:216: error: no matching function for call to ‘madness::Vector<double, 3ul>::Vector(double, double, double)’
note: candidates are: madness::Vector<T, N>::Vector(const madness::Vector<T, N>&) [with T = double, long unsigned int N = 3ul]
note:                 madness::Vector<T, N>::Vector() [with T = double, long unsigned int N = 3ul]

I can't help but notice the 3ul in the error message. Why does the compiler think my innocent little three is an unsigned long?

Thanks in advance,
Nick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮光之海 2024-10-26 23:05:15

你的 Vector 类模板可能是:

template<typename T, size_t N> class Vector ...

你的 size_t 可能是一个 unsigned long 吗?

另外,我怀疑你的真正意思是:

const  madness::Vector<double,3> kvec = vec(0.0, 0.0, 1.0);

is your Vector class template perhaps:

template<typename T, size_t N> class Vector ...

And is your size_t perhaps an unsigned long?

Also, I suspect you really meant:

const  madness::Vector<double,3> kvec = vec(0.0, 0.0, 1.0);
又爬满兰若 2024-10-26 23:05:15

如果您想使用 vec 函数,那么您需要

const  madness::Vector<double,3> kvec( vec(0.0, 0.0, 1.0) );

最终使用复制构造函数。按照您编写的方式,您需要为向量类提供一个需要三个双精度值的构造函数。

If you want to use your vec function then you need to do

const  madness::Vector<double,3> kvec( vec(0.0, 0.0, 1.0) );

which will use the copy constructor ultimately. The way you've written it you would need to give the vector class a constructor that takes three doubles.

我乃一代侩神 2024-10-26 23:05:15

添加一个需要 3 个 double 的构造函数。您还没有,或者您错误地声明了它,或者您想使用 = { ... } 来使用聚合初始化而不是构造函数。选择适合的。

至于 3u - 您很可能将相应的模板参数声明为 unsigned long int

请注意,这有一个重要的结果:

template<typename T, int N>
void f(Vector<T, N> v);

// will not work, since "N" is an int, not an "unsigned long int"!
f(kvec);

您需要知道非类型模板参数的确切类型才能推断出参数。在这种情况下,您需要将 N 设为 unsigned long int

Add a constructor that takes 3 double. You haven't got one yet, or you declared it wrongly or instead of a constructor you want to use aggregate initialization using = { ... }. Choose what fits.

As for the 3u - you most probably declared the corresponding template parameter as unsigned long int.

Notice that this has an important consequence

template<typename T, int N>
void f(Vector<T, N> v);

// will not work, since "N" is an int, not an "unsigned long int"!
f(kvec);

You need to know the exact type of the non-type template parameter to be able to deduce arguments. In this case, you would need N to be a unsigned long int.

昨迟人 2024-10-26 23:05:15

添加一个接受三个三输入的构造函数。就像这样:

template <unsigned int T, class N>
class Vector {
public:
  // ...
  Vector(T x, T y, T z);
  // ..
};

Add a constructor that takes three three inputs. Like so:

template <unsigned int T, class N>
class Vector {
public:
  // ...
  Vector(T x, T y, T z);
  // ..
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文