注册 登录
编程论坛 C++教室

【求助】新手学习C++,第一个程序有问题

tanghuawei 发布于 2007-11-27 16:03, 2254 次点击
#include  <afxcoll.h>    //Provides Access to MFC functions
Class CDrawBox : public CObject
{
public :
// Draws the box.
void DoDraw(char * string);
}
void CDrawBox :: DoDraw(char * cValue)
{
int      iCount;    //Loop counter
int      iSpace;    //Amount of spaces to add for string.
// Draw the top of the box
fprintf(stdout,"\311");
for (iCount = 1;  iCount <= 78 ; iCount ++)
{     
  fprintf(stdout,"\315");
}
fprintf(stdout,"\273");
// Figure out the center of the string, then display it with the box sides.
iSpace = (80 - strlen(cValue)) / 2;
fprintf(stdout,"\272");
for (iCount = 1;  iCount <=  iSpaces ; iCount ++)
{     
  fprintf(stdout,"  ");
}
fprintf(stdout, "%s", cValue);
// Compensate for odd sized strings, then complete the side.
if ((strlen(cValue) % 2) == 1)
{     
  iSpaces --;
}
for (iCount = 1;  iCount <=  iSpaces ; iCount ++)
{     
  fprintf(stdout, "  ");
}
fprintf(stdout,"\272");
// Draw the bottom of the box
fprintf(stdout, "\310");
for (iCount = 1;  iCount <= 78 ; iCount ++)
{     
  fprintf(stdout, "\315");
}
fprintf(stdout, "\274\n");
}
int main(int  argc, char **  argv)
{
char *     cName;    // Name of person typed at command line.
char *     cLocale;   // Program execution location.
CTime     oMyTime;  // A time object.
CString     cDate;    // String used to hold time and date.
CDrawBox  oMyDraw;  // Special text display.
// See if we have enough command line arguments.
if ( argc != 2)
{     
  fprintf(stderr, "Type the program name followed by your name.\n");     
  return 1;
}
// Get the command line arguments
cLocale = argv[0];
cName = argv[1];
// Get the current time and put it in a string.
oMyTime = CTime::GetCurrentTime();
cDate = oMyTime.Format( "%A, %B %d,%Y" );
//Display everything we've collected.
fprintf(stdout, "Hello %s\n\n", cName);
fprintf(stdout, "Program is executing from: \n%s\n\n", cLocale);
fprintf(stdout, "The date is: %s\n", cDate);
// Use our class to draw a box around some text.
oMyDraw.DoDraw("It's a box!");
return 0;
}

为什么第二句老提示错误,我用的是VC++6.0

错误信息:
--------------------Configuration: Console - Win32 Debug--------------------
Compiling...
Console.cpp
D:\Jason\C++\Visual C++6.0\Examples\Console\Console.cpp(2) : error C2146: syntax error : missing ';' before identifier 'CDrawBox'
D:\Jason\C++\Visual C++6.0\Examples\Console\Console.cpp(2) : error C2501: 'Class' : missing storage-class or type specifiers
D:\Jason\C++\Visual C++6.0\Examples\Console\Console.cpp(2) : fatal error C1004: unexpected end of file found
Error executing cl.exe.
Console.exe - 3 error(s), 0 warning(s)

[[italic] 本帖最后由 tanghuawei 于 2007-11-27 16:04 编辑 [/italic]]
10 回复
#2
yuyunliuhen2007-11-27 16:30
1.Class->  class  //关键字大小写问题
2。
class CDrawBox : public CObject
{
public:
// Draws the box.
void DoDraw(char * string);
};//少了个“;”

3。iSpaces,iSpace  //可能某些地方少写s,统一一下
#3
tanghuawei2007-11-27 16:48
谢谢你的回复
我改好之后,运行怎么命令栏窗口一闪就没了,然后出现一个红点在oMyDraw.DoDraw("It's a box!");
左边,什么原因啊,下面是出现的信息:
Loaded 'ntdll.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\kernel32.dll', no matching symbolic information found.
Loaded symbols for 'C:\WINDOWS\system32\MFC42D.DLL'
Loaded symbols for 'C:\WINDOWS\system32\MSVCRTD.DLL'
Loaded 'C:\WINDOWS\system32\gdi32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\user32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\shimeng.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\imm32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\advapi32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\rpcrt4.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\secur32.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\lpk.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\usp10.dll', no matching symbolic information found.
Loaded 'C:\WINDOWS\system32\msvcrt.dll', no matching symbolic information found.
The thread 0x370 has exited with code 1 (0x1).
The program 'D:\Jason\C++\Visual C++6.0\Examples\Console\Debug\Console.exe' has exited with code 1 (0x1).
#4
yuyunliuhen2007-11-27 16:59
这些对你的程序调试是没影响的。
C++编译程序之后会产生一个pdb文件,包含的信息使得程序调试可以edit and continue,但是类似于.pdb这样一类调试辅助的文件在正式发布的程序模块里都是没有的。所以当你调试程序、载入这些模块时,就会报告这些模块没有调试需要的符号信息。
#5
tanghuawei2007-11-27 17:03
请教一下,为什么程序一闪而过,我用Ctrl+F5,可以显示命令框:Type the program name followed by your name,然后提示按任意键继续,就没了,为什么不执行下面的语句?
#6
忘记喧嚣2007-11-28 22:53
哈哈你点错了键 应该点 右上角的 红色感叹号  也就是运行

你点的是调试所以 一闪就没了 然后出现这些东西

我前段时间和你一样的错误  加油呵呵
#7
tanghuawei2007-12-03 10:42
這個程序運行應該是圖中的結果,但是我的只出現Type the program name followed by your name.然后是按任意鍵結束的信息,并沒出現途中那樣的結果,是為什么?
#8
elynis2008-01-04 14:31
我遇到的和楼主一样的问题,都是显示按任意键就结束了,并没有出现书中图里面的一个BOX,里面有文字的现象,也想知道为什么?
#9
sunkaidong2008-01-04 15:08
我没遇到这些问题,不过帮你顶一下..
#10
jockysir2008-07-29 00:16
我是根据下面语句建立一个.bat文件,放在编译好的程序文件夹下,运行.bat就可以看见了
@ ECHO  OFF
D:\VC\CONSOLE\DEBUG\CONSOLE         //D:\VC\CONSOLE\DEBUG\CONSOLE为程序文件的绝对路径
IF ERRORLEVEL==1 ECHO IT'S BAD
D:\VC\CONSOLE\DEBUG\CONSOLE JOHN
IF ERRORLEVEL==1 ECHO IT'S BAD
PAUSE
@ ECHO  ON

[[it] 本帖最后由 jockysir 于 2008-8-8 15:20 编辑 [/it]]
#11
zhanghj2008-07-29 18:04
为什么改了之后出现这些信息:
Linking...
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/shiyan3.exe : fatal error LNK1120: 2 unresolved externals
Error executing link.exe.

shiyan3.exe - 3 error(s), 0 warning(s)
1