类定义 - 两个文件
Class definition- two files
我尝试制作可以创建学生新对象的类。我对类主体(学生.cpp)和类(学生.h)的定义有一些问题。
Error:
In file included from student.cpp:1:
student.h:21:7: warning: no newline at end of file
student.cpp:6: error: prototype for `Student::Student()' does not match any in class `Student'
student.h:6: error: candidates are: Student::Student(const Student&)
student.h:8: error: Student::Student(char*, char*, char*, char*, int, int, bool)
学生.cpp
//body definition
#include "student.h"
#include <iostream>
Student::Student()
{
m_imie = "0";
m_nazwisko = "0";
m_pesel = "0";
m_indeks = "0";
m_wiek = 0;
m_semestr = 0;
m_plec = false;
}
学生.h
//class definition without body
#include <string.h>
class Student {
//konstruktor domyslny
Student (char* imie, char* nazwisko, char* pesel, char* indeks, int wiek, int semestr, bool plec):
m_imie(imie), m_nazwisko(nazwisko), m_pesel(pesel), m_indeks(indeks), m_wiek(wiek), m_semestr(semestr), m_plec(plec)
{}
private:
char* m_imie;
char* m_nazwisko;
char* m_pesel;
char* m_indeks;
int m_wiek;
int m_semestr;
bool m_plec;
};
cpp 文件中的构造函数与标头中的构造函数不匹配。cpp 中的每个构造函数/解构函数/方法实现都应首先在标头中的类中定义。
如果你想有 2 个构造函数 - 1 个没有参数,一个有很多参数。您需要在标头中添加构造函数的定义。
//class definition without body
#include <string.h>
class Student {
//konstruktor domyslny
Student (char* imie, char* nazwisko, char* pesel, char* indeks, int wiek, int semestr, bool plec):
m_imie(imie), m_nazwisko(nazwisko), m_pesel(pesel), m_indeks(indeks), m_wiek(wiek), m_semestr(semestr), m_plec(plec)
{} //here really implementation made
Student(); //one more constructor without impementation
private:
char* m_imie;
char* m_nazwisko;
char* m_pesel;
char* m_indeks;
int m_wiek;
int m_semestr;
bool m_plec;
};
在头文件中,您声明Student
只有一个构造函数,其中包含所有写入的参数,但没有默认的构造Student()
构造函数,您应该将其添加到标头中:
class Student {
Student();
Student(char* imie, char* nazwisko ... ) {}
};
您为 Student 构造函数编写了一个不接受任何参数的主体:
Student::Student( /* NO PARAMETERS */ )
但是这个函数,Student()
,不在类定义中。
这将生成错误:
prototype for `Student::Student()' does not match any in class `Student'
你需要写:
class Student {
public:
Student(); /* NOW it is declared as well as defined */
[... all the other stuff ...]
};
现在,既有Student()
的原型,也有Student(/* 7 parameters */)
另一个错误的修复很简单:
student.h:21:7: warning: no newline at end of file
解决方法是在文件末尾放置换行符! :-)
相关文章:
- 两个文件使用彼此的功能-如何解决
- 结构包含在两个头文件中,这两个文件我都不拥有
- Levenshtein 两个文件的距离花费了太多时间
- 如何在c ++中以一行(水平)打印两个文件的数据?如给定的.这两种形式来自两个不同的文本文件
- 包括两个文件 C++ 之间的冲突
- 如何检测两个文件条目是否引用与Qt相同的物理文件系统?
- 比较两个文件夹的关系
- 连接两个文件时如何避免冗余尾随换行符?
- 在循环工作时,首先将两个文件读为向量,但仅次于迭代一次
- 比较两个文件中的两个中间人的最佳方法.C++
- 同时逐行阅读两个文件
- 包含两个C++文件的制作文件:如何选择要编译的文件
- 在 c++ 中对两个文件使用标头
- 同时阅读两个文件
- 如何连接两个文件夹/项目的生成文件,这两个文件夹/项目必须一起运行且独立运行
- 从其他两个文件中创建一个分类整数的文件
- C 程序处理两个文件,而不是一个文件
- 知道分割故障在哪里比较两个文件
- 如何计算两个文件中字符单词和行的总数,并将它们相加以得出总数
- 将相同数据写入两个文件的有效方法