I have two files quantity.h
and main.cpp
whose contents are below
file quantity.h
#include <iostream>
#include <utility>
template <typename T = unsigned int>
class quantity
{
enum volume {ltr,gallon,oz};
public:
using ret = std::pair<volume,T>;
ret get_volume()
{
return std::make_pair(ltr,5);
}
};
Then I have main.cpp
#include<quantity.h>
int main() {
quantity <unsigned int> q1;
ret t = q1.get_volume();
return 0;
}
It compiles if I only call q1.get_volume();
but I want to store the result in a std::pair ret
which is declared/defined in quantity.h
file . I get compilation error for the code as
error: 'ret' was not declared in this scope
Use auto, and let the compiler do the deduction work for you :
#include <iostream>
#include <utility>
template <typename T = unsigned int>
class quantity
{
enum volume { ltr, gallon, oz };
public:
auto get_volume()
{
return std::make_pair(ltr, 5);
}
};
int main()
{
quantity <unsigned int> q1;
auto t = q1.get_volume();
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.