加一句 int String::count = 0; 因为这是静态变量。
另外!你反复为str分配内存(new ...),这是不行的!!!!

解决方案 »

  1.   

    class String {
        char *str;
        static int count;
    public:
        String() { count++; str = new char; *str = 0; }
        String( char * );
        ~String() { delete str; }
        operator char *();
        int operator < (String s) { return strcmp(str, s.str) < 0; }
        void print() { printf("%s", str); }
        
        friend int operator < (String, String);
        friend void report() ;
    };int String::count;//在你的类定义后面加上这条语句就可以了
    这条语句的目的是对类的静态变量进行说明
      

  2.   

    有时候会提示找不到预编译头,可以在cpp开头加一句
    #include "stdafx.h"如果你的vc确实有些环境变量没有设置好,可以运行VCVARS32.BAT
    在目录Program Files\Microsoft Visual Studio\VC98\Bin
      

  3.   

    TO :homecpp(Zhiyu) 好像就是Count不对!!!!
      

  4.   

    加入static int String::count = 5(?)
      

  5.   


    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>class String
    {
    char *str;
    static int count;
    public:
    String() { count++; str = new char[1]; str[0] = 0; }
    String( char * );
    ~String() { delete str; }
    operator char *();
    int operator < (String &s) { return strcmp(str, s.str) < 0; }
    void print() { printf("%s", str); }

    friend int operator < (String &, String &);
    friend void report() ;
    };int String::count = 0;String::String(char *s)
    {
            count++;
            str = new char[strlen(s) + 1];
            strcpy(str, s);
    }String::operator char*() {
            char *p = new char[strlen(str) + 1];
            strcpy (p, str);
            return p;        //use only a copy of str for protection
    }int operator < (String &s1, String &s2)
    {
          return strcmp (s1.str, s2.str);
    }void report() 
    {
          printf("Report on String usage: " );
          printf(" %d Strings created\n", String::count);
    }void main()
    {
            String s0 ;
            String s1 ("s1 == \"Hello C\"\n");
            String s2 ("s2 == \"Hello C++\"\n");
            String *sp = new String( "Hello world!" );
            
            char *cp = (char *) *sp;        //same as *cp = *sp 
    char *pc = (char *) *sp;        if ( !strcmp(cp, pc) )
                  printf("char *cp == (char *) *sp == \"%s\"\n", cp); delete cp;
    delete pc;        s1.print();
            s2.print();        if(s1.operator < (s2))
                  printf("s1.operator < (s2)\n");
            if(operator < (s1, s2))
                  printf("operator < (s1, s2)\n");
            report(); delete sp;
    }你看看。
      

  6.   


    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>class String
    {
    char *str;
    static int count;
    public:
    String() { count++; str = new char[1]; str[0] = 0; }
    String( char * );
    ~String() { delete str; }
    operator char *();
    int operator < (String &s) { return strcmp(str, s.str) < 0; }
    void print() { printf("%s", str); }

    friend int operator < (String &, String &);
    friend void report() ;
    };int String::count = 0;String::String(char *s)
    {
            count++;
            str = new char[strlen(s) + 1];
            strcpy(str, s);
    }String::operator char*() {
            char *p = new char[strlen(str) + 1];
            strcpy (p, str);
            return p;        //use only a copy of str for protection
    }int operator < (String &s1, String &s2)
    {
          return strcmp (s1.str, s2.str);
    }void report() 
    {
          printf("Report on String usage: " );
          printf(" %d Strings created\n", String::count);
    }void main()
    {
            String s0 ;
            String s1 ("s1 == \"Hello C\"\n");
            String s2 ("s2 == \"Hello C++\"\n");
            String *sp = new String( "Hello world!" );
            
            char *cp = (char *) *sp;        //same as *cp = *sp 
    char *pc = (char *) *sp;        if ( !strcmp(cp, pc) )
                  printf("char *cp == (char *) *sp == \"%s\"\n", cp); delete cp;
    delete pc;        s1.print();
            s2.print();        if(s1.operator < (s2))
                  printf("s1.operator < (s2)\n");
            if(operator < (s1, s2))
                  printf("operator < (s1, s2)\n");
            report(); delete sp;
    }你看看。
      

  7.   

    error C2720: 'count' : 'static ' storage-class specifier illegal on members
    Error executing cl.exe.
      

  8.   

    对不起,前两天没上网
    但是:homecpp(Zhiyu)好象还有问题,我把你给我的两个CPP,一个H,一个DSP(?不是DWS)加入了还有三个错:
    Deleting intermediate files and output files for project 'hjgh - Win32 Debug'.
    --------------------Configuration: hjgh - Win32 Debug--------------------
    Compiling...
    hjgh.cpp
    StdAfx.cpp
    Linking...
    nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
    nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
    Debug/hjgh.exe : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe.hjgh.exe - 3 error(s), 0 warning(s)