Working Templates header file, not working anymore

195 Views Asked by At

So, I had 2 files:

  • DFH_lib.h
  • Lib_Test.cpp

The programs were working perfectly just some 20-24 hours ago(I even have the executable file), I decided exclude the DFH_lib.cpp file as it is effectively empty (see below) and voila! It's giving me

Declaration Syntax Error

on the first line of the top template function.

  • I tried rearranging but it didn't help

This is super annoying, I'm stuck with Turbo because of my school and now this template based header file that I spend weeks building is suddenly not compiling.

Code


This is how the DFH_lib.h file starts (excluding the multi-line comment):

#ifndef DFH_lib_H
#define DFH_lib_H

enum bool { false, true };

template <class T>    //Error on this line
void AddColumn(T data, const int& width) {
    cout<<setw(width)<<data<<" | ";
}

NOTE: The error comes while compiling the DFH_lib.h

Question


On adding a DFH_lib.cpp file to the project, with nothing but this:

#ifndef _DFH_lib_CPP
#define _DFH_lib_CPP

#include"DFH_lib.h"

#if !defined __FSTREAM_H
#include<fstream.h>
#endif

#if !defined __IOMANIP_H
#include<iomanip.h>
#endif

#if !defined __CONIO_H
#include<conio.h>
#endif

#if !defined __STDIO_H
#include<stdio.h>
#endif

#if !defined __STRING_H
#include<string.h>
#endif
#endif

Everything worked fine! Why is this happening? All the headers written in DFH_lib.cpp are already included in the lib_Test.cpp prior to including DFH_lib.h.

In my opinion, the DFH_lib.cpp file is does nothing but it seems to be important none the less.

P.S. - I apologize if this is a duplicate, I know this question exists but I couldn't relate it to my situation. Here, the .cpp is effectively empty but still required.

A quick MCVE, that I put together:

UTILIZATION FILE:

#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include"fileio.h"
 
void fillSpace(char ch, const int& width) {
    cout<<setw(width)<<setfill(ch)<<"|";
}
int record_id = 1;
char char_member = 'A';
int int_member = 12;
float float_member = 2.3;
 
void show_tablular() {
    cout<<endl; AddColumn(record_id,7); AddColumn(char_member, 20); AddColumn(int_member, 11); AddColumn(float_member, 13);
}
 
void main()
{
    clrscr();
    cout<<endl; AddColumn("Record", 7); AddColumn("Char Member", 20); AddColumn("Int Member", 11); AddColumn("Float Member", 13);
    cout<<endl; AddColumn(" ", 7); AddColumn(" ", 20); AddColumn(" ", 11); AddColumn(" ", 13);
    show_tablular();
    cout<<endl; fillSpace('_', 7+2); fillSpace('_', 20+3); fillSpace('_', 11+3); fillSpace('_', 13+3);
    cout<<setfill(' ');  //Normalize cout based outputting format
 
    getch();
}

IMPLEMENTATION FILE:

#include"fileio.h"
 
#if !defined __FSTREAM_H
#include<fstream.h>
#endif
 
#if !defined __IOMANIP_H
#include<iomanip.h>
#endif
 
#if !defined __CONIO_H
#include<conio.h>
#endif
 
#if !defined __STDIO_H
#include<stdio.h>
#endif
 
#if !defined __STRING_H
#include<string.h>
#endif

HEADER FILE:

#ifndef file_H
#define file_H
 
enum bool { false, true };
 
template <class T>
void AddColumn(T data, const int& width) {
    cout<<setw(width)<<data<<" | ";
}
 
#endif

NOTE: They only work if added into a project in the same order from top to bottom otherwise the above error is encountered.

0

There are 0 best solutions below