-3

having created classes in different files i want to link these classes with the main function written in some other file (all of these files exist in a single project in codeblocks).i am unable to link these files also if writing header is the solution then what all elements to include in the header. my main function is as

#include <iostream>
#include "linkedlist.h"
using namespace std;

int main()
{
    linkedlist<int> obj1;
    obj1.create();
    obj1.add_node_at_beginning(10);
    obj1.add_node_at_beginning(20);
    obj1.add_node_at_beginning(30);
}

all of these functions are defined in linkedlist class which has a template my header file is

#ifndef LINKEDLIST_H_INCLUDED
#define LINKEDLIST_H_INCLUDED

template <class N>
struct Node
{
Node<N> *ptr_to_nxt_node;
 N data;
};

template <class LL>
class linkedlist
{
    Node<LL> *strt;
    Node<LL> *end;
    int n;
    Node<LL> *nde;

public:
    linkedlist();
    void create();
    void add_node_at_beginning(LL dat);
    void add_node_at_end(LL dat);
};


#endif // LINKEDLIST_H_INCLUDED
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • What problem are you getting? Is there are an additional `linkedlist.cpp` implementation file you don't show? – Benjamin Bannier Aug 15 '13 at 19:31
  • 1
    My psychic debugging tells me [this question and answers](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) about templates in C++ source files (.cpp) vs. header files (.h) is likely your problem. That based on the fact that your template header has no actual *implementation code*, and therefore I assume it is stuffed incorrectly in a .cpp file. It would help immensely if you posted the actual error messages **exactly** as they appear in your build output, but thats what I'm going with. – WhozCraig Aug 15 '13 at 19:32
  • @KushalSrivastava Read `Whoz's` comment! – gifnoc-gkp Aug 15 '13 at 19:39

1 Answers1

1

I don't see how codeblocks fails to associate them. Anyway you can always do it manually ...

    g++ -g -o main.exe main.cpp linkedlist.cpp

Make sure:

  • main.cpp linkedlist.cpp and linkedlist.h exist in your project. If not select your project and then click Add existing file.

  • Check if linkedlist.h is in the same folder as main.cpp if not you'll need to include it like this
    #include "subfolder/main.cpp"

gifnoc-gkp
  • 1,506
  • 1
  • 8
  • 17