#include<iostream>
#include<string>
using namespace std;
void write_string(OutIt& out, const string& val)
{
string::const_iterator end=val.begin()+val.length();
copy(val.begin(), end, out);
}
int _tmain(int argc, _TCHAR* argv[])
{
string val1="che jin xing very good!";
string val2;
write_string(val2,val1);
return 0;
}
编译出错
d:\microsoft visual studio 8\vc\include\xutility(572) : error C2039: “iterator_category”: 不是“std::basic_string<_Elem,_Traits,_Ax>”的成员
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
哪位大侠出手指点一下啊!!!
9 个解决方案
#1
#include <iostream>
#include <string>
using namespace std;
template<class OutIt>
void write_string(OutIt& out, const string& val)
{
string::const_iterator end=val.begin()+val.length();
copy( val.begin(), end, out );
}
int main(int argc, char* argv[])
{
string val1="che jin xing very good!";
string val2;
write_string( val2.begin() ,val1);
return 0;
}
#2
up
#3
用了模板类。。
#4
akirya 我用的vs2005 出现
:\microsoft visual studio 8\vc\include\xutility(572) : error C2039: “iterator_category”: 不是“std::basic_string<_Elem,_Traits,_Ax>”的成员
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> d:\microsoft visual studio 8\vc\include\xutility(2282): 参见对正在编译的类 模板 实例化“std::iterator_traits<_Iter>”的引用
1> with
1> [
1> _Iter=std::string
1> ]
1> e:\编程练习\vs\vs1\vs1\vs1.cpp(18): 参见对正在编译的函数 模板 实例化“_OutIt std::copy<std::_String_const_iterator<_Elem,_Traits,_Alloc>,OutIt>(_InIt,_InIt,_OutIt)”的引用
1> with
1> [
1> _OutIt=std::string,
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>,
1> OutIt=std::string,
1> _InIt=std::_String_const_iterator<char,std::char_traits<char>,std::allocator<char>>
1> ]
1> e:\编程练习\vs\vs1\vs1\vs1.cpp(41): 参见对正在编译的函数 模板 实例化“void write_string<std::string>(OutIt &,const std::string &)”的引用
1> with
1> [
1> OutIt=std::string
1> ]
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C2146: 语法错误 : 缺少“;”(在标识符“iterator_category”的前面)
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C2602: “std::iterator_traits<_Iter>::iterator_category”不是“std::iterator_traits<_Iter>”基类的成员
1> with
1> [
1> _Iter=std::string
1> ]
1> d:\microsoft visual studio 8\vc\include\xutility(572) : 参见“std::iterator_traits<_Iter>::iterator_category”的声明
1> with
1> [
1> _Iter=std::string
1> ]
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C2868: “std::iterator_traits<_Iter>::iterator_category”: 非法的 using 声明语法;应输入限定名
1> with
1> [
1> _Iter=std::string
:\microsoft visual studio 8\vc\include\xutility(572) : error C2039: “iterator_category”: 不是“std::basic_string<_Elem,_Traits,_Ax>”的成员
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> d:\microsoft visual studio 8\vc\include\xutility(2282): 参见对正在编译的类 模板 实例化“std::iterator_traits<_Iter>”的引用
1> with
1> [
1> _Iter=std::string
1> ]
1> e:\编程练习\vs\vs1\vs1\vs1.cpp(18): 参见对正在编译的函数 模板 实例化“_OutIt std::copy<std::_String_const_iterator<_Elem,_Traits,_Alloc>,OutIt>(_InIt,_InIt,_OutIt)”的引用
1> with
1> [
1> _OutIt=std::string,
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>,
1> OutIt=std::string,
1> _InIt=std::_String_const_iterator<char,std::char_traits<char>,std::allocator<char>>
1> ]
1> e:\编程练习\vs\vs1\vs1\vs1.cpp(41): 参见对正在编译的函数 模板 实例化“void write_string<std::string>(OutIt &,const std::string &)”的引用
1> with
1> [
1> OutIt=std::string
1> ]
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C2146: 语法错误 : 缺少“;”(在标识符“iterator_category”的前面)
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C2602: “std::iterator_traits<_Iter>::iterator_category”不是“std::iterator_traits<_Iter>”基类的成员
1> with
1> [
1> _Iter=std::string
1> ]
1> d:\microsoft visual studio 8\vc\include\xutility(572) : 参见“std::iterator_traits<_Iter>::iterator_category”的声明
1> with
1> [
1> _Iter=std::string
1> ]
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C2868: “std::iterator_traits<_Iter>::iterator_category”: 非法的 using 声明语法;应输入限定名
1> with
1> [
1> _Iter=std::string
#5
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
template<typename OutIt>
void write_string(OutIt& out, const string& val)
{
string::const_iterator end=val.begin()+val.length();
copy(val.begin(), end, out);
}
int _tmain(int argc, _TCHAR* argv[])
{
string val1 = "che jin xing very good!";
string val2(val1.length(), '\0');
write_string(val2.begin(), val1);
return 0;
}
#6
呃,我是用VS2008测试的,我贴的代码没问题呀
#7
注意,copy的第三个参数要iterator
template <class InputIterator, class OutputIterator>
OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result ); <algorithm>
#8
1楼传的参数是 write_string( val2.begin() ,val1);
而lz传的参数是:write_string(val2,val1);
而lz传的参数是:write_string(val2,val1);
#9
终于解决了 write_string(val2.begin(), val1); 模板中的out参数是引用,为何也能传递指针??
#1
#include <iostream>
#include <string>
using namespace std;
template<class OutIt>
void write_string(OutIt& out, const string& val)
{
string::const_iterator end=val.begin()+val.length();
copy( val.begin(), end, out );
}
int main(int argc, char* argv[])
{
string val1="che jin xing very good!";
string val2;
write_string( val2.begin() ,val1);
return 0;
}
#2
up
#3
用了模板类。。
#4
akirya 我用的vs2005 出现
:\microsoft visual studio 8\vc\include\xutility(572) : error C2039: “iterator_category”: 不是“std::basic_string<_Elem,_Traits,_Ax>”的成员
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> d:\microsoft visual studio 8\vc\include\xutility(2282): 参见对正在编译的类 模板 实例化“std::iterator_traits<_Iter>”的引用
1> with
1> [
1> _Iter=std::string
1> ]
1> e:\编程练习\vs\vs1\vs1\vs1.cpp(18): 参见对正在编译的函数 模板 实例化“_OutIt std::copy<std::_String_const_iterator<_Elem,_Traits,_Alloc>,OutIt>(_InIt,_InIt,_OutIt)”的引用
1> with
1> [
1> _OutIt=std::string,
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>,
1> OutIt=std::string,
1> _InIt=std::_String_const_iterator<char,std::char_traits<char>,std::allocator<char>>
1> ]
1> e:\编程练习\vs\vs1\vs1\vs1.cpp(41): 参见对正在编译的函数 模板 实例化“void write_string<std::string>(OutIt &,const std::string &)”的引用
1> with
1> [
1> OutIt=std::string
1> ]
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C2146: 语法错误 : 缺少“;”(在标识符“iterator_category”的前面)
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C2602: “std::iterator_traits<_Iter>::iterator_category”不是“std::iterator_traits<_Iter>”基类的成员
1> with
1> [
1> _Iter=std::string
1> ]
1> d:\microsoft visual studio 8\vc\include\xutility(572) : 参见“std::iterator_traits<_Iter>::iterator_category”的声明
1> with
1> [
1> _Iter=std::string
1> ]
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C2868: “std::iterator_traits<_Iter>::iterator_category”: 非法的 using 声明语法;应输入限定名
1> with
1> [
1> _Iter=std::string
:\microsoft visual studio 8\vc\include\xutility(572) : error C2039: “iterator_category”: 不是“std::basic_string<_Elem,_Traits,_Ax>”的成员
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> d:\microsoft visual studio 8\vc\include\xutility(2282): 参见对正在编译的类 模板 实例化“std::iterator_traits<_Iter>”的引用
1> with
1> [
1> _Iter=std::string
1> ]
1> e:\编程练习\vs\vs1\vs1\vs1.cpp(18): 参见对正在编译的函数 模板 实例化“_OutIt std::copy<std::_String_const_iterator<_Elem,_Traits,_Alloc>,OutIt>(_InIt,_InIt,_OutIt)”的引用
1> with
1> [
1> _OutIt=std::string,
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>,
1> OutIt=std::string,
1> _InIt=std::_String_const_iterator<char,std::char_traits<char>,std::allocator<char>>
1> ]
1> e:\编程练习\vs\vs1\vs1\vs1.cpp(41): 参见对正在编译的函数 模板 实例化“void write_string<std::string>(OutIt &,const std::string &)”的引用
1> with
1> [
1> OutIt=std::string
1> ]
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C2146: 语法错误 : 缺少“;”(在标识符“iterator_category”的前面)
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C2602: “std::iterator_traits<_Iter>::iterator_category”不是“std::iterator_traits<_Iter>”基类的成员
1> with
1> [
1> _Iter=std::string
1> ]
1> d:\microsoft visual studio 8\vc\include\xutility(572) : 参见“std::iterator_traits<_Iter>::iterator_category”的声明
1> with
1> [
1> _Iter=std::string
1> ]
1>d:\microsoft visual studio 8\vc\include\xutility(572) : error C2868: “std::iterator_traits<_Iter>::iterator_category”: 非法的 using 声明语法;应输入限定名
1> with
1> [
1> _Iter=std::string
#5
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
template<typename OutIt>
void write_string(OutIt& out, const string& val)
{
string::const_iterator end=val.begin()+val.length();
copy(val.begin(), end, out);
}
int _tmain(int argc, _TCHAR* argv[])
{
string val1 = "che jin xing very good!";
string val2(val1.length(), '\0');
write_string(val2.begin(), val1);
return 0;
}
#6
呃,我是用VS2008测试的,我贴的代码没问题呀
#7
注意,copy的第三个参数要iterator
template <class InputIterator, class OutputIterator>
OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result ); <algorithm>
#8
1楼传的参数是 write_string( val2.begin() ,val1);
而lz传的参数是:write_string(val2,val1);
而lz传的参数是:write_string(val2,val1);
#9
终于解决了 write_string(val2.begin(), val1); 模板中的out参数是引用,为何也能传递指针??