【问题标题】:Class definition- two files类定义 - 两个文件
【发布时间】:2012-04-30 17:47:14
【问题描述】:

我尝试在课堂上创建新的学生对象。 我对类主体(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)

student.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;

}

student.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;
};

【问题讨论】:

    标签: c++ class char


    【解决方案1】:

    您在 cpp 文件中的构造函数与标头中的构造函数不匹配。 cpp中的每一个构造函数/析构函数/方法的实现都应该先在class的header中定义。

    如果你想拥有 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;
    };
    

    【讨论】:

      【解决方案2】:

      在你的头文件中,你声明Student只有一个带有所有写入参数的构造函数,但没有默认的Student()构造函数,你应该将它添加到头文件中:

      class Student {
        Student();
        Student(char* imie, char* nazwisko ... ) {}
      };
      

      【讨论】:

        【解决方案3】:

        您为不带任何参数的 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 
        

        解决方法是在文件末尾添加换行符! :-)

        【讨论】:

        • 您错过了他在多参数构造函数的标头中有{ }。这样一个确实有定义,而cpp中的无参数一在头文件中没有匹配的声明。
        • 现在我明白我的错误了。现在一切正常。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多