模板类的非模板函数友元
有人可以解释一下我做错了什么吗?这是我从编译器得到的错误。
非常感谢
1>------ Build started: Project: Ch16, Configuration: Release Win32 ------
1> p643_inclusion.cpp
1> p643_inclusion_main.cpp
1> p643_print.cpp
1>p643_print.cpp(5): error C2065: 'T1' : undeclared identifier
1>p643_print.cpp(5): error C2065: 'T2' : undeclared identifier
1>p643_print.cpp(6): warning C4552: '<<' : operator has no effect; expected operator with side-effect
1>p643_print.cpp(7): warning C4552: '<<' : operator has no effect; expected operator with side-effect
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
p643_inclusion.h
#ifndef P643H
#define P643H
template< class T1, class T2> class Car {
friend void print (const Car<T1, T2> &c1);
private:
T1 Wheels;
T2 DriversName;
public:
Car(): Wheels(4), DriversName("None") {}
Car(T1, T2);
};
template <class T1, class T2> class Driver {
private:
T1 Name;
T2 Surname;
public:
Driver(): Name("None"), Surname("None") {}
};
#include "p643_inclusion.cpp"
#endif
p643_inclusion.cpp
# ifndef P643CC
#define P643CC
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
#include "p643_inclusion.h"
template<class T1, class T2>
Car<T1, T2>::Car(T1 w, T2 d) {
Wheels = w;
DriversName = d;
}
#endif
p643_print.cpp
#include "p643_inclusion.h"
template< class T1, class T2> class Car;
void print (const Car<T1, T2> &c1) {
cout << c1.Wheels << endl;
cout << c1.DriversName << endl;
}
main
#include "p643_inclusion.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
Car<int, string> myCar;
Driver<string, string> myDriver;
print(myCar);
return 0;
}
Can somebody explain me what I am doing wrong? This is teh error I get from the compiler.
Many Thanks
1>------ Build started: Project: Ch16, Configuration: Release Win32 ------
1> p643_inclusion.cpp
1> p643_inclusion_main.cpp
1> p643_print.cpp
1>p643_print.cpp(5): error C2065: 'T1' : undeclared identifier
1>p643_print.cpp(5): error C2065: 'T2' : undeclared identifier
1>p643_print.cpp(6): warning C4552: '<<' : operator has no effect; expected operator with side-effect
1>p643_print.cpp(7): warning C4552: '<<' : operator has no effect; expected operator with side-effect
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
p643_inclusion.h
#ifndef P643H
#define P643H
template< class T1, class T2> class Car {
friend void print (const Car<T1, T2> &c1);
private:
T1 Wheels;
T2 DriversName;
public:
Car(): Wheels(4), DriversName("None") {}
Car(T1, T2);
};
template <class T1, class T2> class Driver {
private:
T1 Name;
T2 Surname;
public:
Driver(): Name("None"), Surname("None") {}
};
#include "p643_inclusion.cpp"
#endif
p643_inclusion.cpp
# ifndef P643CC
#define P643CC
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
#include "p643_inclusion.h"
template<class T1, class T2>
Car<T1, T2>::Car(T1 w, T2 d) {
Wheels = w;
DriversName = d;
}
#endif
p643_print.cpp
#include "p643_inclusion.h"
template< class T1, class T2> class Car;
void print (const Car<T1, T2> &c1) {
cout << c1.Wheels << endl;
cout << c1.DriversName << endl;
}
main
#include "p643_inclusion.h"
#include<iostream>
#include<string>
using namespace std;
int main()
{
Car<int, string> myCar;
Driver<string, string> myDriver;
print(myCar);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的函数实际上不是非模板函数。
这是错误的。您能问问自己
T1
到底是什么吗?和T2?您应该将其实现为:
并且您应该将其设为
friend
:Your function is not actually a non-template function.
This is wrong. Can you ask yourself what exactly is
T1
? andT2
?You should implement this as:
And you should make it
friend
as:混合模板和友谊并不总是像看起来那么简单。我的建议是,您在类定义中定义 befriended 函数,然后您的问题基本上就会消失:
对于模板
test
的每个实例化,它将声明并定义一个(非模板化)自由函数它采用使用触发模板实例化的相同模板参数实例化的test
对象。其他可用选项(如果可能的话,我会避开这些):
您可以使
print
成为模板,并声明该模板是您的类模板(整个模板)的友元:这向所有人开放您的内部结构模板的潜在实例化,包括可能的专业化,您可能不想这样做。如果您只想与该模板的特定实例成为朋友,您可以这样做,但它会变得更加麻烦:
有关进一步的解释,您可以查看答案 此处
Mixing templates and friendship is not always as simple as it might seem. My advice is that you define the befriended function in the class definition, and then your problem will basically go away:
For each instantiation of the template
test
, it will declare and define a (non templated) free function that takes atest
object instantiated with the same template arguments that triggered the instantiation of the template.Other options available (I would stay clear of these if possible):
You can make
print
be a template and declare that template a friend of your class template (the whole template):This opens your internals to all of the potential instantiations of the template, including possible specializations, and you might not want to do that. If you want to befriend only a particular instantiation of that template, you can do so, but it becomes more cumbersome:
For a further explanation, you can look at the answer here