C+的函数指针+; 我正在学习LIPPMAN对C++的C++。下面是一些代码,其中两行包含错误,而我不知道为什么会发生错误以及如何修复错误 #include "stdafx.h" #include <vector> #include <iostream> #include <cstdlib> #include <fstream> using namespace std; const vector<int>* fibon_seq(int size) { const int max_size = 1024; static vector<int> elems; if (size <= 0 || size > max_size) { cerr << "Error"; return 0; } for (int ix = elems.size(); ix < size; ++ix) { if (ix == 0 || ix == 1) elems.push_back(1); else elems.push_back(elems[ix - 1] + elems[ix - 2]); } return &elems; } void display(const vector<int>* vec, ostream &os = cout) { if (!vec) cerr << "null vector"; for (int i = 0; i < (vec)->size(); ++i) os << (*vec)[i] << " "; os << endl; } bool fibon_elem(int pos, int &elem, const vector<int>* (*seq_ptr)(int)) { const vector<int> *pseq = seq_ptr(pos); if (!pseq){ elem = 0; return false; } elem = (*pseq)[pos - 1]; return true; } int main() { const vector<int>* (*(*seq_array)[1])(int); (*seq_array)[0] = fibon_seq; vector<int>* (*seq_ptr)(int); int seq_index = 0; seq_ptr = (*seq_array)[0];//This is the line with error. //(a value of type "const std::vector<int, std::allocator<int>> *(*)(int)" //cannot be assigned to an entity of type "std::vector<int, std::allocator<int>> //*(*)(int)" //C2440 '=': cannot convert from 'const std::vector<int,std::allocator<_Ty>> //*(__cdecl *)(int)' to 'std::vector<int,std::allocator<_Ty>> //*(__cdecl *)(int)' int a; fibon_elem(12, a, seq_ptr);//This is the line with error. //argument of type "std::vector<int, std::allocator<int>> *(*)(int)" //is incompatible with parameter of type "const std::vector<int, std::allocator<int>> //*(*)(int)" //C2664 'bool fibon_elem(int,int &,const std::vector<int,std::allocator<_Ty>> //*(__cdecl *)(int))': cannot convert argument 3 from 'std::vector<int,std::allocator<_Ty>> //*(__cdecl *)(int)' to 'const std::vector<int,std::allocator<_Ty>> //*(__cdecl *)(int)' test getchar(); return 0; } #包括“stdafx.h” #包括 #包括 #包括 #包括 使用名称空间std; 常量向量*fibon_seq(整数大小){ const int max_size=1024; 静态矢量元素; if(最大尺寸){ cerr

C+的函数指针+; 我正在学习LIPPMAN对C++的C++。下面是一些代码,其中两行包含错误,而我不知道为什么会发生错误以及如何修复错误 #include "stdafx.h" #include <vector> #include <iostream> #include <cstdlib> #include <fstream> using namespace std; const vector<int>* fibon_seq(int size) { const int max_size = 1024; static vector<int> elems; if (size <= 0 || size > max_size) { cerr << "Error"; return 0; } for (int ix = elems.size(); ix < size; ++ix) { if (ix == 0 || ix == 1) elems.push_back(1); else elems.push_back(elems[ix - 1] + elems[ix - 2]); } return &elems; } void display(const vector<int>* vec, ostream &os = cout) { if (!vec) cerr << "null vector"; for (int i = 0; i < (vec)->size(); ++i) os << (*vec)[i] << " "; os << endl; } bool fibon_elem(int pos, int &elem, const vector<int>* (*seq_ptr)(int)) { const vector<int> *pseq = seq_ptr(pos); if (!pseq){ elem = 0; return false; } elem = (*pseq)[pos - 1]; return true; } int main() { const vector<int>* (*(*seq_array)[1])(int); (*seq_array)[0] = fibon_seq; vector<int>* (*seq_ptr)(int); int seq_index = 0; seq_ptr = (*seq_array)[0];//This is the line with error. //(a value of type "const std::vector<int, std::allocator<int>> *(*)(int)" //cannot be assigned to an entity of type "std::vector<int, std::allocator<int>> //*(*)(int)" //C2440 '=': cannot convert from 'const std::vector<int,std::allocator<_Ty>> //*(__cdecl *)(int)' to 'std::vector<int,std::allocator<_Ty>> //*(__cdecl *)(int)' int a; fibon_elem(12, a, seq_ptr);//This is the line with error. //argument of type "std::vector<int, std::allocator<int>> *(*)(int)" //is incompatible with parameter of type "const std::vector<int, std::allocator<int>> //*(*)(int)" //C2664 'bool fibon_elem(int,int &,const std::vector<int,std::allocator<_Ty>> //*(__cdecl *)(int))': cannot convert argument 3 from 'std::vector<int,std::allocator<_Ty>> //*(__cdecl *)(int)' to 'const std::vector<int,std::allocator<_Ty>> //*(__cdecl *)(int)' test getchar(); return 0; } #包括“stdafx.h” #包括 #包括 #包括 #包括 使用名称空间std; 常量向量*fibon_seq(整数大小){ const int max_size=1024; 静态矢量元素; if(最大尺寸){ cerr,c++,C++,您的函数fibon\u seq返回一个常量向量*,您正试图将其分配给一个向量*,即seq\u ptr。将seq\u ptr更改为常量向量*或更改光纤的返回类型。错误所在的两行是什么?@MacStation:Search for//这是有错误的行。Wh具体错误是什么?@MacStation我在代码中对它们进行了注释。它们都在主函数中。我在你的问题中没有看到任何实际的错误消息。通过总结,你省去了我们需要帮助你的关键信息。这真的很有效!对于有错误的第一行!!所以这里的问题是cont vector*不能

您的函数fibon\u seq返回一个
常量向量*
,您正试图将其分配给一个
向量*
,即seq\u ptr。将seq\u ptr更改为
常量向量*
或更改光纤的返回类型。

错误所在的两行是什么?@MacStation:Search for
//这是有错误的行。
Wh具体错误是什么?@MacStation我在代码中对它们进行了注释。它们都在主函数中。我在你的问题中没有看到任何实际的错误消息。通过总结,你省去了我们需要帮助你的关键信息。这真的很有效!对于有错误的第一行!!所以这里的问题是
cont vector*
不能分配给
vector*
。嗯,请你再给我解释一下好吗?真的很感谢!@JackieWang
const vector*
是指向
int
s的
vector
的指针,它是指向
int
s的
vector
的指针,它可以更改被更改。允许某人将不可更改的内容分配给可更改的内容将打开有人试图更改不可更改内容的可能性。这将是不好的。@user4581301太好了!明白了!非常感谢!!如果找到答案,请确保将问题标记为已回答。很抱歉,但实际上,仍然存在错误代码中的另一个错误。它在调用函数时将(指向函数的指针)作为参数调用。您可以在源代码中找到这一行,就在您刚才提到的那一行之后!