C++类模板的问题
Issues with C++ Class templates
我在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(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:usersaurelib.csdesktopstudentprojectstudentprojectstudent.cpp 13 1 StudentProject
Error2 error C2995: 'Student<T>::~Student(void)' : function template has already been defined c:usersaurelib.csdesktopstudentprojectstudentprojectstudent.cpp 18 1 StudentProject
用于所有功能。
如果我从.cpp
文件中删除#include "Student.h"
,我会得到语法错误。
我正在使用Visual Studio。正如我所说,当我在模板底部#include "Student.tpp"
时,我没有问题。但当我使用相同的代码和#include "Student.cpp"
时。
提前感谢!
我的猜测是:
当您将包含类成员函数实现的文件命名为Student.cpp
时,编译器会尝试对其进行编译。当您将其命名为Student.tpp
时,编译器不会尝试编译它。
在您的文件中,Student.h
#include是Student.cpp
,Student.cpp
#include则是Student.h
。当预处理器充实Student.cpp
的内容时,它最终会包含两次文件的内容。这就是为什么会出现function template has already been defined
错误。
预防方法:
-
不要将文件命名为
Student.cpp
。您可以使用Student.tpp
,也可以使用更具描述性的名称Student_Impl.h
。 -
除了使用
#pragma once
之外,还在文件中添加#include防护。#pragma once #ifndef _student_impl_h_ #define _student_impl_h_
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 使用mongocxx驱动程序时包含头文件问题