简体   繁体   English

编译错误:无法推断出模板参数

[英]Compile error: could not deduce template argument

I am trying to finish a school project of mine, but I have run into a problem. 我正在努力完成我的学校项目,但我遇到了一个问题。 I am trying to use templates in my work, but it seems I don't really understand how to do that. 我正在尝试在我的工作中使用模板,但似乎我真的不明白该怎么做。 Here a part of my code: 这是我的代码的一部分:

main.cpp main.cpp中

#include "stdafx.h"
#include "Osztaly.h"
#include "Fuggvenyek.h"
#include <string>
#include <iostream>

int main(){ 
    char kepzes = setKepzes();  
    //diakokBeolvasasa(kepzes);
    diakokKiirasa(kepzes, diakokBeolvasasa(kepzes));
    return 0;
}

Fuggvenyek.h Fuggvenyek.h

#ifndef FUGGVENYEK_H
#define FUGGVENYEK_H

char setKepzes();

template <class szak>
szak diakokBeolvasasa(char);

template <class szak>
void diakokKiirasa(char, szak);

#endif

So I am trying to pass back different type of values depending on an if statement. 所以我试图根据if语句传回不同类型的值。 DiakokKiirasa function then should receieve it as a second argument, and use it to write out some other stuff. 然后DiakokKiirasa函数应该接收它作为第二个参数,并用它来写出其他一些东西。

Fuggvenyek.cpp Fuggvenyek.cpp

#include "Fuggvenyek.h"
#include "Osztaly.h"
using namespace std;

char setKepzes(){
    char kepzes;
    cout << "A beolvasando szemely kivalasztott kepzese:\n    i - informatikus"
        << "g - gepesz\n>> ";
    cin >> kepzes;
    return kepzes;
}

template <class szak>
szak diakokBeolvasasa(char kepzes){
    I33 informatikusok;
    G22 gepeszek;
    //ha a kepzese informatikus
    if (kepzes == 'i'){
        informatikusok.setDiakokSzama();        
        informatikusok.setDiakAdatok();
        return informatikusok;
    }
    //ha a kepzese gepesz
    else if (kepzes == 'g'){
        gepeszek.setDiakokSzama();
        gepeszek.setDiakAdatok();       
        return gepeszek;
    }
}

template <class szak>
void diakokKiirasa(char kepzes, szak diakok){
        diakok.getDiakAdatok();
}

My compile errors: 我的编译错误:

Error   1   error C2783: 'szak diakokBeolvasasa(char)' : could not deduce template argument for 'szak'  d:\programming\c++\cppproject\cppproject\main.cpp   10  1   CppProject
Error   2   error C2780: 'void diakokKiirasa(char,szak)' : expects 2 arguments - 1 provided d:\programming\c++\cppproject\cppproject\main.cpp   10  1   CppProject

Thanks in advance! 提前致谢!

In order for the compiler to deduce template arguments, these template arguments have to depend on function parameter types. 为了使编译器推导出模板参数,这些模板参数必须依赖于函数参数类型。 In your case 在你的情况下

template <class szak>
szak diakokBeolvasasa(char);

template argument does not depend on function parameter type. template参数不依赖于函数参数类型。 For this reason, it is not possible for the compiler to deduce template arguments for this function. 因此,编译器无法推导出此函数的模板参数。 (The compiler cannot deduce template argument from function return type.) (编译器无法从函数返回类型推导出模板参数。)

When calling this function you will have to specify template arguments explicitly, as in 调用此函数时,您必须明确指定模板参数,如

diakokBeolvasasa<double>(kepzes);

Use whatever type you wanted your szak to be in place of double in the above example. 在上面的例子中,使用你想要的szak代替double任何类型。

There is another potential issue with your diakokBeolvasasa function. 您的diakokBeolvasasa功能还有另一个潜在问题。 It seems to return a result of either I33 or G22 type. 它似乎返回I33G22类型的结果。 If these types are unrelated, then... well... it can't be done that way. 如果这些类型不相关,那么......好吧......不能这样做。 There's no such thing as function that can return an "unpredictable" type in C++. 没有函数可以在C ++中返回“不可预测”类型。 And templates won't help you here. 模板不会帮助你。 So, are I33 or G22 somehow related or not? 那么, I33G22以某种方式相关?

The second error is just a product of the first. 第二个错误只是第一个错误的产物。

PS As it has been noted in the comments, you should not place template definitions into .cpp files. PS正如评论中指出的那样,您不应将模板定义放入.cpp文件中。 Templates have to be defined in header files. 必须在头文件中定义模板。

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

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