我正在读一本书(Accelerated C ++)并从那本书中练习,试图自己学习C ++。
现在我想了解为什么以下代码无效:
// analysis.cpp
void write_analysis(ostream& out, const string& name,
double analysis(const vector<Student_info>&),
const vector<Student_info>& did,
const vector<Student_info>& didnt)
{
out << name << ":median(did) = " << analysis(did) <<
", median(didnt)=" << analysis(didnt) << endl;
}
template <double F(Student_info&)>
double generic_analysis(const vector<Student_info>& students)
{
vector<double> grades;
transform(students.begin(), students.end(),
back_inserter(grades), F);
return median(grades);
}
template<> double generic_analysis<grade_aux>(const vector<Student_info>& students);
double median_analysis(const vector<Student_info>& students)
{
return generic_analysis<grade_aux>(students);
// if not using template function, the code will be
// as following
// vector<double> grades;
// transform(students.begin(), students.end(),
// back_inserter(grades), grade_aux);
// return median(grades);
}
analysis.cpp
// analysis.h
template <double F(const Student_info&)>
double generic_analysis(const std::vector<Student_info>&);
void write_analysis(std::ostream&, const std::string&,
double analysis(const std::vector<Student_info>&),
const std::vector<Student_info>&,
const std::vector<Student_info>&);
double grade_aux(const Student_info&);
double median_analysis(const std::vector<Student_info>&);
analysis.h
// main.cpp
int main()
{
// students who did and didn't do all their homework
vector<Student_info> did, didnt;
// read the student records and partition them
Student_info student;
while (read(cin, student)) {
did.push_back(student);
}
// do the analyses
write_analysis(cout, "median", median_analysis, did, didnt);
return 0;
}
的main.cpp
// grade.cpp
// compute a student's overall grade from midterm and final exam grades
// and homework grade
double grade(double midterm, double final, double homework)
{
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}
double grade_aux(const Student_info& s)
{
try {
return grade(s);
} catch (domain_error) {
return grade(s.midterm, s.final, 0);
}
}
grade.cpp
,错误信息为:
g ++ -Wall * .cpp -o main /tmp/ccbXbVcV.o:In function
median_analysis(std::vector<Student_info, std::allocator<Student_info> > const&)': analysis.cpp:(.text+0x91): undefined reference to
双 generic_analysis&LT;及(grade_aux(Student_info const&amp;))&gt;(std :: vector&gt; const&amp;)'collect2:ld返回1退出状态
同时,当我将generic_analysis代码声明和定义放在头文件中时, 它会工作。请有人解释原因?
修改 删除行时:
template<> double generic_analysis<average_grade>(const vector<Student_info>& students);
我得到了:
g ++ -Wall * .cpp -o main /tmp/cc3VhXKU.o:在函数
median_analysis(std::vector<Student_info, std::allocator<Student_info> > const&)': analysis.cpp:(.text+0x91): undefined reference to
double generic_analysis&lt;&amp;(grade_aux(Student_info const&amp;))&gt;(std :: vector&gt; const&amp;)' collect2:ld返回1退出状态
好像我需要一个Explicit instantiation
。然后我添加了以下行:
template double generic_analysis<grade_aux>(const vector<Student_info>&);
现在错误信息是:
g ++ -Wall * .cpp -o main
analysis.cpp:实例化'double generic_analysis(const std :: vector&amp;)[with double(* F)(const Student_info&amp;)= grade_aux]':
analysis.cpp:26:72:从这里实例化 analysis.cpp:26:72:错误:显式实例化'double generic_analysis(const std :: vector&amp;)[with double(* F)(const Student_info&amp;)= grade_aux]'但没有可用的定义[-fpermissive] analysis.cpp:实例化'double generic_analysis(const std :: vector&amp;)[with double(* F)(const Student_info&amp;)= grade_aux]':
analysis.cpp:26:72:从这里实例化 analysis.cpp:26:72:错误:显式实例化'double generic_analysis(const std :: vector&amp;)[with double(* F)(const Student_info&amp;)= grade_aux]'但没有可用的定义[-fpermissive] analysis.cpp:实例化'double generic_analysis(const std :: vector&amp;)[with double(* F)(const Student_info&amp;)= grade_aux]':
analysis.cpp:26:72:从这里实例化 analysis.cpp:26:72:错误:显式实例化'double generic_analysis(const std :: vector&amp;)[with double(* F)(const Student_info&amp;)= grade_aux]'但没有定义[-fpermissive]
答案 0 :(得分:1)
您定义:
template<> double generic_analysis<grade_aux>(const vector<Student_info>& students);
实施在哪里?
如果您正在尝试强制实例化某个功能:
答案 1 :(得分:1)
你宣布了
template<> double generic_analysis<grade_aux>(const vector<Student_info>& students);
但没有在任何地方定义它。如果你真的打算有这样一个明确的专业化,你需要定义它(可能不在标题中)。
如果另一方面意图是使用主要模板generic_analysis
,那么您不需要声明上述特化。