Skip to content

Commit

Permalink
fix 0709 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterzhao committed Jul 8, 2016
1 parent 49aac49 commit 6dee4cd
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
15 changes: 15 additions & 0 deletions error/error0709.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error1:
passing 'const EasySTL::vector<std::basic_string<char>, EasySTL::alloc>' as 'this'
argument of 'T* EasySTL::vector<T, Alloc>::end()
[with
T = std::basic_string<char>;
Alloc = EasySTL::alloc;
EasySTL::vector<T, Alloc>::iterator = std::basic_string<char>*]'
discards qualifiers [-fpermissive]
size_type size() const { return size_type(end() - begin());}


answer:
const对象只能调用const成员函数;在一个加了const限定符的成员函数中,不能够调用 非const成员函数。
因为如果调用了非const成员函数,就违反了const成员函数不改变对象的规定。
所以为begin() end() 函数添加const
2 changes: 1 addition & 1 deletion test/testFun.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace EasySTL {
namespace Test{
template<class Container>
void print_container(Container& container, const std::string& name = ""){//不是每一个容器都有const_iterator
std::cout << "Container " << name << " :";
std::cout << name << " :";
for (auto val : container){
std::cout << val << " ";
}
Expand Down
28 changes: 23 additions & 5 deletions test/vectorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,29 @@ namespace EasySTL {
void testCase1(){
//std::string old_SGI_vector = "old_SGI_vector";
//std::string new_EasySTL_vector = "new_EasySTL_vector";
stdVec<std::string> v1(10, "zzz");
easyVec<std::string> v2(10, "zzz");
EasySTL::Test::print_container(v1, "SGI_vector");
EasySTL::Test::print_container(v2, "EasySTL_vector");
assert(EasySTL::Test::container_equal(v1, v2));
//stdVec<std::string> v1(10, "zzz");
easyVec<std::string> v2(3, "zm");
//EasySTL::Test::print_container(v1, "SGI_vector");
EasySTL::Test::print_container(v2, "construct");

std::string a = "huyun";
v2.push_back(a);

EasySTL::Test::print_container(v2, "push_back");

v2.pop_back();

EasySTL::Test::print_container(v2, "pop_back");

auto p = v2.begin();

p = p + 2;

std::cout << "the second one" <<*p <<std::endl;


//assert(EasySTL::Test::container_equal(v1, v2));

}
}
}
6 changes: 3 additions & 3 deletions vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "allocator.h"
#include "algorithm.h"
//#include "iterator.h"
#include "iterator.h"
//#include "construct.h"
#include "uninitialized.h"

Expand Down Expand Up @@ -46,8 +46,8 @@ namespace EasySTL {
}

public:
iterator begin() { return start_;}
iterator end() { return finish_;}
iterator begin() const { return start_;}
iterator end() const { return finish_;}
size_type size() const { return size_type(end() - begin());}
size_type capacity() const { return size_type(end_of_storage_ - begin());}
bool empty() const { return begin() == end();}
Expand Down

0 comments on commit 6dee4cd

Please sign in to comment.