the problem with the class template

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
 
using namespace std;
template <typename T>
class vector
{
public:
 vector()
 {
	first=0;
	second=0;
 }
  vector(T first, T second)
 {
  first=a;
   second=b;
 }
  T display();
private:
	double first;
	 double second;
};
template <typename T>
  T vector<T>::displaly()
  {
  cout<<"("<<first<<","<<second<<endl;
  
  }
  void main()
  {
   vector<int> a(9,8);
   a.display();

  }


//compiler said that 'displaly' : is not a member of 'vector<T>', and I am sort of confused because I declared that in the class vector.
add #include <vector>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>

// using namespace std; // *** avoid

template <typename T>
class vector
{
    public:
        vector()
        {
            first=0;
            second=0;
        }

        //vector(T first, T second) // *** a, b
        vector( T a, T b )
        {
            first=a;
            second=b;
        }

        T display();

    private:
        double first;
        double second;
};

template <typename T>
//T vector<T>::displaly() // *** typo
T vector<T>::display()
{
    //cout<<"("<<first<<","<<second<<endl;
    std::cout << "(" << first << ',' << second << ")\n" ;
    return first ; // *** added
}

//void main() // main must return int
int main()
{
    vector<int> a(9,8);
    a.display();
}
Topic archived. No new replies allowed.