在地图上飞行 2015-08-31 08:26 采纳率: 25%
浏览 1676

学习C++ primer中的编译错误

 #include<iostream>
#include<memory>
#include<initializer_list>
#include<string>
#include<utility>

using namespace std;

class StrVec{
private:
    static allocator<string> alloc;   //分配元素
    void chk_n_alloc(){if (size()==capacity()) reallocate();}
    pair<string*,string*> alloc_n_copy(const string*,const string*);
    void free();
    void reallocate();
    string *elements;
    string *first_free;
    string *cap;
public:
    StrVec():elements(nullptr),first_free(nullptr),cap(nullptr){}
    StrVec(const StrVec&);
    StrVec &operator=(const StrVec&);
    ~StrVec();

    void push_back(const string&);
    size_t size() const{return first_free-elements;}
    size_t capacity() const{return cap-elements;}
    void reserve(size_t ); //并不改变容器中元素的数量,它仅仅影响StrVec预先分配
    string *begin() const{return elements;}
    string *end() const{return first_free;}
    void resize(size_t);
    void resize(size_t,const string&);
};

void StrVec::push_back(const string &s){
    chk_n_alloc();
    alloc.construct(first_free++,s);
}

pair<string*,string*> StrVec::alloc_n_copy(const string *b,const string *e){
    auto data=alloc.allocate(e-b);
    return {data,uninitialized_copy(b,e,data)};
}
void StrVec::free(){
    if(elements){
        for(auto p=first_free;p!=elements;)
            alloc.destroy(--p);
        alloc.deallocate(elements,cap-elements);
    }
}
StrVec::StrVec(const StrVec &s){
    auto newdata=alloc_n_copy(s.begin(),s.end());
    elements=newdata.first;
    first_free=cap=newdata.second;
}

StrVec::~StrVec(){ free();}

StrVec& StrVec::operator=(const StrVec &s){
    auto data=alloc_n_copy(s.begin(),s.end());
    free();
    elements=data.first;
    first_free=cap=data.second;
    return *this;
}

void StrVec::reallocate(){
    auto newcapacity=size()?2*size():1;
    auto newdata=alloc.allocate(newcapacity);
    auto dest=newdata;
    auto elem=elements;
    for(size_t i=0;i!=size();++i)
        alloc.construct(dest++,std::move(*elem++));
    //释放旧的内存空间
    free();
    //更新新的内存空间
    elements=newdata;
    first_free=dest;
    cap=elements+newcapacity;
}

void StrVec::reserve(size_t sz){
    if(sz>capacity()){
                    auto newdata=alloc.allocate(sz);
                    auto dest=newdata;
                    auto elem=elements;
        for(size_t i=0;i!=size();++i)
            alloc.construct(dest++,std::move(elem++));
        free();
        elements=newdata;
        first_free=dest;
        cap=elements+sz;
    }

}

void StrVec::resize(size_t n){
    if(n>size()){
                    while(size()<n) push_back("");
    }
    else if(n<size()){
                    while(size()>n)
                    alloc.destroy(--first_free);
    }
}
void StrVec::resize(size_t n,const string &s){
    if(n>size()){
                        while(n<size())
                    push_back(s);
    }
    else if(n<size()){
                    while(n>size())
                    alloc.destroy(--first_free);
    }
}
int main(int argc, char  *argv[])
{

    return 0;
 }




编译产生的错误:120 4 d:\dev-cpp\mingw64\lib\gcc\x86_64-w64-mingw32\4.8.1\include\c++\ext\new_allocator.h [Error] no matching function for call to 'std::basic_string::basic_string(std::basic_string*)',应该如何解决

  • 写回答

3条回答 默认 最新

  • oyljerry 2015-08-31 09:03
    关注

    你的编译器对string支持怎么样,换个编译器试试。

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?