急啊!!!谢谢啦!
源文件:
#include <iostream>
#include <fstream>
using namespace std;
template <class Ch, class Tr = char_traits<Ch> >
class basic_nullstreambuf : public basic_streambuf<Ch, Tr>
{
public:
basic_nullstreambuf(void)
{
};
virtual ~basic_nullstreambuf(void)
{
};
protected:
virtual streampos seekoff(
streamoff off,
ios::seek_dir dir,
int nMode = ios::in | ios::out)
{
return Tr::eof();
};
virtual streampos seekpos(
streampos pos,
int nMode = ios::in | ios::out)
{
return Tr::eof();
};
virtual int overflow(int nCh = Tr::eof())
{
return 0;
};
virtual int underflow(void)
{
return Tr::eof();
};
};
typedef basic_nullstreambuf<char> nullstreambuf;
class nullstream : public ostream
{
public:
nullstream(): ostream(new nullstreambuf())
{
};
virtual ~nullstream()
{
};
};
class ClassB
{
public:
ClassB()
{
m_pStream = &m_NullStream;
};
~ClassB()
{
};
void setOut(ostream &out)
{
m_pStream = &out;
};
ostream &out()
{
return *m_pStream;
};
ostream &getNullStream() const
{
return m_NullStream;
};
private:
static nullstream m_NullStream;
ostream *m_pStream;
};
nullstream ClassB::m_NullStream;
int main()
{
ofstream file("B.txt");
ClassB obj;
obj.setOut(file);
cout << "Where is this first message output?" << endl;
obj.out() << "Where is this second message output?" << endl;
file.close();
return 0;
}
给出的错误提示:
ostreamTest.cpp:22: no type `seek_dir' in `std::basic_ios<char,
std::char_traits<char> >'
ostreamTest.cpp:22: parse error before `,' token
ostreamTest.cpp:24: default template arguments may not be used in function
templates
ostreamTest.cpp:24: template definition of non-template `virtual std::streampos
std::basic_ios<char, std::char_traits<char> >::seekoff(...)'
ostreamTest.cpp:24: template definition of non-template `virtual std::streampos
basic_nullstreambuf<Ch, Tr>::seekoff(...)'
ostreamTest.cpp:46: wrong number of template arguments (1, should be 2)
ostreamTest.cpp:8: provided for `template<class Ch, class Tr> class
basic_nullstreambuf'
ostreamTest.cpp:46: ISO C++ forbids declaration of `nullstreambuf' with no type
ostreamTest.cpp: In constructor `nullstream::nullstream()':
ostreamTest.cpp:53: no matching function for call to `std::basic_ostream<char,
std::char_traits<char> >::basic_ostream(nullstreambuf*&)'
/usr/include/c++/3.2.2/iosfwd:61: candidates are: std::basic_ostream<char,
std::char_traits<char> >::basic_ostream(const std::basic_ostream<char,
std::char_traits<char> >&)
/usr/include/c++/3.2.2/ostream:72: std::basic_ostream<_CharT,
_Traits>::basic_ostream(std::basic_streambuf<_CharT, _Traits>*) [with _CharT
= char, _Traits = std::char_traits<char>]
5 个解决方案
#1
可能是模板嵌套的问题
#2
模板里面的模板用typedef试试
#3
至少,你先把seekdir搞定掉。按编译提示,它不在ios里,你就去找出正确的地方呀。
你现在写的代码肯定是强烈耦合于VS2005的私有实现了。
你现在写的代码肯定是强烈耦合于VS2005的私有实现了。
#4
http://poli.cs.vsb.cz/c/help/iostream.htm
seek_dir 是一个枚举常量,
如果找不到的话,
从 VS 中拷贝这个枚举定义过来看看 ······
seek_dir 是一个枚举常量,
如果找不到的话,
从 VS 中拷贝这个枚举定义过来看看 ······
#5
呃,是seekdir
#1
可能是模板嵌套的问题
#2
模板里面的模板用typedef试试
#3
至少,你先把seekdir搞定掉。按编译提示,它不在ios里,你就去找出正确的地方呀。
你现在写的代码肯定是强烈耦合于VS2005的私有实现了。
你现在写的代码肯定是强烈耦合于VS2005的私有实现了。
#4
http://poli.cs.vsb.cz/c/help/iostream.htm
seek_dir 是一个枚举常量,
如果找不到的话,
从 VS 中拷贝这个枚举定义过来看看 ······
seek_dir 是一个枚举常量,
如果找不到的话,
从 VS 中拷贝这个枚举定义过来看看 ······
#5
呃,是seekdir