Lohitaksh Trehan
Lohitaksh Trehan

Reputation: 304

How to include a header file in c++?

/***You willl find good information in the answer selected and comments of post by @datell*****/

/**** Also there is something related to use off "modules" and "inline before template" (i am asking on template classes and not template functions, both are different) in c++, that if its related, i cant relate in reality(i am a noob)*****/

i am asking how can i include a header file in my main.cpp, i have implemented basic stack operations in it ; main.cpp btack.h and btack.cpp. Compiler is giving a long list of linkage errors(works fine if i give declaration and defination in btack.h, ofcourse i dont want to do that for modularity). its a temmplate class. i will provide the code which runs, that is without btack.cpp

main.cpp

#include <iostream>
using namespace std;
#include "btack.h"
int main() {
    char a;
    int z;
    //std::cout << "Hello, World!\n";
    btack <int> b(3);
    btack <char> c(3);
    b.push(4);
    b.push(5);

btack.h

#ifndef btack_h
#define btack_h
#include <iostream>
using namespace std;
template <typename s> class btack
{
    int TOS,size;
    s *ptr;
public:
    btack(int );
    ~btack();
    void push(s ob);
    s pop();

};

template <typename s> btack<s>::btack(int i)
{
    ptr = (s*)malloc(i*sizeof(s));
    TOS=0;
    size=i;
}

template <typename s> void btack<s>::push(s ob)
{
    if(TOS>=size)
    {
        cout<<"stack is full"<<endl;
        return;
    }
    ptr[TOS]=ob;
    TOS++;
}

template <typename s> s btack<s>::pop()
{
    if(TOS<=0)
    {
        cout<<"stack is empty"<<endl;
        return ((s) 0);
    }
    TOS--;
    return ptr[TOS];
}
template <typename s> btack<s>::~btack()
{free(ptr);}

#endif /* btack_h */

so basically i was going through so many internet posts and i found that you have to use "Export" keyword, but that word is no longer used, i was unable to find a proper guide on this topic. NEw c++ standards please. A guide how i can learn this thing from scratch. Please you mighty coders, a guiding link will help.

Update******** For all those saying about explicitly declaring data types at end of header: suppose it is a STL implementation of stack.h, you dont instantiate explicitly the various types of data types you want to use, it automatically do that


points of worth:

C++14 does not really permit abstract templates... In some sense, they need to be "concrete" (implemented in header files) – Basile Starynkevitch

Upvotes: 0

Views: 419

Answers (4)

user14055173
user14055173

Reputation: 1

// stack node structure
struct Node {
    char data;
    struct Node* next;
};
struct Node* initiStack(void);
int isEmpty(struct Node** top);
// push function
void push(struct Node** top, int data);

// pop function
int pop(struct Node** top);
int peek(struct Node** top_re);
int size(char expn[]);

Upvotes: 0

In practice, a C++14 compiler needs to know for every aggregate type (in particular instantiated template classes) its size, alignment, vtable (if any) and sequence of fields -with their type and alignment-.

Hence, templates are practically not abstract types, even if programmers should view them that way.

Therefore, standard containers headers (like <vector>, <map> etc...) are generally including a lot of internal stuff defining the internal implementation of the template, and all the template member functions are inlined.

In practice, a standard header like <vector> is expanded to a lot of stuff (about ten thousand lines of C++ on my GCC 6 compiler on Linux).

Try the following command (it is preprocessing) with a simple file mytest.cc having #include <vector>:

 g++ -C -E -H -Wall mytest.cc > mytest.ii

The -H option shows all the internal included files. The -C -E is asking for preprocessed form with comments into mytest.ii. Then look with an editor (or a pager) into the generated mytest.ii; it would be quite big.

And that is why C++ compilation is often slow.

Modules are a future feature of C++ which could help. See this question.

Upvotes: 1

dtell
dtell

Reputation: 2568

Since you are using a template class you don't need any .cpp file for an implementation of it.

So you got two files: main.cpp and btack.h. Main contains #include "btack.h".

You compile it with g++ main.cpp.

Upvotes: 1

RomMer
RomMer

Reputation: 1089

three separated files

btack.cpp with the cpp code for btack btack.h the header file main.cpp

in main.cpp ; on first line just write

 #include "btack.h"

if btack is in the same directory; if it's not, write the path of the file

then compile main.cpp and btack.cpp (don't forget to also #include "btack.h" in btack.cpp)

Upvotes: 0

Related Questions