c++ - 类定义-两个文件

标签 c++ class char

我尝试创建一个可以创建学生新对象的类。 我在定义类主体 (student.cpp) 和类 (student.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 文件中的构造函数与 header 中的构造函数不匹配。 cpp 中的每个构造函数/析构函数/方法实现都应首先在 header 的类中定义。

如果你想有 2 个构造函数 - 1 个没有参数,一个有很多参数。您需要在 header 中添加构造函数的定义。

//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;
};

关于c++ - 类定义-两个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10388138/

相关文章:

c - 指向结构体中 char 的指针,段错误

c++ - 以buffer为参数的类函数,一头雾水

c++ - POSIX 套接字和 BSD 套接字有什么区别?

c++ - Visual C++ 2010 原子类型支持?

c++ - 非模板函数中的 std::forward

java - 无法在类对象的 ArrayList 中存储值。 (代码已编辑)

c++ - 如何在多态 C++ 中实现这个类?

python - 类格式问题

java - 计算文件中每个字符的个数

C++ _popen()/system() 在 Windows 上运行连续命令