C++类模板的问题
我在Student.h中有以下代码C++类模板的问题,c++,visual-studio-2012,C++,Visual Studio 2012,我在Student.h中有以下代码 #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(
#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文件,我会得到各种各样的错误。但是,如果我只包含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,则会出现语法错误
我使用的是视觉工作室。就像我说的,当我在模板底部包含Student.tpp时,我没有问题。但是当我使用相同的代码并包含Student.cpp时
非常感谢 我的猜测是:
当您命名包含类成员函数Student.cpp实现的文件时,编译器会尝试编译它。当您将其命名为Student.tpp时,编译器不会尝试编译它
在您的文件中,Student.h include的Student.cpp和Student.cpp include的Student.h。当proprocessor充实Student.cpp的内容时,它将两次包含文件的内容。这就是为什么您得到的函数模板已经被定义为错误
预防方法:
不要将文件命名为Student.cpp。您可以使用Student.tpp或更具描述性的名称Student\u Impl.h
除了使用pragma一次之外,还可以在文件中添加include-guard
移除所有的s。但请注意,模板代码不应位于.cpp文件中,除非它包含在头文件中,这可能会导致更多的混乱而不是喜悦。但是为什么你一开始就把这个类作为模板呢?我看不到模板参数在任何地方被使用。为什么学生应该是第一名的模板类?我在任何地方都找不到任何使用类型名t的声明。听着,你这里有个XY问题,实际上不需要。这只是一个概念。计划是接受T对象,而不是WriteCheck函数中的float。一切正常,我只是想知道为什么我不能使用cpp,而被迫使用.tpp。托马斯,我通过阅读这个问题,让程序开始运行。然而,我认为没有人解释为什么选择tpp而不是cpp。以及为什么cpp在Alli中似乎不起作用如果您在头文件中包含tpp,这与您在头文件中实现它是一样的,所以编译器知道模板是如何声明和实现的。而且,正如那个问题所说,当编译器需要从中实例化任何类时,它需要知道它是如何实现的。这就是为什么它不能在编译版本中,即在cpp文件中感谢Tomas,我想我现在理解得更清楚了。我感谢你的帮助!
#pragma once
#ifndef _student_impl_h_
#define _student_impl_h_