error C4996: 'std::_Uninitialized_copy0':與錯誤 LNK2001 無法解析的外部符號 "private: static class std::allocator

TinnCHEN發表於2019-04-16
/*
在構造簡易版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或者在類外定義

相關文章