代码之家  ›  专栏  ›  技术社区  ›  Patrick

VC6和模板错误

  •  0
  • Patrick  · 技术社区  · 16 年前

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

    模板版本无法编译,出现“致命错误C1001:内部编译器错误(编译器文件'msc1.cpp',第1794行)”。非模板函数都能正确编译。

    谢谢, 帕特里克

    编辑:

    全班是:

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

    CAudit audit
    audit << "Billy" << write;
    
    4 回复  |  直到 15 年前
        1
  •  1
  •   MSalters    16 年前

    对于旧的Visual Studio 6来说,函数指针模板的过载肯定太多了。 作为解决方法,您可以为操纵器和重载运算符定义一个类型<<对于这种类型。

    #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;
    }
    
        2
  •  1
  •   Ralph    16 年前

    当然,最好的建议是升级到VC7.0、7.1、8.0、9.0或10的测试版。

    话虽如此,你可以通过一个简单的技巧来简化查找:

    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 只需要处理 CAudit 成员们打电话 print .

        3
  •  0
  •   KV Prajapati    16 年前
     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;
    }
    
        4
  •  0
  •   Ralph    16 年前

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

    你能再发一些代码吗?

    推荐文章