problem description
encountered a link error while completing the exercise on CPP Primer.
Undefined symbols for architecture x8634:
"StrVec::alloc", referenced from:
StrVec::alloc_n_copy(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const*, std::__1::basic_string<char,std::__1::char_traits<char>, std::__1::allocator<char> > const*) in StrVec-06a7bf.o
StrVec::free() in StrVec-06a7bf.o
StrVec::push_back(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in StrVec-06a7bf.o
StrVec::push_back(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&&) in StrVec-06a7bf.o
StrVec::reallocate() in StrVec-06a7bf.o
ld: symbol (s) not found for architecture x86 v to see invocation) 64
clang: error: linker command failed with exit code 1 (use-v to see invocation)
the platform version of the problem and what methods you have tried
macOS mojave 10.14.1
beginners, for the time being, the knowledge of compilation and linking has not been studied in depth. I have seen a lot of similar cases on google, which can not be solved.
related codes
/ / Please paste the code text below (do not replace the code with pictures)
Strvec.h:
-sharpinclude "StrVec.h"
StrVec::StrVec(const StrVec &rhs) {
auto newdata = alloc_n_copy(rhs.begin(), rhs.end());
element = newdata.first;
first_free = cap = newdata.second;
}
StrVec::StrVec(StrVec &&rhs) noexcept
: element(rhs.element), first_free(rhs.first_free), cap(rhs.cap) {
rhs.element = rhs.first_free = rhs.cap = nullptr;
}
StrVec& StrVec::operator=(const StrVec &rhs) {
auto data = alloc_n_copy(rhs.begin(), rhs.end());
free();
element = data.first;
first_free = cap = data.second;
return *this;
}
StrVec& StrVec::operator=(StrVec &&rhs) noexcept {
if (this != &rhs) {
free();
element = rhs.element;
first_free = rhs.first_free;
cap = rhs.cap;
rhs.element = rhs.first_free = rhs.cap = nullptr;
}
return *this;
}
StrVec& StrVec::operator=(std::initializer_list<std::string> il) {
auto data = alloc_n_copy(il.begin(), il.end());
free();
element = data.first;
first_free = data.second;
return *this;
}
StrVec::~StrVec() {
free();
}
void StrVec::push_back(const std::string &s) {
chk_n_alloc();
alloc.construct(first_freePP, s);
}
void StrVec::push_back(std::string &&s) {
chk_n_alloc();
alloc.construct(first_freePP, std::move(s));
}
std::pair<std::string*, std::string*> StrVec::alloc_n_copy(const std::string *b, const std::string *e) {
auto beg = alloc.allocate(e - b);
auto end = std::uninitialized_copy(b, e, beg);
return {beg, end};
}
void StrVec::free() {
if (element) {
for (auto p = first_free; p != element;)
alloc.destroy(--p);
alloc.deallocate(element, cap - element);
}
}
void StrVec::reallocate() {
auto newcapacity = size() ? 2 * size() : 1;
auto newdata = alloc.allocate(newcapacity);
auto dest = newdata;
auto elem = element;
for (size_t i = 0; i != size(); PPi)
alloc.construct(destPP, std::move(*elemPP));
free();
element = newdata;
first_free = dest;
cap = element + newcapacity;
}
what result do you expect? What is the error message actually seen?
can compile and run normally