我重载了运算符<<,为类实现了一个类似流的接口:
template<typename T>
CAudit& operator << ( const T& data ) {
audittext << data;
return *this;
}
CAudit& operator << ( LPCSTR data ) {
audittext << data;
return *this;
}模板版本无法编译,并显示"fatal error C1001: INTERNAL COMPILER ERROR (编译器文件'msc1.cpp',第1794行)“。非模板函数都可以正确编译。
这是因为在处理模板时VC6s的缺陷吗?有什么方法可以解决这个问题吗?
谢谢,帕特里克
编辑:
完整的类是:
class CAudit
{
public:
/* TODO_DEBUG : doesn't build!
template<typename T>
CAudit& operator << ( const T& data ) {
audittext << data;
return *this;
}*/
~CAudit() { write(); }//If anything available to audit write it here
CAudit& operator << ( LPCSTR data ) {
audittext << data;
return *this;
}
//overload the << operator to allow function ptrs on rhs, allows "audit << data << CAudit::write;"
CAudit& operator << (CAudit & (*func)(CAudit &))
{
return func(*this);
}
void write() {
}
//write() is a manipulator type func, "audit << data << CAudit::write;" will call this function
static CAudit& write(CAudit& audit) {
audit.write();
return audit;
}
private:
std::stringstream audittext;
};该问题发生在操作符<<的函数重载中,该操作符用于允许将write()用作流操纵器:
CAudit audit
audit << "Billy" << write;发布于 2009-09-08 02:27:22
对于老的Visual Studio6来说,函数指针模板的重载肯定是太多了。作为一种变通办法,你可以为你的操纵器定义一个类型,并为该类型重载operator<<。下面是一些代码:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <sstream>
#include <windows.h>
class CAudit {
std::ostringstream audittext;
void do_write() {}
public:
~CAudit() { do_write(); }
// types for manipulators
struct Twrite {};
// manipulators
static Twrite write;
// implementations of <<
template<typename T>
CAudit& operator << ( const T& data ) {
audittext << data;
return *this;
}
CAudit& operator << ( LPCSTR data ) {
audittext << data;
return *this;
}
CAudit& operator << ( Twrite& ) {
do_write();
return *this;
}
};
// static member initialization
CAudit::Twrite CAudit::write;
int main(int argc, char* argv[])
{
CAudit a;
int i = 123;
const char * s = "abc";
a << i << s << CAudit::write;
return 0;
}发布于 2009-09-07 11:44:55
这种错误看起来绝对像是由VC6的前标准模板实现引起的那种崩溃。
当然,最好的建议是升级到VC7.0、7.1、8.0、9.0或测试版10。与Windows版本相比,当Me、2000、XP、Vista和7可用时,它仍在使用Windows 98。
话虽如此,您可以通过一个简单的技巧大大简化查找:
class CAudit {
template<typename T>
CAudit& operator<<(T const& t) {
this->print(t);
return *this;
}
private:
void print(int);
void print(LPCSTR);
void print(CAudit & (*func)(CAudit &));
template<typename T> print(T const&);
};这里的希望是,第一次查找operator<<就能找到单个成员模板。其他operator<<候选者是其他类和内置的非成员。它们应该明确地比这个模板更糟糕。在operator中的第二次查找只需要处理名为print的CAudit成员。
发布于 2009-09-07 08:56:13
template<typename T>
CAudit& operator << (T data ) {
audittext << data;
return *this;
}编辑:
#include <iostream>
using namespace std;
class CAudit{
public:
CAudit(){}
template< typename T >
CAudit &operator<<(T arg);
CAudit &operator<<(char s);
};
template< typename T>
void oldLog(T arg){
cout << arg;
}
template< typename T >
CAudit &CAudit::operator<<(T arg){
oldLog( arg );
return *this;
}
CAudit &CAudit::operator<<(char arg){
oldLog( arg );
return *this;
}
int main(){
CAudit e;
e << "Hello";
e << 'T';
return 0;
}https://stackoverflow.com/questions/1388230
复制相似问题