c++ - Working Templates header file, not working anymore -
so, had 2 files:
- dfh_lib.h
- lib_test.cpp
the programs working 20-24 hours ago(i have executable file), decided exclude dfh_lib.cpp file empty (see below) , voila! it's giving me
declaration syntax error
on first line of top template function.
- i tried rearranging didn't help
this super annoying, i'm stuck turbo because of school , template based header file spend weeks building not compiling.
code
this how dfh_lib.h file starts (excluding multi-line comment):
#ifndef dfh_lib_h #define dfh_lib_h enum bool { false, true }; template <class t> //error on line void addcolumn(t data, const int& width) { cout<<setw(width)<<data<<" | "; }
note: error comes while compiling dfh_lib.h
question
on adding dfh_lib.cpp file project, nothing 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 happening? all headers written in dfh_lib.cpp included in lib_test.cpp prior including dfh_lib.h.
in opinion, dfh_lib.cpp file nothing seems important none less.
p.s. - apologize if duplicate, know this question exists couldn't relate situation. here, .cpp empty still required.
a quick mcve, put together:
#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(); }
#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
#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: work if added project in same order top bottom otherwise above error encountered.
Comments
Post a Comment