error LNK2005: "public: void __thiscall tree::make_tree(int,int)" (?make_tree@tree@@QAEXHH@Z) already defined in main.obj

Hi,
I believe I am defining the tree structure multiple times and this is causing problems. I have tried to correct for this but not with any success. Was wondering if some would take a look and see what the issue might be?

There are three files to consider here. The Myheader.h file, The Myroutines.cpp file and Main.cpp. Both Myroutines.cpp and Main.cpp reference the Myheader.h file. The error is get is of the following type:

MyRoutines.obj : error LNK2005: "public: void __thiscall tree::make_tree(int,int)" (?make_tree@tree@@QAEXHH@Z) already defined in main.objHere make_tree is a routine that is defined as part of the tree class. It is defined in the Myheader.h file. That file is included in main.cpp and Myroutines.cpp.

I) snippet of Code in Myheader.h:

#ifndef _MY_HEADER_

class node{
/* some code here defininng the node class */
};


class tree{
public:
int size,nl,nh;
node **p;
void make_tree(int ml, int mh); tree(){size=0; p = new node*[0];}
tree(int ml, int mh){make_tree(ml,mh);}
tree(tree const &src){size = src.size; p = src.p;}
};

void tree::make_tree(int ml, int mh){
int i,j,nl,nh,length,space;

nl=ml;
nh=mh;
size = (mh-ml+1);
space = size*(size+1)/2;

p = new node*[size];
p=p-ml;
p[ml] = new node[space];
//Code here which allocates the array of nodes to each of the pointers.
}

#define _MY_HEADER_
#endif /* _MY_HEADER_ */

2) Code in main.cpp

#define NR_END 1
#define FREE_ARG char*
#define NRANSI
#define MAXITS 200
#define TOLF 1.0e-4
#define TOLMIN 1.0e-6
#define TOLX 1.0e-7
#define STPMX 100.0
#define TINY 1.0e-20
#define ALF 1.0e-4
#define EPS 1.0e-4
#define MAX_WIDTH 200

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <fstream>
#include <cfloat>
#include <cstdlib>
#include <cmath>
#include <Mynrutil.h>
#include <Mynr.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

#include "Myheader.h"

using namespace std;

/* Routines to read data */
int nfields(char *g);
mymatrix read(char *filename);


int main()
{
char filename[MAX_WIDTH + 1]="Readme.txt";
mymatrix readmat;
int i;

readmat = read(filename);
if ((readmat.m) > 0 && (readmat.n > 0)) cout << readmat << endl;
else cout << "Yikes" << endl;
system("PAUSE");
return 0;
}

III) Code in Myroutines.cpp

#define NR_END 1
#define FREE_ARG char*
#define NRANSI
#define MAXITS 200
#define TOLF 1.0e-4
#define TOLMIN 1.0e-6
#define TOLX 1.0e-7
#define STPMX 100.0
#define TINY 1.0e-20
#define ALF 1.0e-4
#define EPS 1.0e-4
#define MAX_WIDTH 200

#include <iostream>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <fstream>
#include <cfloat>
#include <cstdlib>
#include <cmath>
#include <Mynrutil.h>
#include <Mynr.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>


using namespace std;

#include "Myheader.h"


int nfields(char *line)
{
/* Counts the number of non-space entries in the line */
char *token;
int i=0;

token = strtok(line," "); //Read until space is encountered
while (token != NULL)
{
i++;
token = strtok(NULL," ");
}
return i;
}

mymatrix read(char *filename)
{
// Reads a file of doubles into a matrix which it passes on to the main routine
// Doubles are separated by a single space
//code that works fine by itself
}
[code] "Please use code tags" [/code]
If you are going to define a function/method in a header, then it must be inline (or a template).
Thanks!
Topic archived. No new replies allowed.