gpt4 book ai didi

c++ - 类构造函数内部的成员函数调用

转载 作者:行者123 更新时间:2023-11-30 01:58:14 25 4
gpt4 key购买 nike

我在类里面遇到了一些定义问题:

class Test{

protected:

int a;
int *b;
Teste() {}

public:

int getA() {return a;}
int getB() {if (b) return *b; else return 0;}
bool isB() {if(b) return true; else return false;}
Test(int a1, int b1): a(a1) {b = new int(b1);}
Test(const Test& test) {
if (test.isB())
this->b = new int(test.getB());
this->a = test.getA();
}

};

我收到以下错误消息:

“无效参数‘候选人是 bool isB()’”

“无效参数‘候选人是 bool getB()’”

问题是什么?

提前谢谢你,

最佳答案

您必须将您的 getter 函数声明为 const,以便能够通过您拥有的 const Test& 测试访问它们。

...
int getA() const { return a; }
int getB() const { if (b) return *b; else return 0; }
bool isB() const { if(b) return true; else return false; }
...

关于c++ - 类构造函数内部的成员函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17664079/

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