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

C++类的定义问题。。。。

thlgood 发布于 2011-09-30 00:43, 1376 次点击
我听说类一般声明在.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


谢谢各位
8 回复
#2
succubus2011-09-30 01:42
digclass.h:17:31: 错误:为构造函数指定返回值无效
digclass.cpp:78:6: 错误:‘ShowDig’未声明

这写的不是很明确的嘛
头文件里void DigClass(const char Num);改成DigClass(const char Num);
实现里void ShowDig::DigClass(const int n)改成void DigClass::ShowDig(const int n)
#3
thlgood2011-09-30 13:51
我改了,还是有错啊
#4
thlgood2011-09-30 13:57
已经改对了
#5
xiaoyuer4032011-09-30 14:06
改了之后报的什么错?贴来看看
#6
GeneralJing2011-09-30 15:29
数据成员up,down,middle不能作为变量直接输出吧 我觉得不应该这样直接作为变量输出,所以才会说up,down,middle没有定义
#7
thlgood2011-09-30 15:41
感谢各位的回答,问题我已经解决了。

我把改好的代码贴上来吧:

============================digclass.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>

void DigClass::SetDig(const char Num)
{

#ifdef _DEBUG_
    cout << "please input num without other character\n";
    exit (1);
#endif
    switch (Num)
    {
        *************省略*****************
    }
}


void DigClass::ShowDig(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
    }
}
#8
thlgood2011-09-30 15:41
其实我是在学svn。。只不过顺便学C++一下啦。。。
#9
pangding2011-10-01 10:53
svn 是值得学学,只不过没想到 c++ 还能顺便学学……
svn 用于什么语言都行呀,你可以选择自己熟悉的嘛。
1