question about compiling interdependent files

I wrote a "string" class, and a "substring" class and put them into their respective .cpp and .h files. So now I have 4 files.

I want to ask how i'm supposed to compile this??? Right now, as a example, for the string class, i'm trying

g++ -c string.cpp


in "string.cpp", I included "string.h". in string.h, i included "substring.h"

and lastly, in substring.cpp, i included "substring.h"

Now, I'm utterly confused as to how to compile the string and substring class so that i can use them in a tester program.

Thanks for any help.
g++ [...] string.cpp substring.cpp [...]
g++ -o string.o -c string.cpp
g++ -o substring.o -c substring.cpp
g++ -o tester.o -c tester.cpp

Linux:
g++ -o tester tester.o substring.o string.o

Windows:
g++ -o tester.exe tester.o substring.o string.o
i'm trying the commands given above.

The problem is, when my code had a bunch of (ifndef define endif) statements. It complained for a whole page of undefined variables.

Then i took out the ifndef's , now it complains of redefined variabes. Is there something i need to be careful of?

right now, i put the ifndef's for the string.h and the substring.h file
You need the #ifndef __HEADER_NAME_H statements in your header files.

If you are getting compilation errors you are going to need to post the code here so people can try to work out what is wrong.
i got it to compile. But i put ifndef every time i tried to include sth.

I then tried to take out the ifnef's everywhere but in the header files. Now i have a really wierd error. The codes follow

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "string.h"

#ifndef SUBSTRING_H
#define SUBSTRING_H


class string;

class substring {
    public:
	substring (string&, unsigned int, unsigned int);
	//substring (const substring& substr);
	void operator = (string& s);
	operator string ()const;

    private:
	string& base;
	int index;
	int length;
};

#endif 



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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "substring.h"
#include "string.h"
#include "assert.h"

substring::substring (string& s, unsigned in, unsigned int len) : base(s), index(in), length(len){
	base = s;
	if (in>=s.length()){
		in = 0;
		len = 0;
	}

	index = in;
	int numCharLeft = s.length() -in;
	length = len;
	if (len>numCharLeft)
		length = numCharLeft;

}

/*
substring::substring (const substring& substr){
	base = substr.base;
	index= substr.index;
	length = substr.length;
}*/


void substring::operator= (string& str){
// replace the substring part of the base with str

	const int strLen = str.length();
	if (length== strLen){
		int i;
		for (i=0; i<length; i++)
			base[index+i] = str[i];
	}else{
		int newLen = base.length() - length + strLen + 1;
		char *  temp = new char [newLen];
		for (int i=0; i<index; i++)
			temp[i] = base[i];
		for (int i=0; i<strLen; i++)
			temp[index+i] = str[i];
		for (int i=0; i<newLen-1;i++)
			temp[index+strLen+i] = base[index+length + i];

		
		base = temp;
	}
	return;

}

substring::operator string () const{
	char * temp = new char [length+1];
	assert(temp!=0);

	int i;
	for (i=0; i<length; i++)
		temp[i] = base[index+i];

	temp[i]='\0';
	string s(temp);
	delete [] temp;
	return s;
}


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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include "substring.h"


#ifndef STRING_H
#define STRING_H


class substring;

class string{
    public:
	//constructors
	string ();
	string (char);
	string (int);
	string (const char * p);
	string (const string&);

	//destructor
	~string ();

	//assignment and concatenation
	void operator= (const string&);
	void operator+= (const string&);

	substring operator()(unsigned int start, unsigned int len);

	std::istream& getline(std::istream& in);

	//get info
	int length () const;

	//access to a character, both as left side and as right side
	char& operator[] (unsigned int);

	//relational
	int compare (const string&);

	//conversion to ordinary C string
	operator  const char* () const;

   private:
	//data storage
	char * buffer;
	unsigned short int bufferlength;

};

std::istream& operator>>  (std::istream& in, string& str);

#endif



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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "string.h"
#include "assert.h"
#include "substring.h"

string::string () {
	bufferlength = 1;
	buffer = new char[bufferlength];
	buffer[0] = '\0';
}

string::string (int size) {
	bufferlength = 1+ size;
	buffer = new char[bufferlength];
	for (int i=0; i<bufferlength;i++)
		buffer[i] = '\0';
}


string::string (char c) {
	bufferlength = 2;
	buffer = new char[bufferlength];
	buffer[0] = c;
	buffer[1] = '\0';
}

string::string (const char* str){
	int size = 0;
	for (;str[size]!='\0';size++);

	bufferlength = size+1;
	buffer = new char[bufferlength];
	assert(buffer!=0);
	for (int i=0; i<size; i++)
		buffer[i] = str[i];

	buffer[bufferlength-1] = '\0';
}

string::string (const string& str){
	int size = str.length();

	bufferlength = size+1;
	buffer = new char[bufferlength];
	assert(buffer!=0);
	for (int i=0; i<size; i++)
		buffer[i] = str[i];

	buffer[bufferlength-1] = '\0';
}

std::istream& operator>> (std::istream& in, string& s){
	char buffer [200];
	in >> buffer;
	s = buffer;
	return in;
}

std::istream& string::getline (std::istream& in){
	in.getline(buffer, bufferlength);
	return in;
}

string::~string (){
	delete[] buffer;
}

void string::operator= (const string& str){
	int len = str.length();
	if (len > bufferlength - 1){
		bufferlength = len+1;
		delete [] buffer;
		buffer = new char [bufferlength];
		assert(buffer!=0);
	}

	for (int i=0; i<len; i++)
		buffer[i] = str[i];

	buffer[len] = '\0';
}

void string::operator+=(const string& str){
	int len = str.length();
	int thisLength = length();

	int total_len = len + thisLength;
	if (bufferlength < total_len + 1){
		bufferlength = total_len + 1;
		string temp = buffer;
		delete [] buffer;
		buffer = new char [bufferlength];
		assert(buffer!=0);
		int i;
		for (i = 0; i< thisLength; i++)
			buffer[i] = temp[i];
		buffer[i] = temp[i];
		
	}

	//start to copy
	int i;
	for (i = 0; i<len;i++)
		 buffer[i+thisLength] = str[i];

}

int string::length() const {
	int size = 0;
	for (;buffer[size]!='\0';size++);
	return size;
}

char dummy;

char& string::operator[] (unsigned int index){
	assert(index>=0 and index < bufferlength);
	//find index of '\0'
	int size = length();
	if (index == size) {
		dummy = '\0';
		return dummy;
	}

	return buffer[index];
}

int string::compare (const string& str){
	int i;
	for (i=0; buffer[i]==str[i] && buffer[i]!='\0' && str[i]!='\0'; i++);
	return (buffer[i] - str[i]);
}

string::operator const char* () const {
	return buffer;
}

substring string::operator() (unsigned int in, unsigned int len){
	substring s(*this, in, len);
} 



<------------------------------------------------------------------------------------------------------------>

And the error message is

from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
from substring.h:1,
from string.h:2,
and this follows for two pages ................

then it says : "substring.h:1:20: error: #include nested too deeply"

i'm suspecting that the code got into a infinite loop, but shouldn't it stop because of the ifndef???







1
2
3
4
#include "string.h"

#ifndef SUBSTRING_H
#define SUBSTRING_H 


The whole point of the include guards is to prevent including something twice. But in order for them to work you have to put everything inside them:


1
2
3
4
#ifndef SUBSTRING_H
#define SUBSTRING_H

#include "string.h" 

thanks a lot man.

i think i got it now. now i can actually debug
Topic archived. No new replies allowed.