gpt4 book ai didi

c++ - 使用我自己的迭代方法的 undefined reference

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:54:06 25 4
gpt4 key购买 nike

我为我的 String 类声明并实现了一些迭代方法:begin()end() 等...但是当我尝试在测试程序中使用它们,但我得到了所有它们的 undefined reference 。

String.h:

public:

typedef char* iterator;
typedef const char* const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;

iterator begin();
iterator begin() const;
iterator end();
iterator end() const;
const_iterator cbegin();
const_iterator cend();
reverse_iterator rbegin();
reverse_iterator rbegin() const;
reverse_iterator rend();
reverse_iterator rend() const;
const_reverse_iterator crbegin();
const_reverse_iterator crend();

private:
char* string_;

String.cpp:

inline String::iterator String::begin() {
return string_;
}

inline String::iterator String::begin() const {
return string_;
}

inline String::iterator String::end() {
return string_ + length_ - 1;
}

inline String::iterator String::end() const {
return string_ + length_ - 1;
}

inline String::const_iterator String::cbegin() {
return string_;
}

inline String::const_iterator String::cend() {
return string_ + length_ - 1;
}

inline String::reverse_iterator String::rbegin() {
return reverse_iterator(end());
}

inline String::reverse_iterator String::rbegin() const {
return reverse_iterator(end());
}

inline String::reverse_iterator String::rend() {
return reverse_iterator(begin());
}

inline String::reverse_iterator String::rend() const {
return reverse_iterator(begin());
}

inline String::const_reverse_iterator String::crbegin() {
return reverse_iterator(end());
}

inline String::const_reverse_iterator String::crend() {
return reverse_iterator(begin());
}

怎么了?

最佳答案

undefined reference ?但它编译得很好。我添加了一个构造函数:

String(const char *cc) {
string_ = new char[length_ = strlen(cc) + 1];
strcpy(string_, cc);
}

当然,添加了一个数据成员作为

int _length;

我的测试程序如下:

字符串 aa("abc");String::iterator itr = aa.begin();cout << itr << endl;

打印“abc”没问题!

关于c++ - 使用我自己的迭代方法的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22962159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com