| |

VerySource

 Forgot password?
 Register
Search
View: 905|Reply: 8

Class template problem

[Copy link]

3

Threads

10

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

Post time: 2020-3-17 15:00:01
| Show all posts |Read mode
template <class Type>
class Bar
{
   template <class T> friend void Foo1 (const T&);
};

How can Foo1 be defined outside the template class Bar?

I wrote it like this
template <class T> void Foo1 (const T&) {}

But when compiling, it says error C2668: "Foo1": The call to overloaded function is ambiguous
May be the one declared in the template class, or the one defined outside

What should I do?
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-6-20 09:15:01
| Show all posts
template<class T>
void Bar<T>::Foo1(const T&)
{}
Reply

Use magic Report

3

Threads

10

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 China

 Author| Post time: 2020-6-20 18:00:01
| Show all posts
Is it wrong on the second floor?

Foo1 is not a member function
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-6-21 23:00:01
| Show all posts
template<class T>
class Bar
{
   template<class T2>
   friend void Foo1(const T2&);
};

template <class T2>
void Foo1(const T2&)
{}

//Look at the member template
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-6-22 08:45:01
| Show all posts
Wrong, I can compile this
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-6-22 20:00:02
| Show all posts
#include <iostream>

template<class T>
class Bar
{
public:
   Bar(T b): b_(b) {}
   
   template<class T2>
   friend void Foo1(const T2&);
private:
T b_;
};

template <class T2>
void Foo1(const T2&t)
{
std::cout << t.b_ << std::endl;
}

int main()
{
Bar<int> b(5);
The
Foo1(b);
The
return 0;
}
Reply

Use magic Report

3

Threads

9

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Korea, Republic of

Post time: 2020-6-24 17:00:01
| Show all posts
The program upstairs cannot be compiled in vc! There are two errors? ? ? ...
error C2668:'Foo1': ambiguous call to overloaded function
Reply

Use magic Report

0

Threads

63

Posts

43.00

Credits

Newbie

Rank: 1

Credits
43.00

 China

Post time: 2020-6-28 08:45:01
| Show all posts
I use MinGW
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-7-16 23:30:01
| Show all posts
Can be compiled by A
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list