passing a stringstream to a function - c++

I'm trying to pass a stringstream, from a file, to a function. When I'm calling the template function, I'm getting an error: no matching function for call to 'toFile'. I verified that the life is opened and the data has passed from it to the stringstream.
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
template <typename T1>
void toFile(string type, int NumOfElements, stringstream& ss){
T1* myArray = new T1[NumOfElements]; // declaring new array to store the elements
int value;
for(int i = 0; i < NumOfElements; i++){ // store the elements in the array
ss >> value;
myArray[i] = value;
cout << myArray[i] << " ";
}
}
int main(int argc, char *argv[])
{
ifstream ins;
ofstream outs;
string strg1;
string type;
int NumOfElements = 0;
stringstream inputString;
ins.open(argv[1]);
if(argc<1) {
cout << "please provide the file path." << endl;
exit(1);
}
while (getline(ins, strg1)){ // reading line from the file
inputString.clear(); // clearing the inputString before reading a new line
inputString << strg1;
inputString >> type ; // reading 1st element in a row
inputString >> NumOfElements; // reading 2nd element in a row
toFile(type, NumOfElements, inputString);
}
ins.close();
return 0;
}

toFile is a function template, so it can only be called with a template parameter. Sometimes function templates can deduce their parameters from their arguments, but since T1 isn't used in your argument list, there's no way to deduce it. You'll need to explicitly provide the template argument instead, for example:
toFile<int>(type, NumOfElements, inputString);

You didn't specify a template argument for toFile, so the template cannot be instantiated.
Thus, no function toFile<T> (which would have been an instantiation of said template) exists.
The argument cannot be deduced automatically because none of the function arguments have anything to do with it.

Related

std::string::insert doesn't work with to_string(). CPP

I am writing a code to insert an integer at an index of the string, but after providing the integer to add as string, insert function is not giving the correct output.
It is giving the error that :
no matching member function to call for insert string
This is my code:
#include <iostream>
using namespace std;
int main()
{
string s = "45564528";
int x = 8;
s.insert(s.begin()+5,to_string(x));
cout<<s<<endl;
return 0;
}
The expected output is 455648528.
Looking at the documentation for std::string::insert() shows that it takes a char or an iterator range, not a std::string, which std::to_string() naturally returns. At least, this is the case for the overloads that take an iterator for the first argument.
#include <iostream>
#include <string> // CHANGED: Include what you use
// using namespace std; // CHANGED: Bad practice
int main()
{
std::string s = "45564528";
int x = 8;
// CHANGED: Create string from the int, and use the iterator range overload
// to account for multi-digit numbers
auto tmp = std::to_string(x);
s.insert(s.begin()+5, tmp.begin(), tmp.end());
std::cout << s << '\n'; // CHANGED: std::endl is rarely actually needed
return 0;
}
There is an overload that lets you insert another std::string, but the first argument must be an index and not an iterator. So this would work as well:
#include <iostream>
#include <string>
int main()
{
std::string s = "45564528";
int x = 8;
s.insert(5, std::to_string(x));
std::cout << s << '\n';
return 0;
}

How to set a custom variable name through a function argument

So what i am trying to do is basically get the second argument of a function, and making the second argument the name of a variable so i can easily store the users input. Here is my code
`
#pragma once
#include <iostream>
using namespace std;
void askAndStore(string question, string variable)
{
cout << question + " ";
cin >> variable;
}
`
Pass variable by reference:
void askAndStore(string question, string& variable)
{
cout << question + " ";
cin >> variable;
}
string& instead of string. Without using & you'd be passing in a copy of your varaible, using a reference type string& you pass the actual variable in which is then modified by cin >> variable;.
P.S Don't use using namespace std;
You can't do this in C++.
You could have a std::map<std::string, SomeType> that you populate with your read-in names.
#pragma once
#include <iostream>
#include <map>
#include <string>
class Values
{
std::map<std::string, std::string> values;
public:
void askAndStore(std::string question, std::string name)
{
std::cout << question << " ";
std::cin >> values[name];
}
std::string get(std::string name)
{
return values[name];
// or return values.at(name); if name must already exist in values
}
};
int main()
{
Values user;
user.askAndStore("What is your name?", "usersName");
}
That assumes your values are std::strings

How to convert std::string to any of several types in constructor?

I have the following class template which has a member variable whose type is determined by the template argument. I want to initialize the value of this member in the constructor, which only takes a std::string. Thus, my problem is that I need to convert std::string to any of several types (int, double, bool, string). I don't think I can specialize just the constructor, and I would rather not specialize the entire class for each type. The problem with my code below is that stringstream stops streaming out when it hits a space:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
template <typename Ty>
struct Test
{
Ty value;
Test(string str) {
stringstream ss;
ss.str(str);
ss >> value;
}
};
int main()
{
Test<int> t1{"42"};
Test<double> t2{"3.14159"};
Test<string> t3{"Hello world"};
cout << t1.value << endl << t2.value << endl << t3.value << endl;
return 0;
}
The output of the above code is:
42
3.14159
Hello
instead of "Hello world". Is there some way to get stringstream to not stop at whitespace, or some other device that will do arbitrary conversions like I need?
This works for me. Just declare a special implementation before the generalized implementation:
#include <iostream>
#include <string>
#include <sstream>
template<typename T>
struct Test {
T value;
Test(std::string);
};
template<>
inline Test<std::string>::Test(std::string str) {
value = str;
}
template<typename T>
inline Test<T>::Test(std::string str) {
std::stringstream ss;
ss.str(str);
ss >> value;
}
int main() {
Test<int> t1{"42"};
Test<double> t2{"3.14159"};
Test<std::string> t3{"Hello world"};
std::cout
<< t1.value << std::endl
<< t2.value << std::endl
<< t3.value << std::endl;
return 0;
}
Here is an ideone working example.

Subscript requires array or pointer type

Hello the aim of this program is to initialize all elements of the vector Rooms to the string Empty, this is my first attempt at tackling vectors and passing by reference as such I'm not sure what is wrong.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void mainMenu(vector <int> &VectorRooms(), string &EmptyString);
int main()
{
vector < int > Rooms(13);
string str1 = "Empty";
mainMenu(Rooms, str1);
}
void mainMenu(vector <int> &VectorRooms(), string &EmptyStrings)
{
int i;
for (i = 0; i < VectorRooms.size(); i++)
{
VectorRooms[i] = EmptyStrings;
cout << VectorRooms[i] << endl;
}
}
You have:
void mainMenu(vector <int> &VectorRooms(), string &EmptyStrings)
You mean:
void mainMenu(vector <int> &VectorRooms, string &EmptyStrings)
Remove those parentheses. With the parentheses there, you're actually declaring VectorRooms as a function pointer to a function that returns a vector <int> & and takes no parameters, and index [] is invalid on a function pointer type. That, of course, isn't your intention

Why my template function is not a valid match in this call?

i made the following code to convert number to string and reverse
in the commented part of code i want to make the function type a template
thinking that it will acquire the type according to context "e.g if i assign
it to int variable the function will be of type int " but this not occur and compiler
give error message
D:\computer science\project\stringToint.cpp In function 'int main()':
49 25 D:\computer science\project\stringToint.cpp [Error] no matching function for call to 'intstr(const char [10])'
49 25 D:\computer science\project\stringToint.cpp [Error] candidate is:
17 21 D:\computer science\project\stringToint.cpp template<class T> T intstr(std::string)
i think that their was error in using stringstream object
but i was successful in achieving the function to work if
i specify the type of function but this will make me write
different function for every type
is i miss understand some thing please help
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
template<typename T>
string strint (T oty)
{
string ity;
stringstream ss;
ss<<oty;
ss>>ity;
return ity;
}
/*
template<typename T>
T intstr (string oty)
{
T ity;
stringstream ss;
ss<<oty;
ss>>ity;
return ity;
}
*/
int intstr (string oty)
{
int ity;
stringstream ss;
ss<<oty;
ss>>ity;
return ity;
}
signed char charstr (string oty)
{
signed char ity;
stringstream ss;
ss<<oty;
ss>>ity;
return ity;
}
int main()
{
int i;
signed char c;
string s;
s=strint(123);
cout<<s<<endl;
i=intstr("123456789");
cout<<i<<endl;
c=charstr("2");
cout<<c;
return 0;
}
You should explicitly specify template parameter for function, since compiler can't deduce T, because there are no parameters of type T in function args. Like
intstr<int>("123456789");
thanks #ForEveR i now reduced my code to look like this and it is working well
i hope it is the best solution of converting number to string and vise verse
using stringstream
thanks alot
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
template<typename T1,typename T2>
T2 strint (T1 oty)
{
T2 ity;
stringstream ss;
ss<<oty;
ss>>ity;
return ity;
}
int main()
{
cout<< strint <string,int>("1234") <<endl;
cout<< strint <int,string>(456) <<endl;
cout<< strint <string,float>("3.14") <<endl;
cout<< strint <string,char>("3") <<endl;
return 0;
}