码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-05-14 22:02:03      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:sgi   stl   power   

#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 iota

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;
}


再次编译运行

技术分享

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

标签:sgi   stl   power   

原文地址:http://blog.csdn.net/hero06206/article/details/45726065

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!