简体   繁体   English

如何格式化函数以调用模板化类?

[英]How do I format my function to call a templated class?

I'm sure there is a very easy answer, but I can't figure it out. 我敢肯定有一个很简单的答案,但我不知道。 I have written a templated class, but I want to pass that class by reference in a class function that isn't templated. 我已经编写了模板化的类,但是我想在未模板化的类函数中通过引用传递该类。 Heres what I have. 这是我的东西。 I get a bunch of errors. 我遇到很多错误。 All I need to do is figure how to format the way to insert templated class into function, but I'm at a lost. 我需要做的只是弄清楚如何格式化将模板化类插入函数的方式,但是我很茫然。 Thank you and sorry if the code doesn't really help you out. 谢谢,如果代码不能真正帮助您,则对不起。

#include <iostream>
using namespace std;

template <typename T>
class Foo {
public:
    Foo();
    insert(const T& Item)
    //And other function, just examples
};

class noFoo(){
void test(Foo <T>& foo);
int i;
int j;
int k
};

template <typename T>
void noFoo::test(Food <T>& foo)}
cout << "hi";
}
int main() {
    Foo<char> wr;
    test(wr);
    return 0;
}

Make test a function template. 使test成为功能模板。 I also corrected loads of syntax errors for you ( class noFoo() ?), removed unnecessary code, and ran clang-format for indentation. 我还为您纠正了许多语法错误( class noFoo()吗?),删除了不必要的代码,并运行了clang-format的缩进。

#include <iostream>

template <typename T>
class Foo {};

class noFoo
{
public:
    template <typename T>
    void test(Foo<T> &);
};

template <typename T>
void noFoo::test(Foo<T> &)
{
    std::cout << "hi\n";
}

int main()
{
    Foo<char> wr;
    noFoo{}.test(wr);
}

Since your question is tagged , here the same code in D. 由于您的问题被标记为 ,因此这里的代码与D相同。

import std.stdio;

class Foo(T) {};

class noFoo
{
public:
    void test(T)(Foo!(T))
    {
        writeln("hi");
    }
};

void main()
{
    auto wr = new Foo!char;
    (new noFoo).test(wr);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM