Writing to File + some Matrix issue

iam having a bit diffcult time why i cant write to text file object
lets say i cant add some set method in mystring class
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
class Mystring
{public:
Mystring(char *s)
{
ptr = new char[strlen(s) + 1];
assert (ptr);
strcpy(ptr, s);
}
~ Mystring()
{ if(ptr) delete [] ptr; }
private:
char *ptr;
};
int main(){
int i=0;
fstream file;
file.open("data.dat",ios::in|iosbin);
while(i<10){
 Mystring s("jjjd");
file<<s;
delete s;
i++;
}
file.close();
return 0;

Last edited on
sorry i have another quastion :
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
template <class DataT>
class ArrayOb
{
protected:
	DataT info;
public:
	ArrayOb<DataT>(DataT val) : info(val) {}
};


template <class DataT>
class SparseArray
{
	int size;
	ArrayOb <DataT> **ptr;
public:
	SparseArray(int length);
	~SparseArray();
	bool store(int i, DataT val);
	DataT &operator[ ](int i) :
	void dense();
};
template<class DataT>
 SparseArray<DataT>::SparseArray(int length) :size(length) {
	if (size > 0) {
		ptr = new ArrayOb* [size];
		for (int i = 0; i < size; i++) {
			ptr[i] = ArrayOb(const DataT &val) // how can i put here???
		}
	}
	template<class DataT>
SparseArray<dataT>::~SparseArray() {
	for(int i = 0; i < size; i++){
		delete[] ptr[i];
}
	delete[]ptr;
}


whats wrong here in the constractur that i done? i need to put in evry i position in array some object from ArrayOb
Last edited on
In your first post, at line 20 you haven't defined a << operator for Mystring. I suggest that you define osteam & operator << (ostream &, const Mystring &)

In your second post, what is the point of ArrayOb? All it does is put a wrapper around the template class. I'd get rid of it and use the template directly.

Line 28. ptr is a pointer, so to fix the syntax error, you'd need to assign it to another pointer. But a pointer to what? And what value?

Take a step back. What are you trying to accomplish here? If you're storing a sparse array then you need to store both the index and the value somehow. How are you planning to do that? Figure out how you [i]want
the class to work first. Write comments in the class declaration to describe this. Then write code to realize your intention. Doing it this way will break the problem down into a design part, and an implementation part. It's much easier to write the code when you have a clear idea ahead of time about what the code should do.
Topic archived. No new replies allowed.