i'm creating the read().
my big objective on function:
1 - no arguments for wait the user press enter(cin.get());
2 - accept several parameters for read to several variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void read()
{
//nothing
cin.get();
}
template<typename first_t, typename... rest_t>
void read(const first_t &first, const rest_t... rest) //yes i added the addressed operator
{
std::cin>>first;
read(rest...); //function is recursive
//when the parameter pack becomes empty
//read(); will be called with no parameters
//which is why we declare it above
}
how i used:
1 2 3 4
string name;
read(name);
write(name);
read();
but i get several errors on "std::cin>>first;":
"error: no matching function for call to 'std::basic_istream<char>::read(const std::__cxx11::basic_string<char>&)'|"
so what i did wrong?
i can't understand that error message, but i have seen the big error:
the parameters are 'const', so we can't change their values... i haven't seen that error.
but now it's fixed:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void read()
{
//just wait enter key:
cin.get();
}
template<typename first_t, typename... rest_t>
void read(first_t &first, rest_t... rest)
{
std::cin >> first;
read(rest...); //function is recursive
//when the parameter pack becomes empty
//read(); will be called with no parameters
//which is why we declare it above
}
thank you so much for all
PS: the write() function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void write()
{
//nothing
}
template<typename first_t, typename... rest_t>
void write(const first_t &first, const rest_t... rest)
{
std::cout << first;
write(rest...); //function is recursive
//when the parameter pack becomes empty
//write(); will be called with no parameters
//which is why we declare it above
//these will never be used without parameters, but must be here
}
i can use the 'const' because the variables aren't changed.
i used the address operator, maybe i win memory space, but someone can correct me
void read()
{
//wait if the eneter key is pressed:
cin.get();
}
template<typename first_t, typename... rest_t>
void read(first_t &first, rest_t... rest)
{
std::cin >> first;
read(rest...); //function is recursive
//when the parameter pack becomes empty
//read(); will be called with no parameters
//which is why we declare it above
}
void read(string ¶meter)
{
std::getline (std::cin,parameter);
}
void read(char *parameter)
{
cin.getline (parameter, 256);
}
even with these changes, i found the problem:
- the cpp file is created;
- the cpp calls the header file: #include "Untitled1.h"
but the closed file to cpp.. that wasn't changed... after sometime i notice that... so i changed to real header file: #include "C:\Users\Cambalinho\Documents\CB\testfolder\Untitled1.h"
now works fine.
sometimes we have these simple problem.. but to notice isn't easy.
thank you so much for all