gpt4 book ai didi

c++ - 'point' 的初始化没有匹配的构造函数

转载 作者:行者123 更新时间:2023-11-30 02:27:04 34 4
gpt4 key购买 nike

很抱歉,如果我仍然问这个问题,但是如果我做一个小例子,我得到的解决方案是有效的,如果我尝试在我的项目中实现它,我会不断收到标题中的错误。
这是我的结构

template<typename T>
struct element{
int i;
int j;
T val;

element(){}
element(T &other): val(other){} // this is the solution i previously got

template<typename U>
element(const element<U>& e): val(static_cast<T>(e.val)), i(e.i), j(e.j){}
// this one is used to help the main class cast types.
};

在我的 sparseMatrix 类中,我创建了一个元素类型的动态数组,

element<T> *m; // the class is also template

一切都适用于基本数据类型。
我需要做的是让我的动态数组(由 elements 组成)使用自定义数据类型。
我一直在做的测试是用一个结构点

struct point {
int x;
int y;

point(int xx, int yy) : x(xx), y(yy) {}
};

我在类中实现的构造函数是

explicit sparseMatrix(const T& d) : mat(0), capacity(0), n(0), def_value(d) {
m = new element<T> [capacity];
}

主要是我做的

sparseMatrix<point> mcp(point(1, 2));

我得到错误'point'初始化没有匹配的构造函数我做错了什么?
我该如何解决这个问题?
[注意,我不能使用 c++11]

最佳答案

在模板类中 element您存储类型的实例 T .当 element<T>然后创建实例 val默认构建

当您使用 point 时作为T你会得到一个错误,因为它没有默认构造函数。

关于c++ - 'point' 的初始化没有匹配的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42136450/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com