2012-04-30 38 views
1

我尝试创建班级,在那里我可以创建学生的新对象。 我有类定义体(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; 
}; 

回答

0

你写的学生构造一个机构,不带任何参数:

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 

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

+1

您错过了他在多参数构造函数的头文件中拥有'{}'。因此,一个*有*定义,而cpp中的no参数在头文件中没有匹配的声明。 – crashmstr

+0

现在我明白我的错误了。现在一切正常。 – mathewM

1

在你头文件,你声明Student只有一个构造函数的所有书面参数,但没有默认构造函数Student(),你应该把它添加到标题:

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

你在CPP文件的构造不不匹配头中的构造函数。 cpp中的每个构造函数/析构函数/方法实现都应该首先在头文件的类中定义。

如果你想有2个构造函数 - 1没有参数,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; 
};