c++ 的问题模板构造函数
代码:
#include<iostream>
using namespace std;
template<class T, int N> class point {
T coordinate[N];
public:
point(const point<T,N>&);
const double& operator[](int i) const {
return coordinate[i];
}
};
template<class T, int N> point<T,N>::point(const point<T,N>&p)
{
for(int i=0;i<N;i++)
coordinate[i]=p.coordinate[i];
};
int main() {
point<int,2> P2;
point<double,3> P3;
cout<<P2[0]<<P3[1];
return 0;
}
输出:
prog.cpp: In function ‘int main()’:
prog.cpp:17: error: no matching function for call to ‘point<int, 2>::point()’
prog.cpp:11: note: candidates are: point<T, N>::point(const point<T, N>&) [with T =
int, int N = 2]
prog.cpp:18: error: no matching function for call to ‘point<double, 3>::point()’
prog.cpp:11: note: candidates are: point<T, N>::point(const point<T, N>&) [with T =
double, int N = 3]
prog.cpp: In member function ‘const double& point<T, N>::operator[](int) const [with
T = int, int N = 2]’:
prog.cpp:19: instantiated from here
prog.cpp:8: warning: returning reference to temporary
请帮我排除故障。
code:
#include<iostream>
using namespace std;
template<class T, int N> class point {
T coordinate[N];
public:
point(const point<T,N>&);
const double& operator[](int i) const {
return coordinate[i];
}
};
template<class T, int N> point<T,N>::point(const point<T,N>&p)
{
for(int i=0;i<N;i++)
coordinate[i]=p.coordinate[i];
};
int main() {
point<int,2> P2;
point<double,3> P3;
cout<<P2[0]<<P3[1];
return 0;
}
output:
prog.cpp: In function ‘int main()’:
prog.cpp:17: error: no matching function for call to ‘point<int, 2>::point()’
prog.cpp:11: note: candidates are: point<T, N>::point(const point<T, N>&) [with T =
int, int N = 2]
prog.cpp:18: error: no matching function for call to ‘point<double, 3>::point()’
prog.cpp:11: note: candidates are: point<T, N>::point(const point<T, N>&) [with T =
double, int N = 3]
prog.cpp: In member function ‘const double& point<T, N>::operator[](int) const [with
T = int, int N = 2]’:
prog.cpp:19: instantiated from here
prog.cpp:8: warning: returning reference to temporary
Please help me sort out the faults.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
未提供编译器生成的默认构造函数,因为您已经创建了自己的构造函数。因此,当您创建不带参数的构造函数的
P2
时,您需要定义一个默认构造函数以供编译。The compiler-generated default constructor is not being provided because you have created a constructor of your own. Therefore when you create
P2
with no arguments to its constructor, you need to define a default constructor for it to compile.当你声明一个变量时,
它使用默认构造函数;它可以在 2 种情况下使用:
类主体中的构造函数。因此
编译器会生成一个默认的
自动,您可以使用它。
显式构造函数(无论是空的,
如果你不做任何事情)
因为在这里你什么都不做:只需声明一个空的默认构造函数:
这将清除你的错误。
另外还有一个重要警告:
这是因为您的
操作符[]
。更改行,
至,
When you declare a variable something like,
It uses default constructor; it can be used in 2 scenarios:
constructor in your class body. Thus
compiler will generate a default one
automatically and you can use it.
constructor explicitly (be it empty,
if you don't do anything)
Since here you don't do anything: just declare an empty default constructor:
This will clear your errors.
Also there is an Important Warning:
That is because of your
operator []
.Change the line,
To,
问题是,通过这两行,
您尝试通过默认的无参数构造函数创建两个“点”对象。
但是,除非您不指定任何其他构造函数,否则不会自动生成此构造函数。实现默认构造函数将解决您的问题
The problem is that with these two lines
you are attempting to create two 'point' object via the default parameterless constructor.
However, this constructor is not automatically generated unless you do not specify any others. Implementing the default constructor will solve you problem