2014-09-25 106 views
0

我在Student.h問題與C++類模板

#ifndef _student_h_ 
#define _student_h_ 

template <class T> 
class Student 
{ 
public: 
    Student(); 
    ~Student(); 
    void setWage(float hourlyWage); 
    void addHours(float hoursWorked); 
    bool pay(); 
    bool writeCheck(float value); 
    float getTotalEarnings(); 
    float getStudentCut(); 
    float getSwauCut(); 
    float getWage(); 
private: 
    float wage; 
    float hours; 
    float swauCut; 
    float studentCut; 
    float totalEarnings; 
}; 
#include "student.tpp" //Works with .tpp, errors with .cpp 

#endif 

下面的代碼作爲即時通訊試圖分開我的代碼,我嘗試下面的代碼放到這兩個一個.cpp和用於測試的.tpp。

#pragma once 
#include "stdafx.h" 
#include "Student.h" 

template <class T> 
Student<T>::Student() 
{ 
    wage = 0.0f; 
    hours = 0.0f; 
    swauCut = 0.0f; 
    studentCut = 0.0f; 
    totalEarnings = 0.0f; 
} 

template <class T> 
Student<T>::~Student() 
{ 
} 

template <class T> 
void Student<T>::setWage(float hourlyWage) 
{ 
    wage = hourlyWage; 
} 

template <class T> 
void Student<T>::addHours(float hoursWorked) 
{ 
    hours += hoursWorked; 
} 
template <class T> 
bool Student<T>::pay() 
{ 
    if (hours == 0 || wage == 0) 
     return false; 
    studentCut += .25*(hours * wage); 
    swauCut += .75*(hours * wage); 
    totalEarnings += hours * wage; 
    hours = 0.0f; 
    return true; 
} 
template <class T> 
bool Student<T>::writeCheck(float value) 
{ 
    if (value < studentCut){ 
     studentCut -= value; 
     return true; 
    } 

    return false; 
} 
template <class T> 
float Student<T>::getTotalEarnings() 
{ 
    return totalEarnings; 
} 
template <class T> 
float Student<T>::getStudentCut() 
{ 
    return studentCut; 
} 
template <class T> 
float Student<T>::getSwauCut() 
{ 
    return swauCut; 
} 
template <class T> 
float Student<T>::getWage() 
{ 
    return wage; 
} 

我的問題是,如果我使用.cpp文件和註釋掉tpp文件,我會遇到各種各樣的錯誤。但是,如果我只是#include Student.tpp文件編譯正常並且工作。我的印象是cpp和tpp相對相同?

的誤差即時得到是:

Error1 error C2995: 'Student<T>::Student(void)' : function template has already been defined c:\users\aurelib.cs\desktop\studentproject\studentproject\student.cpp 13 1 StudentProject 
Error2 error C2995: 'Student<T>::~Student(void)' : function template has already been defined c:\users\aurelib.cs\desktop\studentproject\studentproject\student.cpp 18 1 StudentProject 

....所有的功能。

如果我從.cpp文件中刪除#include "Student.h",我得到語法錯誤。

我正在使用Visual Studios。而且就像我說的,當我在模板的底部輸入#include "Student.tpp"時,我沒有問題。但是當我使用相同的代碼和#include "Student.cpp"

非常感謝!

+0

刪除所有''秒。但是請注意,模板代碼不應該放在'.cpp'文件中,除非它包含在標題中,否則可能會導致更多混淆而不是歡樂。但是,爲什麼您首先將該課程作爲模板?我看不到任何地方使用的模板參數。 – Biffen 2014-09-25 16:59:15

+0

爲什麼'學生'是第一名的模板課?我無法在任何地方找到使用'typename T'的任何聲明。看你在這裏有一個XY問題。 – 2014-09-25 17:02:17

+0

它實際上並不需要。它只是一個概念。該計劃將接受WriteCheck函數中的T對象而不是float。一切正常,我只是想知道爲什麼我不能使用CPP,並被迫進入.tpp。而托馬斯,我通過閱讀這個問題開始了程序的運行。但我不認爲這是解釋爲什麼tpp選擇cpp。以及爲什麼cpp根本不起作用 – 2014-09-25 17:06:09

回答

0

我的猜測是:

當你的名字包含的類成員函數Student.cpp的實現文件,編譯器試圖編譯它。當您將其命名爲Student.tpp時,編譯器不會嘗試對其進行編譯。

在您的文件中,Student.h #include的Student.cppStudent.cpp#include的Student.h。當協處理器充實了Student.cpp的內容時,它最終包含文件的內容兩次。這就是爲什麼你得到function template has already been defined錯誤。

方法來防止它:

  1. 不要將文件命名爲Student.cpp。您可以使用Student.tpp或更具描述性的名稱,Student_Impl.h

  2. 在文件中添加#include警衛,除了使用#pragma once

    #pragma once 
    #ifndef _student_impl_h_ 
    #define _student_impl_h_