c++ 的问题模板构造函数

发布于 2024-11-09 02:41:18 字数 1308 浏览 0 评论 0原文

代码:

#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 技术交流群。

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

发布评论

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

评论(3

帝王念 2024-11-16 02:41:18

未提供编译器生成的默认构造函数,因为您已经创建了自己的构造函数。因此,当您创建不带参数的构造函数的 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.

(り薆情海 2024-11-16 02:41:18

当你声明一个变量时,

point<int,2> P2;

它使用默认构造函数;它可以在 2 种情况下使用:

  1. 您尚未声明ANY
    类主体中的构造函数。因此
    编译器会生成一个默认的
    自动,您可以使用它。
  2. 您声明/定义默认值
    显式构造函数(无论是空的,
    如果你不做任何事情)

因为在这里你什么都不做:只需声明一个空的默认构造函数:

template<class T, int N> class point {
//...
public:
  point() {}  // <-- default constructor
};

这将清除你的错误。

另外还有一个重要警告

prog.cpp:8: warning: returning reference to temporary

这是因为您的操作符[]
更改行,

const double& operator[](int i) const

至,

const T& operator[](int i) const  // for <int, N> you should return 'int' not 'double'

When you declare a variable something like,

point<int,2> P2;

It uses default constructor; it can be used in 2 scenarios:

  1. You haven't declared ANY
    constructor in your class body. Thus
    compiler will generate a default one
    automatically and you can use it.
  2. You declare/define a default
    constructor explicitly (be it empty,
    if you don't do anything)

Since here you don't do anything: just declare an empty default constructor:

template<class T, int N> class point {
//...
public:
  point() {}  // <-- default constructor
};

This will clear your errors.

Also there is an Important Warning:

prog.cpp:8: warning: returning reference to temporary

That is because of your operator [].
Change the line,

const double& operator[](int i) const

To,

const T& operator[](int i) const  // for <int, N> you should return 'int' not 'double'
想念有你 2024-11-16 02:41:18

问题是,通过这两行,

point<int,2> P2;
point<double,3> P3;

您尝试通过默认的无参数构造函数创建两个“点”对象。

但是,除非您指定任何其他构造函数,否则不会自动生成此构造函数。实现默认构造函数将解决您的问题

The problem is that with these two lines

point<int,2> P2;
point<double,3> P3;

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文