Possible Linking Issue

Hello all,

Firstly, let me apologize for anything out of the norm in this post; it is my first.

I have to write a program that parses an input file and generates a linked list for sequences of DNA. I have the i/o functions correct and am starting to work on the linked list. I have three classes (per request of my professor): sequenceDatabase - i/o, DNA - holds the data for each sequence (id, sequence, animal, etc.), and DNAList - holds pointers to each DNA object created (but NOT the actual object).

I am getting quite a few errors upon debugging in my DNAList.h file, most of which appear to stem from it not recognized "DNA" as a type. Below are a few of the errors:
"syntax error: missing ';' before '*'" - Line 11
"missing type specifier - int assumed..." - Line 11
"unexpected token(s) preceding ';'" - Line 11

These errors repeat each time I instantiate any type that uses DNA (regular, pointer, or references to DNA) in the DNAList header.

Below is DNAList.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef DNALIST_H
#define DNALIST_H
#include "DNA.h"
#include "sequenceDatabase.h"
#include <string>

using std::string;

struct DNANode
{
	DNA* data;
	DNANode* next;
};

class DNAList
{
public:
	DNAList();

	bool push_back(DNA* newDNA);

	DNANode* find(string id);

	bool set(const DNA& x);

	bool obliterate(string id);

	int size();

	~DNAList();

private:
	int length;
	DNANode* head;
};

#endif 


Below is DNA.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef DNA_H
#define DNA_H
#include "DNAList.h"
#include "sequenceDatabase.h"
#include <string>

using std::string;

class DNA
{
public:
	//Default constructor. Sets length to 0 and leaves other data members uninitialized.
	DNA(); 
	//Constructor. Sets each data member
	DNA(string rlabel, string rid, string rsequence, unsigned rlength, int rindex); 

	void print();
	void get();
	void set();

	~DNA();
private:
	string label, id, sequence;
	unsigned length;
	int index;
};

#endif 


And finally, sequenceDatabase.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef SEQUENCEDATABASE_H
#define SEQUENCEDATABASE_H

#include "DNA.h"
#include <fstream>
#include <string>

using std::string;
using std::ofstream;
using std::ifstream;

class SequenceDatabase
{
public:
	SequenceDatabase();
	SequenceDatabase(string ofname);

	void importEntries(string ifname);

	~SequenceDatabase();
private:
	DNAList list;
	ifstream fin;
	ofstream fout;
	unsigned count;

	void Print();
	void Obliterate();
	void Entries();
	void Add();
};

#endif 


I have made my own linked lists before, but only with one templated class to which I passed a struct in the main file. What is going on?

Any suggestions or insights would be very much appreciated!

P.S: The implementation files are currently just skeleton code
Last edited on
Why does your file DNA.h include DNAList.h? It has no use for it.

Why does your file sequenceDatabase.h include DNA.h? It has no use for it, but it is trying to make use of a DNAList object.

Basically, you've screwed up your include files. Get the order of includes right, and think about them such that the compiler doesn't find you trying to use a DNA object before you've told it what a DNA object is.
Last edited on
You have a few circular dependencies.

DNAList.h includes DNA.h.
DNA.h includes DNAList.h.

DNA.h includes sequenceDatabase.h.
sequenceDatabase.h includes DNA.h.

When you try to compile DNA.h, it include DNAList.h. DNAList.h tries to include DNA.h but nothing is included because the include guard DNA_H prevents this. That means when it reaches the definition of DNANode the DNA class has not yet been defined and that is why you get errors.

To avoid circular dependencies you should only include the headers that you need.

DNAList.h doesn't need to include DNA.h or sequenceDatabase.h. You just need to tell the compiler that there exist a class named DNA but you can do that with a simple forward declaration.
1
2
3
#include "DNA.h"
#include "sequenceDatabase.h"
class DNA;

DNA.h doesn't need to include any of your headers.
1
2
#include "DNAList.h"
#include "sequenceDatabase.h" 

sequenceDatabase.h should include DNAList.h instead of DNA.h. SequenceDatabase has a member of type DNAList so it needs the full class definition, that's why a forward declaration is not enough here.
1
2
#include "DNA.h"
#include "DNAList.h" 
Last edited on
@Moschops

I did not realize that the actual #include directive would cause issues with the compiler, but rather thought that it would, at worst, be a bit of unnecessary code.

@Peter87

Thank you! I have made the suggested changes and it is recognizing the correct types now!

For some reason, I was under the impression that forward declarations were only needed if you actually used the class within that file. I had just included everything to avoid figuring out which classes used which from the get-go.
Topic archived. No new replies allowed.