C++类的定义问题。。。。
我听说类一般声明在.h文件中。定义在cpp文件中。于是我写了这样一个类和这样一个头文件但是编译的时候说cpp文件内有语法错误。。。我不是很懂C++语法,大家帮我看一下这两个文件哪里的语法错了
===================错误内容========================
In file included from digclass.cpp:6:0:
digclass.h:17:31: 错误:为构造函数指定返回值无效
digclass.cpp:78:6: 错误:‘ShowDig’未声明
digclass.cpp: 在函数‘void DigClass(int)’中:
digclass.cpp:83:17: 错误:‘up’在此作用域中尚未声明
digclass.cpp:86:17: 错误:‘middle’在此作用域中尚未声明
digclass.cpp:97:17: 错误:‘down’在此作用域中尚未声明
digclass.h:17:31: 错误:为构造函数指定返回值无效
==============================cpp文件===================================
程序代码:
/* *Filename: digclass.cpp *Created: 2011年09月29日 21时19分53秒 *Author: thlgood */ #include "digclass.h" #include <stdio.h> #include <stdlib.h> #include <string.h> DigClass::DigClass(const char Num) { #ifdef _DEBUG_ cout << "please input num without other character\n"; exit (1); #endif switch (Num) { case '0': strcpy(up, " _ "); strcpy(middle, "| |"); strcpy(down, "|_|"); break; case '1': strcpy(up, " "); strcpy(middle, " |"); strcpy(down, " |"); break; case '2': strcpy(up, " _ "); strcpy(middle , " _|"); strcpy(down, "|_ "); break; case '3': strcpy(up, " _ "); strcpy(middle," _|"); strcpy(down, " _|"); break; case '4': strcpy(up, " "); strcpy(middle, "|_|"); strcpy(down, " |"); break; case '5': strcpy(up, " _ "); strcpy(middle, "|_ "); strcpy(down, " _|"); break; case '6': strcpy(up, " _ "); strcpy(middle, "|_ "); strcpy(down, "|_|"); break; case '7': strcpy(up, " _ "); strcpy(middle, " |"); strcpy(down, " |"); break; case '8': strcpy(up, " _ "); strcpy(middle, "|_|"); strcpy(down, "|_|"); break; case '9': strcpy(up, " _ "); strcpy(middle, "|_|"); strcpy(down, " _|"); break; } } void ShowDig::DigClass(const int n) { switch(n) { case 1: printf("%s", up); break; case 2: printf("%s", middle); break; #ifdef _DEBUG_ case 3: printf("%s", down); break; default: printf("Error, Not UP, Not Middle, NOT DOWN!!\n"); exit(2); #else default: printf("%s", down); #endif } }
======================END================
=========================头文件=================================
程序代码:
/* * Filename: DigClass.h * Description: Nothing * Created: 2011年09月29日 21时16分40秒 * Author: thlgood, */ #ifndef _DITCLASS_H_ #define _DIGCLASS_H_ class DigClass { private: char up[4]; char middle[4]; char down[4]; public: void DigClass(const char Num); void ShowDig(const int n); }; #endif
=====================END=======================
============================说明=============================
我之前是学C语言的,目前是C++初学者。
所以代码的风格明显和C比较接近。。不过不碍事的。只是帮我改一下语法错误就好了。
我的平台是GNU/Linux Gcc
谢谢各位