Compiler error: Template in a Library.

Hello, I am just implementing a smart pointer class as a little exercise and have these two files "test.h" (library) and "test_main.c" (main program). When I try to compile these two files using:


g++ test_main.c -o test_main


I get the following error(s):

test_main.c: In function ‘int main(int, char**)’:
test_main.c:14:22: error: type/value mismatch at argument 1 in template parameter list for ‘template<class type> class smart_pointers::smart_pointer’
test_main.c:14:22: error:   expected a type, got ‘link’
test_main.c:14:26: error: invalid type in declaration before ‘=’ token
test_main.c:14:32: error: expected type-specifier before ‘link’
test_main.c:14:32: error: invalid conversion from ‘int*’ to ‘int’
test_main.c:14:32: error: expected ‘,’ or ‘;’ before ‘link’
test_main.c:15:5: error: base operand of ‘->’ is not a pointer
test_main.c:16:20: error: base operand of ‘->’ is not a pointer


Why is this happening? When I rename all instances of "link" in test_main.c it compiles and runs fine... But I need the program to work without changing the test_main.c file (the class should be called "link" and nothing else).

Here is my code:

test.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
#include<stdlib.h>
#include<iostream>

namespace smart_pointers{
   using namespace std;

   template<typename type>
      class smart_pointer{
   public:
      type *pdata;

      smart_pointer(){
         pdata = NULL;
      }
      smart_pointer(type *dat){
         pdata = dat;
      }
      type *operator->(){
         return pdata;
      }
   };
}


test_main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "test.h"

using namespace smart_pointers;

class link
{
 public:
   smart_pointer<link>next;
   int data;
};

int main(int argc, char*argv[])
{
   smart_pointer<link> l = new link();
   l->data = 5;
   printf("%d\n", l->data);
}


Thank your for your time.
Topic archived. No new replies allowed.