gpt4 book ai didi

c++ - 无法使用将模板用作方法中参数的结构

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

我有以下代码:

template <typename A, typename B>
struct TheStruct {
A a;
B b;
};

class TheClass {
public:
void do_something(TheStruct<A,B> blah);
}

我在 do_somethng 方法上遇到编译器错误,类似于 error: 'A' was not declared in this scope...

在方法参数中定义这种类型的正确语法是什么?

谢谢!

最佳答案

您需要将 TheClass 设为模板:

template <typename A, typename B>
class TheClass {
public:
void do_something(TheStruct<A,B> blah);
};

或者您需要将 do_something() 设为模板:

class TheClass {
public:
template <typename A, typename B>
void do_something(TheStruct<A,B> blah);
};

关于c++ - 无法使用将模板用作方法中参数的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4961031/

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