-
now what?
why do I get an error for that?
Code:
istream* input;
int main(int argc, char* argv[])
{
switch (argc)
{
case 1: // read from standard input
input = &cin;
break;
case 2:
input = new istringstream(argv[1]); // read argument string
break;
default :
error ("Too many arguments");
return 1;
}
...
I get the following error :
Error : illegal use of incomplete struct/union/class 'std::basic_istringstream<char, std::char_traits<char>, std::allocator<char>>'
calc.cpp line 94 input = new istringstream(argv[1]); // read argument string
-
My compiler doesn´t accept your code, so I´m trying to explain with a different example. Istringstream is a template:
'std::basic_istringstream<char, std::char_traits<char>, std::allocator<char>>'
and templates have different new usage. Like this:
Code:
list<int> *vet;
int main(int argc, char* argv[])
{
//vet = new list; this is wrong
vet = new list<int>();
}
Hope that helps... what compiler do you use anyway?
-
Check out his signature :P
-
It works on my compiler(gcc 3.2)...
-
Also, though not obvious, there is kind of a logic flaw with that code. In the first case, input is assiged to cin's memory address. In the second case, you assign it to a newly allocated istringstream. As you know, you will have a memory leak if you don't delete that istringstream object. However, after the switch statement, how will you know if you assigned input to cin or to a new istringstream? If you try to delete input when it is assigned to cin, you will get a big fat error(or at least I do on my OS). Also, wouldn't you be able to accomplish the same thing like this:
Code:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv) {
string argvar;
switch(argc) {
case 1: cin >>argvar; break;
case 2: argvar = argv[1]; break;
default: cerr <<"Too many arguments!\n"; return 0;
}
//Now you have your argument in argvar
}
Hope that helps.