qq_23115189 2017-01-01 13:07 采纳率: 0%
浏览 970
已结题

各位大神,小弟有的一个C++的问题,是关于类函数的使用和该类的全局变量使用冲突的问题

C++代码如下,求各位大神支招,其实应该很简单,还是我忘了一些语法~~代码什么的都不重要,就开头那里的几句关于提前声明类和定义该类的全局变量冲突了~~
#include
#include
#include
#include
#include
#include

using namespace std;

bool flag = true;

char G_name[20] = "";

class Student;

Student *head = new Student(G_name,0,0);

class Student
{
public:
Student(char *name, int number, int age)
{
int i = 0;

    for (i = 0;i < sizeof(name);i++)
    {
        this->m_name[i] = name[i];
    }

    this->m_number = number;

    this->m_age = age;
}

~Student()
{
    cout << "删除该学生信息完毕" << endl;
}

void AddHead(Student *std)
{
    cout << "请输入你想要插入的学生的姓名" << endl;

    cin >> std->m_name;

    cout << "请输入你想要插入的学生的学号" << endl;

    cin >> std->m_number;

    cout << "请输入你想要插入的学生的年龄" << endl;

    cin >> std->m_age;

    std->p = head;

    head->p = NULL;

    head = std;

    std = NULL;
}

void AddTail(Student *std)
{
    cout << "请输入你想要插入的学生的姓名" << endl;

    cin >> std->m_name;

    cout << "请输入你要插入的学生的学号" << endl;

    cin >> std->m_number;

    cout << "请输入你要插入的学生的年龄" << endl;

    cin >> std->m_age;

    Student *ff = NULL;

    ff = head;

    while (ff->p != NULL)
    {
        ff = ff->p;
    }

    ff->p = std;

    std->p = NULL;
}

void Print()
{
    Student *ff = NULL;

    ff = head;

    cout << "学生年龄" << "     " << "学生姓名" << "     " << "学生学号" << endl;

    while (ff->p != NULL)
    {
        cout << ff->m_age;
        cout << "     ";
        cout << ff->m_name;
        cout << "     ";
        cout << ff->m_number << endl;
    }
    cout << ff->m_age;
    cout << "     ";
    cout << ff->m_name;
    cout << "     ";
    cout << ff->m_number << endl;
}

void Delete()
{
    Student *ff = NULL;

    Student *fx = NULL;

    ff = head;

    int number = 0;

    cout << "请输入你要删除的学生的学号" << endl;

    cin >> number;

    while (ff->m_number != number)
    {
        ff = ff->p;
    }

    fx = ff;

    ff = ff->p;

    free(fx);

    fx = NULL;
}

void Modify()
{
    Student *ff = NULL;

    Student *fx = NULL;

    ff = head;

    int number = 0;

    cout << "请输入你要修改的学生的学号" << endl;

    cin >> number;

    while (ff->m_number != number)
    {
        ff = ff->p;
    }

    cout << "请输入修改后的学生姓名" << endl;

    cin >> ff->m_name;

    cout << "请输入修改后的学生学号" << endl;

    cin >> ff->m_number;

    cout << "请输入修改后的学生年龄" << endl;

    cin >> ff->m_age;
}

void Exit()
{
    cout << "您好,您已经退出" << endl;
}

private:
char m_name[20];
int m_number;
int m_age;
Student *p = NULL;
};

void print()
{
cout << "请输入您想要执行的命令的选项" << endl;
cout << "1.我想前插入一个学生信息" << endl;
cout << "2.我想后插入一个学生的信息" << endl;
cout << "3.我想打印所有当前学生的信息" << endl;
cout << "4.我想删除某位学生的信息" << endl;
cout << "5.我想修改某位学生的信息" << endl;
cout << "6.我什么也不想做,直接退出" << endl;

}

void Execute()
{
int a = 0;

cin >> a;

char name[20] = "";

int number = 0;

int age = 0;

Student *stud = new Student(name, number, age);

switch (a)
{
    case 1:
        head->AddHead(stud);
        break;
    case 2:
        head->AddTail(stud);
        break;
    case 3:
        head->Print();
        break;
    case 4:
        head->Delete();
        break;
    case 5:
        head->Modify();
        break;
    case 6:
        flag = false;
        break;
default:
    break;
}

}

int main(void)
{
while (flag)
{
print();

    Execute();
}
system("pause");

return 0;

}
最后编译器就给我一句话:“Student”:类没有构造函数。该如何解决啊~~大神们

  • 写回答

7条回答

  • oyljerry 2017-01-01 13:14
    关注

    把Student的类定义放到一个单独头文件,然后再用的地方包含。

    评论

报告相同问题?

悬赏问题

  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题