| |

VerySource

 Forgot password?
 Register
Search
View: 17|Reply: 14

Class template issue ver2

[Copy link]

3

Threads

10

Posts

10

Credits

Newbie

Rank: 1

Credits
10

 India

Post time: 2020-3-21 15:00:01
| Show all posts |Read mode
template <int i1, int i2>
class One
{
template <int i1, int i2> friend ostream&operator << (ostream&, const One <i1, i2>&);
template <int i1, int i2> friend void fun2 (const One <i1, i2>&);
};

template <int i1, int i2> ostream&operator << (ostream&, const One <i1, i2>&) {}
template <int i1, int i2> void fun2 (const One <i1, i2>&) {}

template <class T>
class Two
{
template <class T> friend ostream&operator << (ostream&, const Two <T>&);
template <class T> friend void fun1 (const Two <T>&);

};

template <class T> ostream&operator << (ostream&, const Two <T>&) {}
template <class T> void fun1 (const Two <T>&) {}

In addition to fun1, all 3 functions have ambiguous meanings.

How to deal with it?
Reply

Use magic Report

0

Threads

23

Posts

13

Credits

Newbie

Rank: 1

Credits
13

 China

Post time: 2020-7-1 18:45:01
| Show all posts
In the two friend functions of class One, t1 and t2 of One<i1,i2> of the parameter table are the template parameters of the outer class One used.
Or is it the parameter list in the friend declaration? ? ? ? ? ? In addition, when you declare friends, because there is no advance declaration, the compiler cannot
Know what Yuyuan you put there! ! !

What you want may be (class I will not change it for you, similar):
//First three forward declarations (forward declaration)
template<int i1,int i2>
class One;
template<int i1,int i2>
ostream&operator<<(ostream&,const One<i1,i2>&);
template<int i1,int i2>
void fun2(const One<i1,i2>&);


template<int i1,int i2>
class One
{
friend ostream&operator << <i1, i2>(ostream&, const One<i1,i2>&);
friend void fun2<i1, i2>(const One<i1,i2>&);
};

template<int i1,int i2>
ostream&operator<<(ostream&,const One<i1,i2>&)
{
}


template<int i1,int i2>
void fun2(const One<i1,i2>&)
{
}
Reply

Use magic Report

0

Threads

23

Posts

13

Credits

Newbie

Rank: 1

Credits
13

 China

Post time: 2020-7-1 21:00:01
| Show all posts
What you want may also be (note the case of template parameters):
#include <iostream>
using namespace std;


template<int i1,int i2>
class One;

template<int i1,int i2>
ostream&operator<<(ostream&,const One<i1,i2>&);


template<int i1,int i2>
void fun2(const One<i1,i2>&);


template<int i1,int i2>
class One
{
template <int I1, int I2>
friend ostream&operator << (ostream&, const One<I1,I2>&);
template <int I1, int I2>
friend void fun2(const One<I1,I2>&);
};

template<int i1,int i2>
ostream&operator<<(ostream&,const One<i1,i2>&)
{
}


template<int i1,int i2>
void fun2(const One<i1,i2>&)
{
}
Reply

Use magic Report

0

Threads

23

Posts

13

Credits

Newbie

Rank: 1

Credits
13

 China

Post time: 2020-7-1 22:45:01
| Show all posts
For grammatical problems like this, I suggest you to take a good look at "c++ templates", there is really no need to clarify the syntax,
Compete with the compiler
Reply

Use magic Report

3

Threads

10

Posts

10

Credits

Newbie

Rank: 1

Credits
10

 China

 Author| Post time: 2020-7-2 13:30:02
| Show all posts
Of course what I want is not your answer, I understand what you say

I just want to understand why the same function is declared and defined that way

The compiler will think of it as two different functions

Also, the syntax I defined is just strange, but not wrong

The effect is to allow any instance of the function template to access any instance of the class template

But it seems that after the declaration, I don’t know how to define it.

Blame me for not explaining, sorry!
Reply

Use magic Report

0

Threads

23

Posts

13

Credits

Newbie

Rank: 1

Credits
13

 China

Post time: 2020-7-4 13:15:01
| Show all posts
Why don't you read my second reply carefully, then it will meet your requirements
Reply

Use magic Report

0

Threads

23

Posts

13

Credits

Newbie

Rank: 1

Credits
13

 China

Post time: 2020-7-5 14:00:01
| Show all posts
Finally, I’ll give you a version. If it’s not what you want, then I’m too lazy to say anything.
#include <iostream>
using namespace std;

template<int T1,int T2>
class One;

template<int i1,int i2, int T1, int T2>
void fun2(const One<T1,T2>&);

template<int T1,int T2>
class One
{
const static int NUM = 18;

template<int i1,int i2, int A1, int A2>
friend void fun2(const One<A1,A2>&);
};


template<int i1,int i2, int T1, int T2>
void fun2(const One<T1,T2>&)
{
cout
<< "One<" << T1 << ", "<< T2 << ">::NUM=" << (One<T1,T2>::NUM)
<< "\nviewing from\n\tfun2<" << i1 << ", "<< i2 << "," << T1 << ", "<< T2 << ">\n"<< endl;
}


int main()
{
One<1, 1> one11;
One<2, 4> one24;
fun2<2, 3>(one11);
fun2<5, 6>(one11);
fun2<7, 8>(one11);
fun2<7, 8>(one24);

}
Reply

Use magic Report

3

Threads

10

Posts

10

Credits

Newbie

Rank: 1

Credits
10

 China

 Author| Post time: 2020-7-9 16:30:01
| Show all posts
I read it and I have already tried your second reply method

The overload is still ambiguous

I don’t know. Did you try it on your own compiler while giving the code?

If it is so easy to solve, I will not waste everyone's brain cells on it!
Reply

Use magic Report

3

Threads

10

Posts

10

Credits

Newbie

Rank: 1

Credits
10

 China

 Author| Post time: 2020-7-9 23:15:01
| Show all posts
Your last method I tried

The overload is not clear

And the method you gave is even stranger than mine.
Reply

Use magic Report

0

Threads

1

Posts

2

Credits

Newbie

Rank: 1

Credits
2

 China

Post time: 2020-7-10 14:15:01
| Show all posts
It's really strange, I compiled the program given by661193and it passed
Reply

Use magic Report

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

Points Rules

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

Quick Reply To Top Return to the list