Reputation: 149
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
#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
#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. DiakokKiirasa function then should receieve it as a second argument, and use it to write out some other stuff.
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!
Upvotes: 0
Views: 1610
Reputation: 320777
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. 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.
There is another potential issue with your diakokBeolvasasa
function. It seems to return a result of either I33
or G22
type. 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++. And templates won't help you here. So, are I33
or G22
somehow related or not?
The second error is just a product of the first.
P.S. As it has been noted in the comments, you should not place template definitions into .cpp
files. Templates have to be defined in header files.
Upvotes: 2