How do I make this template class compiling in both VC6 and VC9
我有一个模板类在 VC6 中编译得很好。但它没有在 VC9 中编译。为此,我在一种类型前面添加了”typename”关键字。之后它在 vc9 中编译,但不在 VC6 中。我如何使它与两个编译器一起编译。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <map>
#include <vector>
template <class T1, class T2>
class A
{
public:
std::pair<typename std::vector<std::pair<T1,T2> >::iterator, bool > insert(const std::pair<T1 ,T2> &value_in);
};
template <class T1, class T2>
std::pair< std::vector<std::pair<T1,T2> >::iterator, bool> A<T1,T2>::insert(const std::pair<T1 ,T2>& value_in)
{
std::pair< std::vector<std::pair<T1,T2> >::iterator, bool> b;
return b;
}
void main()
{
A<int, int> a;
a.insert(std::pair<int, int>(0, 0));
}
|
以上代码在 VC6 中编译。但在 VC9 中没有。
以下代码在 VC9 中有效,在 VC6 中无效。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <map>
#include <vector>
template <class T1, class T2>
class A
{
public:
std::pair<typename std::vector<std::pair<T1,T2> >::iterator, bool > insert(const std::pair<T1 ,T2> &value_in);
};
template <class T1, class T2>
std::pair<typename std::vector<std::pair<T1,T2> >::iterator, bool> A<T1,T2>::insert(const std::pair<T1 ,T2>& value_in)
{
std::pair< std::vector<std::pair<T1,T2> >::iterator, bool> b;
return b;
}
void main()
{
A<int, int> a;
a.insert(std::pair<int, int>(0, 0));
}
|
- 在某些时候,您将达到编写可在 VC9 和 Fortran 77 中编译的代码的愚蠢程度。
VC6 不太符合标准。您可能只需要#IFDEF 来解决这个问题。
更改评论:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <map>
#include <vector>
template <class T1, class T2>
class A
{
public:
std::pair<typename std::vector<std::pair<T1,T2> >::iterator, bool > insert(const std::pair<T1 ,T2> &value_in);
};
template <class T1, class T2>
std::pair< typename std::vector<std::pair<T1,T2> >::iterator, bool> // +typename
A<T1,T2>::insert(const std::pair<T1 ,T2>& value_in)
{
std::pair< typename std::vector<std::pair<T1,T2> >::iterator, bool> b; // +typename
return b;
}
int main() // void->int
{
A<int, int> a;
a.insert(std::pair<int, int>(0, 0));
}
|
-
这两个类型名使它正确 C 。使用这个作为你的”主要”,如果你真的需要恕我直言,特别适合 VC6 的一些#ifdefs。
我没有VC6,但是VC2003也报错。所以我在
之前放了一个类型名
1
|
std::pair< typename std::vector<std::pair<T1,T2> >::iterator, bool > insert(const std::pair<T1 ,T2> &value_in);
|
制作它
1
|
typename std::pair< typename std::vector<std::pair<T1,T2> >::iterator, bool > insert(const std::pair<T1 ,T2> &value_in);
|
它成功了,也许你也可以试试。 HTH,
- 奇怪的是,科莫也抱怨。尝试在使用 std::vector<std::pair<T1,T2> > 之前放置类型名,例如 std::pair<typename std::vector<std::pair<T1,T2> >::iterator, bool>乙;他们科莫接受。
来源:https://www.codenong.com/1109050/
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9897.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57