gpt4 book ai didi

c++ - 无效的基类 T 错误

转载 作者:行者123 更新时间:2023-11-28 01:05:59 24 4
gpt4 key购买 nike

我正在尝试做这样的事情,但是,我收到一个错误,“无效的基类 T”。就我们的理解而言,基类将在编译时使用类型名称 T 进行实例化,我将在编译时获得 T 的定义。我尝试用“Class”替换“Typename”,但我仍然遇到同样的错误。有任何想法吗 ?

template <typename T>  
class Base
{
private:
class A : public T{};

};


// It works when I do the following in main ...

class A{}
Base* B = new Base<A> B;

// It throws error, when I pass in int,double,float,
//makes sense,because these are basic data types ...

Base* B = new Base<int> B;


// Neil's Snippet with the error reproduced ...

#include <iostream>
using namespace std;

template <typename T>
class Base
{
public :
Base::Base(int a)
{
A a;
}
private:
class A : public T{};
};

Base <int> b;

int main(){
cout << &b << endl;
}

最佳答案

Typename 不是有效的 C++ 关键字,typename 是。类似的Class 不同于class

请注意,C++ 区分大小写。除此之外,您的代码中缺少一个分号。

class A : public T{}; // missing semicolon here

关于c++ - 无效的基类 T 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6093764/

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