c++ - 该程序的概念是创建一个抽象文件并链接到实现文件,但我收到一条错误消息,提示“学生”不是名称类型
问题描述
**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;
}
在此处输入代码
解决方案
如果您只是删除明显的拼写错误和错误,您的代码就可以工作。正如@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;
}
}
但是,当您甚至没有通过自己的代码时就直接提出问题是不好的。
推荐阅读
- javascript - 语法错误,将 php 嵌入到 javascript 时出现意外“”
- r - 使用线性判别分析来执行特征提取观察的类标签被改变
- java - 在 Spring 中使用默认验证器进行级联验证
- java - 需要服务但在构造函数中找不到
- google-app-engine - 部署到 Google App Engine 时出错
- c++ - 当我至少有一个参数时,为什么 GCC 声称我违反了“可变参数宏的至少一个参数”?
- c# - 需要 Razor Pages 模型绑定 [BindProperty]?
- c# - 引用同名的DLL
- reactjs - 移动到云端 + SSL 后,应用程序中的 React 路由器链接断开
- python - 为什么加载数据集时会出现 Unicode 解码错误消息?