菜鸟需要老鸟指点迷津,急啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 加一句 int String::count = 0; 因为这是静态变量。另外!你反复为str分配内存(new ...),这是不行的!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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;//在你的类定义后面加上这条语句就可以了这条语句的目的是对类的静态变量进行说明 有时候会提示找不到预编译头,可以在cpp开头加一句#include "stdafx.h"如果你的vc确实有些环境变量没有设置好,可以运行VCVARS32.BAT在目录Program Files\Microsoft Visual Studio\VC98\Bin TO :homecpp(Zhiyu) 好像就是Count不对!!!! 加入static int String::count = 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;}你看看。 #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;}你看看。 error C2720: 'count' : 'static ' storage-class specifier illegal on membersError executing cl.exe. 对不起,前两天没上网但是:homecpp(Zhiyu)好象还有问题,我把你给我的两个CPP,一个H,一个DSP(?不是DWS)加入了还有三个错:Deleting intermediate files and output files for project 'hjgh - Win32 Debug'.--------------------Configuration: hjgh - Win32 Debug--------------------Compiling...hjgh.cppStdAfx.cppLinking...nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadexnafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadexDebug/hjgh.exe : fatal error LNK1120: 2 unresolved externalsError executing link.exe.hjgh.exe - 3 error(s), 0 warning(s) 是否有支持Vista的关机提示机制 求遍历ip段ip地址算法. 没有64位的msxml4.dll? 高分请求各位支援,下周二要考试了!!搞不定!这几个vc题!! 这样的定义是什么意思? 我需要做一个报表程序,要求用一张软盘就能装下应用程序和数据库,而且要有表格,我想用access数据库加DBGrid控件,但不知分发给用户时是否要 这么做一个PDF文件! 求助!不能正常画直线 请问GradientFill怎么用。 mfc ole拖拽问题 ATL里面的类指针是多少位的?要有点论据哦! 在这里怎么发布共亨软件?
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;//在你的类定义后面加上这条语句就可以了
这条语句的目的是对类的静态变量进行说明
#include "stdafx.h"如果你的vc确实有些环境变量没有设置好,可以运行VCVARS32.BAT
在目录Program Files\Microsoft Visual Studio\VC98\Bin
#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;
}你看看。
#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;
}你看看。
Error executing cl.exe.
但是: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)