调试程序的时候发生错误:无法从“initializer list”转换为“double” 求大神指教!!

Roderick_Wang 2015-11-18 12:34:01
小弟最近在编一系列矩阵测试的算法,但是在调试的时候编译器报错:C2440 无法从“initializer list”转换为“double”
不清楚是怎么回事,我上网查也没看懂,还请大神指教!!!
下图就是我编译时候报错的截屏

下面上代码~
int main()
{
Eigen::SparseMatrix<double> SparseMatrix_Debugger(5, 5);
vector<double> vec_Debugger;
TripleList TripleList_Debugger;
vector<Eigen::Triplet<double>> Triplet_Debugger;
double Row[11] = { 0, 0, 0, 1, 1, 2, 2, 2, 3, 4, 4 };
double Col[11] = { 0, 2, 3, 0, 4, 1, 2, 4, 1, 0, 2 };
double Val[11] = { 3, 8, 4, 1, 6, 2, 2, 8, 2, 6, 7 };
for (int i = 0;i <= 10;i++)
{
// 将三元表数据存储在自编的 TripleList 类中
TripleList_Debugger.Row.push_back(Row[i]);
TripleList_Debugger.Col.push_back(Col[i]);
TripleList_Debugger.Value.push_back(Val[i]);
//下面的这个 CombineTripleList() 是我自己编的 void 型函数,目的是把 TripleList 类中的 Row、Col、Value 分别逐次赋值给一个 vector 以便于用 swap 函数给 Triplet.Debugger 这个 vector 赋值
TripleList_Debugger.CombineTripleList();
//此处直接用 swap 赋值会报错,这个小弟也不明白是为什么
//Triplet_Debugger.swap(TripleList_Debugger.BridgeToTriplet);
Triplet_Debugger.emplace_back(TripleList_Debugger.Row[i], TripleList_Debugger.Col[i], TripleList_Debugger.Value[i]);
}
SparseMatrix_Debugger.setFromTriplets(Triplet_Debugger.begin(), Triplet_Debugger.end());
Eigen::MatrixXd MatrixXd_Debugger = SparseMatrix_Debugger;
//下面是输出矩阵测试结果
cout << "MatrixXd:\n" << MatrixXd_Debugger << endl;
cout << "\nRow:" << endl;
for (int a = 0;a < TripleList_Debugger.Row.size();a++)
{
cout << TripleList_Debugger.Row[a] << endl;
}
cout << "\nCol:" << endl;
for (int b = 0;b < TripleList_Debugger.Col.size();b++)
{
cout << TripleList_Debugger.Col[b] << endl;
}
cout << "\nValue:" << endl;
for (int c = 0;c < TripleList_Debugger.Value.size();c++)
{
cout << TripleList_Debugger.Value[c] << endl;
}
cout << "\nBridgeToTrplet\n:" << endl;
for (int t = 0;t < TripleList_Debugger.BridgeToTriplet.size();t++)
{
cout << TripleList_Debugger.BridgeToTriplet[t] << endl;
}
return 0;
}

以上是 main() 中的内容,下面把我自己编的类的定义也附上

//TripleList.h
class TripleList
{
public:
TripleList();
~TripleList();
std::vector <double> Row;
std::vector <double> Col;
std::vector <double> Value;
std::vector <double> BridgeToTriplet;
double FindValue(double row, double col);
void CombineTripleList();
};

//TripleList.cpp
#include "stdafx.h"
#include "TripleList.h"

TripleList::TripleList()
{
}

TripleList::~TripleList()
{
}

double TripleList::FindValue(double row, double col)
{
double Ans;
if (row <= 0 || col <= 0)
AfxMessageBox(_T("The coordinate you've entered is illegal, please cheak and reenter!"));
else
for (int i = 0; Row.at(i) == row; i++)
{
for (int n = i; Col.at(n) == col; n++)
{
Ans = Value.at(n);
}
}
return Ans;
}

void TripleList::CombineTripleList()
{
for (int i = 0;i < Row.size();i++)
BridgeToTriplet.emplace_back(Row.at(i), Col.at(i), Value.at(i));
}
...全文
6323 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2015-12-04
  • 打赏
  • 举报
回复
偶遇到类似问题都是用 “每次用/*...*/注释掉不同部分再重新编译,直到定位到具体语法出错的位置。” 的方法解决的。
Roderick_Wang 2015-12-03
  • 打赏
  • 举报
回复
这个问题我到现在也没解决,自己反复检查了好几遍还是不懂。之后我吧这些代码重新写了一遍,里面的内容基本没变,但是却一遍跑通了再没报类似的错误。个人猜测可能是框架的问题
zhouxiaofeng1021 2015-11-19
  • 打赏
  • 举报
回复
C2440 无法从“initializer list”转换为“double” 这个错误提示函数需要参数为“double”型别 然而你传进去的是“initializer list”
paschen 版主 2015-11-19
  • 打赏
  • 举报
回复
如果要表示double型常量,建议写成1.0这种,1是当作int型常量,1.0f则是float型
Roderick_Wang 2015-11-19
  • 打赏
  • 举报
回复
引用 4楼zhouxiaofeng1021 的回复:
C2440 无法从“initializer list”转换为“double” 这个错误提示函数需要参数为“double”型别 然而你传进去的是“initializer list”
多谢回复! 这个我能看明白,但是不知道这里应该怎么修改。能不能请您解释的具体些呢?
Roderick_Wang 2015-11-18
  • 打赏
  • 举报
回复
引用 1楼赵4老师 的回复:
    double Row[11] = { 0., 0., 0., 1., 1., 2., 2., 2., 3., 4., 4. };
    double Col[11] = { 0., 2., 3., 0., 4., 1., 2., 4., 1., 0., 2. };
    double Val[11] = { 3., 8., 4., 1., 6., 2., 2., 8., 2., 6., 7. };
试试看。
多谢回复! 我按照您这个修改后还是会报错,报错的信息和之前一样
赵4老师 2015-11-18
  • 打赏
  • 举报
回复
    double Row[11] = { 0., 0., 0., 1., 1., 2., 2., 2., 3., 4., 4. };
    double Col[11] = { 0., 2., 3., 0., 4., 1., 2., 4., 1., 0., 2. };
    double Val[11] = { 3., 8., 4., 1., 6., 2., 2., 8., 2., 6., 7. };
试试看。

65,202

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧