2

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.

Community
  • 1
  • 1
  • Please show how you are using the header (likely an excerpt from Lib_Test.cpp), and check out [mcve] if you haven't already – Passer By Sep 11 '17 at 04:55
  • in the header you cannot use unqualified names. use std::cout std::setw – Artemy Vysotsky Sep 11 '17 at 05:13
  • @ArtemyVysotsky The C++ tag might be misleading, I added it as SO wasn't recognizing C++ code. I believe we don't need to define namespace in Turbo C++ –  Sep 11 '17 at 05:18
  • @PasserBy DONE! –  Sep 11 '17 at 05:37
  • @Alpha_SO Your question lacks [mcve] (Links to external websites doesn't count). Templated functions are compiled on usage (or should be, I don't know how pre-standard Turbo C++ does things), and your example doesn't show it (nor does it show the error message that you get). Does the type you are using this template for, have overloaded operator for stream output? – Algirdas Preidžius Sep 11 '17 at 06:04
  • 1
    Do you have a final #endif in the header file too? It is missing in the posted code. BTW, passing that int as const& is a bit weird. – Bob__ Sep 11 '17 at 06:23
  • @Alpha_SO I copied your files here to your question (see how the syntax highlight can be invoked even without C++ tag with markdown). take a look at **#1** of this answer [Linker Error: Duplicated Functions](https://stackoverflow.com/a/45933567/2521214) to question possibly of one of your fellow class mates (due to the same file names). The last resort of solution was to copy all to single file. But if the size was too big then the other **TurboC++** bugs surfaced ... That was the main reason for me to move from it away ages ago as it can not handle too complicated code reliably. – Spektre Sep 11 '17 at 07:00
  • @Bob__ Yes, I only posted the starting lines, that's why the #endif is missing, the header file is actually 250 lines long. That, passing as const& thing is what I saw in one of the answers here on SO, I was gonna change it but it works and I'm lazy, so... –  Sep 11 '17 at 07:36
  • @AlgirdasPreidžius Well, I provided the MCVE. It was a bit big so I put it in the links. The example template is used in the MCVE for fundamental datatypes. In the original code, I'm using the show_tabular() function as a class method to display Object's Data Members –  Sep 11 '17 at 07:40
  • If a comment makes your code stop compiling, the compiler is probably to blame. However, you have not shown the comment. But I suspect TurboC++ is to blame, it is quite ancient. If your school if making you use it, they are doing you a great disservice. – sp2danny Sep 11 '17 at 07:46
  • @Spektre I would love to move from this Turbo Hell, but as of now I can't. I understand size issue, so is that why this is happening? Cause it seems kinda odd that the inclusion of the effectively empty file ends up solving the issue. Plus, doesn't that just makes the complete .exe a tiny bit(pun intended) larger? My programs working but I was wondering if I could just learn about some cool concepts of C++ or another failure of Turbo as it seems –  Sep 11 '17 at 07:49
  • @sp2danny Oh no, It's not the comment. It's the exclusion of the DFH_lib.cpp file which while being effectively empty(see above), somehow solves the issue! The .exe is compiled and linked properly if the DFH_lib.cpp file is present in the project in the order given in the MCVE section above –  Sep 11 '17 at 07:50
  • @Alpha_SO If it is too big, it is **not** [mcve] (due to not being minimal). External links expire, leading to the question being incomplete in the future, hence, why all relevant information should be in the question itself. – Algirdas Preidžius Sep 11 '17 at 11:03
  • @AlgirdasPreidžius the problems usually starts with big so also the MCVE must be big. but can we name it MCVE? as it is not compilable ... ? sorry may be I am translating the M**C**VE too directly :) – Spektre Sep 11 '17 at 15:01
  • @AlgirdasPreidžius Well, it is there now. So please help. –  Sep 12 '17 at 12:31

0 Answers0