2012-04-30 6 views
1

Я пытаюсь создать класс, где могу создать новый объект Студента. У меня есть некоторые проблемы с определением класса body (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::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

Вам не хватает того, что он имеет '{}' в заголовке для конструктора с несколькими параметрами. Так что одно * имеет * определение, а параметр no в cpp не имеет объявления соответствия в файле заголовка. – crashmstr

+0

Теперь я понимаю свою ошибку. Теперь все работает отлично. – mathewM

1

В вас заголовок файла вы заявляете, что Student имеет только один конструктор со всеми письменными параметрами по умолчанию, но не Student() конструктор, вы должны добавить его в заголовок:

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

Ваш конструктор в 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; 
}; 
Смежные вопросы