gpt4 book ai didi

c++ - C++成员函数自定义类型返回 'does not name a type'错误

转载 作者:行者123 更新时间:2023-11-28 06:18:06 25 4
gpt4 key购买 nike

在我的类中,我使用“using”关键字“bigvalue_t”声明了一个类型,并尝试创建一个返回相同类型 (to_vector) 的函数。我通过 gcc 返回了这个错误:

 g++ -g -O0 -Wall -Wextra -std=gnu++11 -c bigint.cpp
bigint.cpp:45:1: error: ‘bigvalue_t’ does not name a type
bigvalue_t bigint::to_vector (string& strval) {
^

这是我的头文件中的类:

class bigint {
friend ostream& operator<< (ostream&, const bigint&);
private:
long long_value {};
using unumber = unsigned long;
using digit_t = unsigned char;
using bigvalue_t = vector<digit_t>;
bool negative;
bigvalue_t big_value;
string to_string (unumber& num);
bigvalue_t to_vector (string& strval);
using quot_rem = pair<bigint,bigint>;
friend quot_rem divide (const bigint&, const bigint&);
friend void multiply_by_2 (unumber&);
friend void divide_by_2 (unumber&);
public:

//
// Ensure synthesized members are genrated.
//
bigint() = default;
bigint (const bigint&) = default;
bigint (bigint&&) = default;
bigint& operator= (const bigint&) = default;
bigint& operator= (bigint&&) = default;
~bigint() = default;

//
// Extra ctors to make bigints.
//
bigint (const long);
bigint (const string&);

这是我的构造函数和有问题的函数:

bigint::bigint (long that): long_value (that) {
using digit_t = unsigned char;
using bigvalue_t = vector<digit_t>;
if (that < 0) this->negative = true;
else this->negative = false;
unumber that_value = that;
string that_str = to_string (that_value);
bigvalue_t bignum = to_vector (that_str);
}

bigvalue_t bigint::to_vector (string& strval) {
digit_t digi;
bigvalue_t digivec;
for (auto it = strval.rbegin(); it != rend; ++i) {
digi = strval[it];
digivec.push_back(digi);
}
return digivec;
}

任何有关正在发生的事情的想法都将不胜感激!它似乎没有接受我的类型定义。我被允许在早期的方法中使用我的另一个自定义变量“unumber”而没有错误,所以这让我挠头。

最佳答案

如注释中所述,使用

bigint::bigvalue_t bigint::to_vector (string& strval)
{
// body
}

或者从 C++11 开始

auto bigint::to_vector (string& strval) -> bigvalue_t
{
// body
}

关于c++ - C++成员函数自定义类型返回 'does not name a type'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29852806/

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