compilation error Vec template class

hello to everyone

I am tring to compile an example simplification of a template vector class [called Vec] from the book

Accelerated C++
Practical Programming by Example
by Andrew Koenig and Barbara E. Moo

Pasing the details of the definition of the Vec class

I have for the Vec object instatiation:

Vec.h
explicit Vec(size_type n, const T& t = T());

Vec.cpp
template<class T>
Vec<T>::Vec(size_type n, const T& t = T()) { create(n, t);}

and in the main program

Vec<double> vi(size,1); for initialization

when I compile i get the error:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -ovec.o ..\vec.cpp
..\vec.cpp:173:42: error: default argument given for parameter 2 of 'Vec<T>::Vec(Vec::size_type, const T&)'
..\/vec.h:67:14: error: after previous specification in 'Vec<T>::Vec(Vec::size_type, const T&)'
Build error occurred, build is stopped
Time consumed: 718 ms.

with the g++ compiler


please can you help?
thnks
christos
..\vec.cpp:173:42: error: default argument given for parameter 2 of 'Vec<T>::Vec(Vec::size_type, const T&)'


You shouldnt use default values for function implementations just the definitions.

Vec<T>::Vec(size_type n, const T& t = T()) { create(n, t);}
should be
Vec<T>::Vec(size_type n, const T& t) { create(n, t);}

but with the default value still in vec.h file
Breadman
tnks for your prompt reply. After the correction when i try to initiate in main with

Vec<double>::size_type size;
cin>>size;

// initialize the array with zero values
Vec<double> vi(size,0.0);

I am getting the error:

g++ -O0 -g3 -Wall -c -fmessage-length=0 -omain.o ..\main.cpp
g++ -oaccel_cpp_vec_class.exe vec.o main.o
main.o: In function `main':
G:\eclipse_workspace\accel_cpp_vec_class\Debug/../main.cpp:32: undefined reference to `Vec<double>::Vec(unsigned int, double const&)'
G:\eclipse_workspace\accel_cpp_vec_class\Debug/../main.cpp:32: undefined reference to `Vec<double>::~Vec()'
collect2: ld returned 1 exit status

is there something wrong with my initialization or the destructor?

tnks a lot for your help

christos
dear all,

I am still having issues to build the Vec example from

I am tring to compile an example simplification of a template vector class [called Vec] from the book

Accelerated C++
Practical Programming by Example
by Andrew Koenig and Barbara E. Moo

Please take a look

can you help?

#ifndef _GUARD_VEC_H_
#define _GUARD_VEC_H_

#include <algorithm>
#include <cstdlib>
#include <memory>


template <class T> class Vec {
public:

/* *************************************************
* Class Interface
*************************************************** */

/* Typedefs */
typedef T* iterator;
typedef const T* const_iterator;
typedef size_t size_type;
typedef T value_type;





/* **************************************************
* Costructors and distractor
*************************************************** */

// constructor #1 empty constructor
// or Vec<T>(){create(); }
Vec(); //compiler creates default



explicit Vec(size_type n, const T& t = T());


// Constructor #4 Copy constructor a class member function
// takes single arg that has the same type as class
Vec(const Vec& v); //compiler creates default


// Destractor
// void args delete objects deallocate memory
virtual ~Vec();


/* **********************************************
* Overloading operators
*********************************************** */

// Assignment oveloading op
Vec& operator=(const Vec&); // compiler creates default

//Index overloading operator
// read write
T& operator[](size_type i) { return data[i]; }
// only read
const T& operator[](size_type i) const { return data[i]; }



/* **************************************************
CLASS MEMBER FUNCTIONS
*************************************************** */

// INCREASE THE STACK ADD ELEMENTS
void push_back(const T& t) {
if (avail == limit)
grow();
unchecked_append(t);
}


// remove an element
iterator erase(iterator position) {
iterator next = position;

if (position < avail) {
++next;
}

if (position >= data) {
shrink(position);
unchecked_remove(position);
}

return next;
}


void insert(iterator position,const_iterator first,const_iterator last);


// SIZE METHOD
size_type size() const { return avail - data; }

// BEGIN ITERATOR METHOD [POINTER TO START]
iterator begin() { return data; }
const_iterator begin() const { return data; }

// END ITERATOR METHOD [POINTER TO END ONE PAST]
iterator end() { return avail; }
const_iterator end() const { return avail; }

bool empty() const {return data == avail;}



private:

iterator data; // first element in the Vec
iterator avail; // (one past) the last element in the Vec
iterator limit; // (one past) the allocated memory

// facilities for memory allocation
std::allocator<T> alloc; // object to handle memory allocation

// allocate and initialize the underlying array
void create();
void create(size_type, const T&);
void create(const_iterator, const_iterator);

// destroy the elements in the array and free the memory
void uncreate();

// support functions for push_back
void grow();
void shrink(iterator position);

void unchecked_append(const T&);
void unchecked_remove(iterator position);

};

#endif /* VEC_H_ */



main.cpp

#include <iostream>
#include <cstdlib>
#include <memory>
#include <algorithm>

#include "vec.h"

using std::allocator;
using std::cout;
using std::endl;
using std::cin;
using std::size_t;



int main(){

cout<<"give size of vec:"<<endl;

// give size of array
Vec<double>::size_type size;
cin>>size;

// initialize the array with zero values
Vec<double> vi(10,0);
//Vec<double> vi(size);
//Vec<double> vi;


// check at output
for(Vec<double>::const_iterator it=vi.begin();vi.end();++it){
cout<<*it<<endl;
//}


}
return 0;
}
.... and

vec.cpp

#include <iostream>
#include <cstdlib>
#include <memory>
#include <iterator>

#include "vec.h"


template <class T>
Vec<T>& Vec<T>::operator=(const Vec& rhs){
// check for self-assignment
if (&rhs != this) {

// free the array in the left-hand side
uncreate();

// copy elements from the right-hand to the left-hand side
create(rhs.begin(), rhs.end());
}
return *this;
}



template <class T>
void Vec<T>::create(){
data = avail = limit = NULL;
}


template <class T>
void Vec<T>::create(size_type n, const T& val){

data = alloc.allocate(n);

avail=limit=data+n; // correction here
//limit = avail = data + n;

std::uninitialized_fill(data, avail, val);
//uninitialized_fill(data, limit, val);
}



template <class T>
void Vec<T>::create(const_iterator i, const_iterator j){
data = alloc.allocate(j - i);
limit = avail = std::uninitialized_copy(i, j, data);
}



// clear the entire Vec
template <class T>
void Vec<T>::uncreate(){
if (data) {

// destroy (in reverse order) the elements that were constructed
iterator it = avail;

while (it != data)
alloc.destroy(--it);

// return all the space that was allocated
alloc.deallocate(data, limit - data);
}
// reset pointers to indicate that the Vec is empty again
data=limit=avail=0;

}


template <class T>
void Vec<T>::grow()
{
// when growing, allocate twice as much space as currently in use
size_type new_size = max(2 * (limit - data), ptrdiff_t(1));

// allocate new space and copy existing elements to the new space
iterator new_data = alloc.allocate(new_size);
// iterator new_avail = uninitialized_copy(data, avail, new_data); // change here!!!
// with
std::uninitialized_copy(data, avail, new_data); // dropping return
iterator new_avail = new_data + (avail - data); // new avail calc


// return the old space
uncreate();

// reset pointers to point to the newly allocated space
data = new_data;
avail = new_avail;
limit = data + new_size;
}


// assumes avail points at allocated, but uninitialized space
template <class T>
void Vec<T>::unchecked_append(const T& val)
{
alloc.construct(avail++, val);
}

template <typename T>
void Vec<T>::shrink(iterator position) {
size_type new_size = std::max(limit - data - 1, ptrdiff_t(1));

iterator new_data = alloc.allocate(new_size);
iterator second_portion = std::uninitialized_copy(data, position - 1, new_data);
iterator new_avail = std::uninitialized_copy(position + 1, avail, second_portion);

avail = std::uninitialized_copy(new_data, new_avail, data);

alloc.deallocate(new_data, new_size);

--limit;
}



template <typename T>
void Vec<T>::unchecked_remove(iterator position) {
alloc.destroy(position);
}

template <typename T>
void Vec<T>::insert(iterator position,const_iterator first,const_iterator last) {

//if (position == avail)
if (position >= data && position <=avail) { // change here !! value check!

/* check ***************************************************** */
size_type positionIndex = position - data; // location calculation

while(last-first>limit-avail){
grow();
position = data + positionIndex; // repositioning position
}
/* ************************************************************** */
if(position==avail){

avail = std::uninitialized_copy(first, last, position);
}
else {
iterator new_position = position + (last - first);
// avail = std::uninitialized_copy(position, avail, new_position); change here!

std::uninitialized_copy(position, avail, new_position); // dropping return
avail = new_position + (avail - position); // new avail calc

std::uninitialized_copy(first, last, position);
}
}
}


// constructor #1 empty constructor
template <class T>
Vec<T>::Vec() { create(); }


// constructor #2 and &3 initiator constructor
template<class T>
Vec<T>::Vec(size_type n, const T& t) { create(n, t);}


// Constructor #4 Copy constructor a class member function
// takes single arg that has the same type as class
template<class T>
Vec<T>::Vec(const Vec& v) { create(v.begin(), v.end());}


// Destructor
// void args delete objects deallocate memory
template<class T>
Vec<T>::~Vec() { uncreate(); }


[code] "Please use code tags" [/code]

vec.cpp
That's the problem. The compiler needs to see the implementation of the templates, so it should be in the header file.
Last edited on
well i have followed your suggestion
and changed the code as

vec.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

#ifndef _GUARD_VEC_H_
#define _GUARD_VEC_H_

#include <algorithm>
#include <cstdlib>
#include <memory>


template <class T> class Vec {
public:

	/* *************************************************
	 * Class Interface
	 ***************************************************  */

	/* Typedefs */
    typedef T* iterator;
    typedef const T* const_iterator;
    typedef size_t size_type;
    typedef T value_type;


    /* **************************************************
     *
     * If not defined they will be created by the compiler
     * if any constructor is defined then the default constructor should
     * be also defined!
     * in general the compiler will default define a default, a copy, an assignment
     * operator and a destructor
     * but this is generally dangerous !!!
     *
     * RULE OF THREE [3]
     * BELOW THE TYPE CLASS NAME IS OMITTED
     * T::T() DEFAULT CONSTRCT
     * T::~T() DISTRACT RULE 1
     * T::T(const T&) COPY CONSTR RULE 2
     * T::operator=(const T&) ASSIGNMENT OVELOADING OP RULE 3
      *************************************************** */




    /* **************************************************
         * Costructors and distractor
       *************************************************** */

    // constructor #1 empty constructor
    Vec() { create(); }                                                    //compiler creates default


    // Two constructors 1. Single arg size_type 2. Two args size_type and const T& value
    // case of single arg allocate memory but not create the object
    // explicit keyword makes sense only in costructor definition that takes single arg
    // using explicit the compiler will use the constructor only if the caller expressly
    // invokes the constructor like Vec<int> vi(100);
    //

    // constructor #2 and &3 initiator constructor
    // Doing so tells the compiler to use that constructor
    // only to construct objects explicitly. The compiler
    // will not use an explicit constructor to create objects
    // implicitly by converting operands in expressions or function calls.

    // These constructors use their argument value
    // to determine how many elements to allocate.
    // The constructor argument determines the structure
    // of the object, but not its value.


    // T()=type conversion operator
    // could be a class member: if target type is double
    // then operator double() could convert from a type to double
    // constructor #2 and &3 initiator constructor
    Vec(size_type n, const T& t=T()) { create(n, t);}


    // Constructor #4 Copy constructor a class member function
    // takes single arg that has the same type as class
    Vec(const Vec& v) { create(v.begin(), v.end());}


    // Destructor
    // void args delete objects deallocate memory
    ~Vec() { uncreate(); }

    /* **********************************************
     * Overloading operators
      *********************************************** */

    // Assignment oveloading op
    Vec& operator=(const Vec&);                              // compiler creates default

    //Index overloading operator
    // read write
    T& operator[](size_type i) { return data[i]; }
    // only read
    const T& operator[](size_type i) const { return data[i]; }



    /* **************************************************
            CLASS MEMBER FUNCTIONS
       *************************************************** */

    // INCREASE THE STACK ADD ELEMENTS
    void push_back(const T& t) {
        if (avail == limit)
            grow();
        unchecked_append(t);
    }


    // remove an element
    iterator erase(iterator position) {
             iterator next = position;

             if (position < avail) {
               ++next;
             }

             if (position >= data) {
               shrink(position);
               unchecked_remove(position);
             }

             return next;
           }


    void insert(iterator position,const_iterator first,const_iterator last);


    // SIZE METHOD
    size_type size() const { return avail - data; }

    // BEGIN ITERATOR METHOD [POINTER TO START]
    iterator begin() { return data; }
    const_iterator begin() const { return data; }

    // END ITERATOR METHOD [POINTER TO END ONE PAST]
    iterator end() { return avail; }
    const_iterator end() const { return avail; }

    bool empty() const {return data == avail;}



private:

    iterator data;  // first element in the Vec
    iterator avail; // (one past) the last element in the Vec
    iterator limit; // (one past) the allocated memory

    // facilities for memory allocation
    std::allocator<T> alloc;   // object to handle memory allocation

    // allocate and initialize the underlying array
    void create();
    void create(size_type, const T&);
    void create(const_iterator, const_iterator);

    // destroy the elements in the array and free the memory
    void uncreate();

    // support functions for push_back
    void grow();
    void shrink(iterator position);

    void unchecked_append(const T&);
    void unchecked_remove(iterator position);

};



with main

main.cpp


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

#include <iostream>
#include <cstdlib>
#include <memory>
#include <algorithm>

#include "vec.h"

using std::allocator;
using std::cout;
using std::endl;
using std::cin;
using std::size_t;



int main(){

	cout<<"give size of vec:"<<endl;

	// give size of array
	 Vec<double>::size_type size;
	 cin>>size;

	// initialize the array with zero values
	Vec<double> vi(10,0);
	//Vec<double> vi(size);
    //Vec<double> vi;


	// check at output
	for(Vec<double>::const_iterator it=vi.begin();vi.end();++it){
	cout<<*it<<endl;
	//}


	}
	return 0;
}



and the vec.cpp as

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

#include <iostream>
#include <cstdlib>
#include <memory>
#include <iterator>

#include "vec.h"


template <class T>
Vec<T>& Vec<T>::operator=(const Vec& rhs){
    // check for self-assignment
    if (&rhs != this) {

        // free the array in the left-hand side
        uncreate();

        // copy elements from the right-hand to the left-hand side
        create(rhs.begin(), rhs.end());
    }
    return *this;
}



template <class T>
void Vec<T>::create(){
    data = avail = limit = NULL;
}


template <class T>
void Vec<T>::create(size_type n, const T& val){

    data = alloc.allocate(n);

    avail=limit=data+n;                                            // correction here
    //limit = avail = data + n;

    std::uninitialized_fill(data, avail, val);
   //uninitialized_fill(data, limit, val);
}



template <class T>
void Vec<T>::create(const_iterator i, const_iterator j){
    data = alloc.allocate(j - i);
    limit = avail = std::uninitialized_copy(i, j, data);
}



// clear the entire Vec
template <class T>
void Vec<T>::uncreate(){
    if (data) {

        // destroy (in reverse order) the elements that were constructed
        iterator it = avail;

        while (it != data)
           alloc.destroy(--it);

        // return all the space that was allocated
        alloc.deallocate(data, limit - data);
    }
    // reset pointers to indicate that the Vec is empty again
    data=limit=avail=0;

}


template <class T>
void Vec<T>::grow()
{
    // when growing, allocate twice as much space as currently in use
    size_type new_size = max(2 * (limit - data), ptrdiff_t(1));

    // allocate new space and copy existing elements to the new space
    iterator new_data = alloc.allocate(new_size);
   // iterator new_avail = uninitialized_copy(data, avail, new_data);  // change here!!!
   // with
    std::uninitialized_copy(data, avail, new_data);  // dropping return
    iterator new_avail = new_data + (avail - data);  // new avail calc


    // return the old space
    uncreate();

    // reset pointers to point to the newly allocated space
    data = new_data;
    avail = new_avail;
    limit = data + new_size;
}


// assumes avail points at allocated, but uninitialized space
template <class T>
void Vec<T>::unchecked_append(const T& val)
{
    alloc.construct(avail++, val);
}

template <typename T>
void Vec<T>::shrink(iterator position) {
    size_type new_size = std::max(limit - data - 1, ptrdiff_t(1));

    iterator new_data = alloc.allocate(new_size);
    iterator second_portion = std::uninitialized_copy(data, position - 1, new_data);
    iterator new_avail = std::uninitialized_copy(position + 1, avail, second_portion);

    avail = std::uninitialized_copy(new_data, new_avail, data);

    alloc.deallocate(new_data, new_size);

    --limit;
  }



template <typename T>
void Vec<T>::unchecked_remove(iterator position) {
    alloc.destroy(position);
  }

template <typename T>
void Vec<T>::insert(iterator position,const_iterator first,const_iterator last) {

    //if (position == avail)
    if (position >= data && position <=avail) { // change here !! value check!

  /* check ***************************************************** */
    	size_type positionIndex = position - data;  // location calculation

    	while(last-first>limit-avail){
    		grow();
    		position = data + positionIndex;  // repositioning position
	       }
 /* ************************************************************** */
    	if(position==avail){

      avail = std::uninitialized_copy(first, last, position);
    }
    else {
      iterator new_position = position + (last - first);
 //   avail = std::uninitialized_copy(position, avail, new_position);  change here!

      std::uninitialized_copy(position, avail, new_position);  // dropping return
      avail = new_position + (avail - position);  // new avail calc

      std::uninitialized_copy(first, last, position);
    }
  }
}


when i build i get

main.o: In function `Vec':
G:\eclipse_workspace\accel_cpp_vec_class\Debug/..//vec.h:81: undefined reference to `Vec<double>::create(unsigned int, double const&)'
main.o: In function `~Vec':
G:\eclipse_workspace\accel_cpp_vec_class\Debug/..//vec.h:91: undefined reference to `Vec<double>::uncreate()'
collect2: ld returned 1 exit status

still i am confused

what stupid thing i am doing here?



thks for the advise
Templates and multiple-file projects (last title) http://www.cplusplus.com/doc/tutorial/templates/
the implementation (definition) of a template class or function must be in the same file as its declaration
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
After hours of poking around i havent manage to see what to do with this error message:
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>'

can you help

tnks
In instantiation of 'std::back_insert_iterator<Vec<char> >':
error: no type named 'const_reference' in 'class Vec<char>'

That refers to
1
2
3
4
//Vec<char> data;
    Str(const char* cp) {
        std::copy(cp, cp + std::strlen(cp), std::back_inserter(data));
    }
In order to create a back_inserter iterator you need to define some types in your class (like an interfaz)
Check out iterator_traits and stl_iterator.h to know what do you need.
1
2
3
4
5
class Vec{
public:
  typedef T& reference;
  typedef const reference const_reference;
}
Topic archived. No new replies allowed.