gpt4 book ai didi

c++ - 从子构造函数(模板)访问父成员

转载 作者:行者123 更新时间:2023-11-28 03:20:02 26 4
gpt4 key购买 nike

<分区>

所以我有一些简单的问题/结构:

 class Class1 {
public:
Class1() {};
~Class1() {};
protected:
std::string name;
}

class Class2 : public Class1
{
public:
Class2() : number(id_generator++) {
name = "My-name"; // (1) want to access field inherited from Parent
};

private:
const unsigned int number;
static unsigned int id_generator;
}

编译器提示 (1):'name' was not declared in this scope。怎么了?它看起来很简单,但我没有看到。

EDIT1:我才意识到错误实际上只在这里出现(here 链接到代码):

#include <string>

template<int dim>
class Class1 {
public:
Class1() {};
~Class1() {};
protected:
std::string name;
};

template<int dim>
class Class2 : public Class1<dim>
{
public:
Class2() : number(id_generator++) {
name = "My-name"; // (1) want to access field inherited from Parent
};

private:
const unsigned int number;
static unsigned int id_generator;
};

int main() {}

很明显我把模板搞砸了。抱歉,没有把它写在第一位。

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