首页 > 解决方案 > 该程序的概念是创建一个抽象文件并链接到实现文件,但我收到一条错误消息,提示“学生”不是名称类型

问题描述

**Student.h**

** //这是抽象文件的代码** // Student - 一个基本 的 Student 抽象文件将链接到实现文件以创建一个显示学生姓名和 id 的程序

#ifndef _STUDENT_
#define _STUDENT_
namespace Schools
{
class Student
{
public:
Student(char* pszName, int nID);
virtual char* display()
protected:

** //学生的名字**

char* pszName;
int nID;
};
}
#endif

**//Student.cpp**
**// this is the code of the implementation file**

**// Student - implementation of student**

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include "Student.h"
namespace Schools
{
Student::Student(char* pszNameArg, int nIDArg): nID(nIDArg)
//create an array
pszName = new char[strlength(pszNameArg) + 1];
strcpy(pszName, pszNnameArg);
}

**// 显示函数 - 显示学生的描述 **

char* Student::display()

{


pReturn = new char[strlength(pszName) + 1];

strcpy(pReturn, pszName);



 return pReturn;
}

在此处输入代码

标签: c++

解决方案


如果您只是删除明显的拼写错误和错误,您的代码就可以工作。正如@Pete Becker 建议的那样,遵循包含保护的正确名称约定,以 开头的名称_由实现使用。HEADERNAME_H_通常使用如下所示的内容。

// Student.h
#ifndef STUDENT_H_
#define STUDENT_H_
namespace Schools
{
class Student
{
public:
    Student(char* pszName, int nID);
    virtual char* display();
protected:
    char* pszName;
    int nID;
};
}
#endif
// Student.cpp
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include "Student.h"
namespace Schools 
{
Student::Student(char* pszNameArg, int nIDArg): nID(nIDArg)
{
    //create an array
    pszName = new char[strlen(pszNameArg) + 1];
    strcpy(pszName, pszNameArg);
}

char* Student::display()
{
    char* pReturn = new char[strlen(pszName) + 1];
    strcpy(pReturn, pszName);
    return pReturn;
}
}

但是,当您甚至没有通过自己的代码时就直接提出问题是不好的。


推荐阅读