tnks very much for the reply
I have resolved my Vec class issue.
Trying to go further with the examples I have the Str 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 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
|
#ifndef _GUARD_STR_H_
#define _GUARD_STR_H_
#include <algorithm>
#include <cstdlib>
#include <memory>
#include <iterator>
#include <cstdlib>
#include <memory>
#include <cstring> // c library for strings
#include "vec.h"
using std::ostream;
using std::strlen;
using std::iterator;
using std::back_inserter;
#include "vec.h"
class Str {
// input operator reads from istream and writes to Str
// need to have write access to Str private member data
// for this reason it is declared as friend to Str
friend std::istream& operator>>(std::istream&, Str&);
public:
/* ******************************************************
* Typedefs
******************************************************* */
typedef Vec<char>::size_type size_type;
/* ******************************************************
* Constructors
* not define a copy assignment constructors & destructor
*
* The class has no destructor the memory management is done
* through Vec class
*
* A class that has no destructor doesnot need a copy assignment
* constructors
******************************************************* */
// Constructor #1 Default
Str() { }
// Constructor #2 Initiator taking a char
Str(size_type n, char c): data(n, c) { }
// Constructor #3 Initiator taking a string literal as a char*
//Str(const char* cp);
Str(const char* cp) {
std::copy(cp, cp + std::strlen(cp), std::back_inserter(data));
}
virtual ~Str(){}
// Constructor #4 Initiator creating a Str from 2 iterators start-end
template<class In> Str(In i, In j) {
std::copy(i, j, std::back_inserter(data));
}
/* ***********************************************
* Overloading operators
********************************************* */
// operator #1
//Str& operator+=(const Str& s);
// operator #1
Str& operator+=(const Str& s) {
std::copy(s.data.begin(), s.data.end(),
std::back_inserter(data));
return *this;
}
// operator #2
//Str operator+(const Str& s, const Str& t);
// operator #3-4
char& operator[](size_type i) { return data[i]; }
const char& operator[](size_type i) const { return data[i]; }
/* ***********************************************
* Overloading operators
********************************************* */
size_type size() const { return data.size(); }
private:
Vec<char> data;
};
// output operator not Str member
//std::ostream& operator<<(std::ostream&, const Str&);
ostream& operator<<(ostream& os, const Str& s){
for (Str::size_type i = 0; i != s.size(); ++i)
os << s[i];
return os;
}
//operator #2
Str operator+(const Str& s, const Str& t) {
Str r = s;
r += t;
return r;
}
#endif /* STR_H_ */
|
which initializes in main
with
Vec<Str> vs(5,"this is a test");
when I build i get
**** Internal Builder is used for build ****
g++ -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ..\main.cpp
In file included from c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algobase.h:69:0,
from c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/char_traits.h:41,
from c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/ios:41,
from c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/ostream:40,
from c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/iostream:40,
from ..\main.cpp:9:
c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_iterator.h: In instantiation of 'std::back_insert_iterator<Vec<char> >':
..\/str.h:76:68: instantiated from here
c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_iterator.h:420:7: error: no type named 'const_reference' in 'class Vec<char>'
In file included from c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/char_traits.h:41:0,
from c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/ios:41,
from c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/ostream:40,
from c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/iostream:40,
from ..\main.cpp:9:
c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algobase.h: In static member function 'static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = const char*, _OI = std::back_insert_iterator<Vec<char> >]':
c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algobase.h:404:70: instantiated from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false, _II = const char*, _OI = std::back_insert_iterator<Vec<char> >]'
c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algobase.h:442:39: instantiated from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false, _II = const char*, _OI = std::back_insert_iterator<Vec<char> >]'
c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algobase.h:474:18: instantiated from '_OI std::copy(_II, _II, _OI) [with _II = const char*, _OI = std::back_insert_iterator<Vec<char> >]'
..\/str.h:76:69: instantiated from here
c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_algobase.h:349:8: error: no match for 'operator=' in '__result.std::back_insert_iterator<_Container>::operator* [with _Container = Vec<char>, std::back_insert_iterator<_Container> = std::back_insert_iterator<Vec<char> >]() = * __first'
c:\rtools\mingw\bin\../lib/gcc/mingw32/4.5.0/include/c++/bits/stl_iterator.h:396:5: note: candidate is: std::back_insert_iterator<Vec<char> >& std::back_insert_iterator<Vec<char> >::operator=(const std::back_insert_iterator<Vec<char> >&)
Build error occurred, build is stopped
Time consumed: 858 ms.
can you help?
tnks