当前位置: 代码迷 >> Ruby/Rails >> 【STL源码剖析】-error: ‘power’ was not declared in this scope
  详细解决方案

【STL源码剖析】-error: ‘power’ was not declared in this scope

热度:453   发布时间:2016-04-29 02:13:05.0
【STL源码剖析】--error: ‘power’ was not declared in this scope

#include <numeric>#include <vector>#include <iterator>#include <iostream>using namespace std;int main(int argc, char **argv){	int iArray[5] = {1, 2, 3, 4, 5};		vector<int> iVect(iArray, iArray+5);		cout << accumulate(iVect.begin(), iVect.end(), 0) << endl;	cout << accumulate(iVect.begin(), iVect.end(), 0, minus<int>()) << endl;		cout << inner_product(iVect.begin(), iVect.end(), iVect.begin(), 10) << endl;	cout << inner_product(iVect.begin(), iVect.end(), iVect.begin(), 10, minus<int>(), plus<int>()) << endl;		ostream_iterator<int> oIter(cout, " ");	partial_sum(iVect.begin(), iVect.end(), oIter);	cout << endl;	partial_sum(iVect.begin(), iVect.end(), oIter, minus<int>());	cout << endl;		adjacent_difference(iVect.begin(), iVect.end(), oIter);	//1 1 1 1 1(#1元素照录, #n新元素等于 #n 旧元素 - #n-1 旧元素 )	cout << endl;		adjacent_difference(iVect.begin(), iVect.end(), oIter, plus<int>());	//1 3 5 7 9(#1元素照录, #n新元素等于 op(#n 旧元素, #n-1 旧元素) )	cout << endl;		cout << power(10, 3) << endl;	///usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ext/numeric中有	cout << power(10, 3, plus<int>()) << endl;		int n = 3;	iota(iVect.begin(), iVect.end(), n);		for(int i = 0; i < iVect.size(); i++) {		cout << iVect[i] << " ";	}	cout << endl;}

编译报错:


主要原因:

       虽然GCC编译器完美集成了SGI STL,但是在使用GCC编译器编译使用SGI STL的源码时,需要注意:如iota、power等函数是非C++标准的函数,是SGI专属的函数,对于这类函数需要修改包含的头文件,这类头文件一般位于ext目录下,例如:numeric头文件位于/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/目录下,而扩展的numeric则位于/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ext/目录下。因此,上述代码的头文件改为:

#include <ext/numeric>		//For iota, already #include <numeric>#include <vector>#include <iterator>#include <iostream>using namespace std;using namespace __gnu_cxx;	//For power iota

特别注意:using namespace __gnu_cxx;不可少,因为头文件/usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ext/numeric中有一句_GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx),需要使用__gnu_cxx命名空间。

正确代码:

//#include <ext/functional> 	// For identity_element#include <ext/numeric>		//For iota, already #include <numeric>#include <vector>#include <iterator>#include <iostream>using namespace std;using namespace __gnu_cxx;	//For power iotaint main(int argc, char **argv){	int iArray[5] = {1, 2, 3, 4, 5};		vector<int> iVect(iArray, iArray+5);		cout << accumulate(iVect.begin(), iVect.end(), 0) << endl;	cout << accumulate(iVect.begin(), iVect.end(), 0, minus<int>()) << endl;		cout << inner_product(iVect.begin(), iVect.end(), iVect.begin(), 10) << endl;	cout << inner_product(iVect.begin(), iVect.end(), iVect.begin(), 10, minus<int>(), plus<int>()) << endl;		ostream_iterator<int> oIter(cout, " ");	partial_sum(iVect.begin(), iVect.end(), oIter);	cout << endl;	partial_sum(iVect.begin(), iVect.end(), oIter, minus<int>());	cout << endl;		adjacent_difference(iVect.begin(), iVect.end(), oIter);	//1 1 1 1 1(#1元素照录, #n新元素等于 #n 旧元素 - #n-1 旧元素 )	cout << endl;		adjacent_difference(iVect.begin(), iVect.end(), oIter, plus<int>());	//1 3 5 7 9(#1元素照录, #n新元素等于 op(#n 旧元素, #n-1 旧元素) )	cout << endl;		cout << power(10, 3) << endl;	///usr/lib/gcc/i686-redhat-linux/4.4.6/../../../../include/c++/4.4.6/ext/numeric中	cout << power(10, 3, plus<int>()) << endl;		int n = 3;	iota(iVect.begin(), iVect.end(), n);		for(int i = 0; i < iVect.size(); i++) {		cout << iVect[i] << " ";	}	cout << endl;}


再次编译运行


1楼Hero06206昨天 21:46
我好想忘记return 0;了
  相关解决方案