VC6和模板错误

我正在重载运算符<<为类实现类似接口的流:
template<typename T>
CAudit& operator <<  ( const T& data ) {
    audittext << data;
    return *this;
}

CAudit& operator << ( LPCSTR data ) {
    audittext << data;
    return *this;
}
模板版本无法使用“致命错误C1001:INTERNAL COMPILER ERROR(编译器文件'msc1.cpp',第1794行)”进行编译。非模板函数都可以正确编译。 这是因为VC6在处理模板时存在缺陷,是否有办法解决这个问题? 谢谢, 帕特里克 编辑: 全班是:
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;
};
运算符的函数重载&lt;&lt;&lt;&lt;&lt;&lt;用于允许write()用作流操纵器:
CAudit audit
audit << "Billy" << write;
    
已邀请:
对于好的旧Visual Studio 6来说,函数指针模板的重载肯定太过分了。 作为一种解决方法,您可以为操纵器和重载操作符定义类型&lt;&lt;对于那种类型。 这是一些代码:
#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;
}
    
这种错误肯定看起来像VC6之前标准的模板实现引起的那种崩溃。 当然最好的建议是升级到VC7.0,7.1,8.0,9.0或beta 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
成员。     
 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;
}
    
问题似乎不在您发布的代码段中。这个程序工作正常:
#include "stdafx.h"
#include <string>
#include <iostream>
#include <sstream>
#include <windows.h>

class CAudit {

std::ostringstream audittext;

public:
std::string getAuditText() const { return audittext.str(); }

template<typename T>
    CAudit& operator <<  ( const T& data ) {
    audittext << data;
    return *this;
}


CAudit& operator <<  ( int data ) {
    audittext << data;
    return *this;
}

CAudit& operator << ( LPCSTR data ) {
audittext << data;
return *this;
}
};


int main(int argc, char* argv[])
{
CAudit a;
int i = 123;
const char * s = "abc";

a << i;
a << s;

std::cout << "audittext is: '" << a.getAuditText() << "'n";
return 0;
}
你能发布更多代码吗?     

要回复问题请先登录注册