error C4996: 'std::_Uninitialized_copy0':与错误 LNK2001 无法解析的外部符号 "private: static class std::allocator

/*
在构造简易版vector<string>时遇到这两个问题,代码如下:
*/
#include<memory>
#include<iostream>
#include<string>
#include<utility>
#include<vector>

class StrVec {
public:
	StrVec() :elements(nullptr), first_free(nullptr), cap(nullptr) { }
	StrVec(const StrVec&);
	StrVec(std::initializer_list<std::string>);
	StrVec &operator=(const StrVec&);
	~StrVec();
	void push_back(const std::string);
	size_t size() const { return first_free - elements; }
	size_t capicity() const { return cap - elements; }
	void reserve(size_t n);
	void resize(size_t n);
	string show(std::vector<std::string>::size_type i);
	std::string *begin() const { return elements; }
	std::string *end() const { return first_free; }
private:
	static std::allocator<std::string> alloc;
	void chk_n_alloc() { if (size() == capicity()) reallocate(); }
	std::pair<std::string*, std::string*> alloc_n_copy(const std::string*, const std::string*);
	void free();
	void reallocate();
	std::string *elements;
	std::string *first_free;
	std::string *cap;
};

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

std::pair<std::string*, std::string*> StrVec::alloc_n_copy(const std::string *s1, const std::string *s2) {
	auto data = alloc.allocate(s2 - s1);
	return { data, uninitialized_copy(s1, s2, 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& st) {
	auto newdata = alloc_n_copy(st.begin(), st.end());
	elements = newdata.first;
	first_free = newdata.second;
	cap = newdata.second;
}

StrVec::StrVec(std::initializer_list<std::string> s) {
	auto newdata = alloc_n_copy(s.begin(), s.end());
	elements = newdata.first;
	first_free = newdata.second;
	cap = newdata.second;
}

StrVec::~StrVec() {
	this->free();
}

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

void StrVec::reallocate() {
	auto newcapcity = this->size() ? 2 * this->size() : 1;
	auto newdata = alloc.allocate(newcapcity);
	auto dest = newdata;
	auto elem = this->elements;
	for (size_t i = 0; i < this->size(); ++i) {
		alloc.construct(dest++, std::move(*elem++));
	}
	this->elements = newdata;
	this->first_free = dest;
	this->cap = this->elements + newcapcity;
}

void StrVec::reserve(size_t n) {
	if (n > this->capicity()) {
		auto newdata = alloc.allocate(n);
		auto dest = newdata;
		auto elem = this->elements;
		for (size_t i = 0; i < this->size(); ++i) {
			alloc.construct(dest++, std::move(*elem++));
		}
		this->elements = newdata;
		this->first_free = dest;
		this->cap = this->elements + n;
	}
}

void StrVec::resize(rsize_t n) {
	if (n <= this->capicity()) {
		if (n > this->size()) {
			auto beg = this->first_free;
			for (size_t i = 0; i != n - (this->first_free - this->elements); ++i) {
				alloc.construct(beg++);
			}
			this->first_free = beg;
		}
		else if (n < this->size()) {
			auto beg = this->first_free;
			for (; beg != this->first_free - n;) {
				alloc.destroy(--beg);
			}
			this->first_free = beg;
		}
	}
}

std::string StrVec::show(std::vector<std::string>::size_type i) {
	return *(elements + i);

首先,C4996这个问题,解决方案:
1、打开project的属性
2、打开c/c++目录
3、点击预处理器
4、在右侧表单中编辑第一条“预处理器定义”
5、将报错提示中的问题填入,我的时_SCL_SECURE_NO_WARNINGS
6、应用确认
或者在头文件前加上:#pragma warning(disable:4996)

接着LNK2001 无法解析的外部符号 "private: static class std::allocator:
原因在于第26行,类内静态变量未初始化,删去static或者在类外定义

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值