Thread: now what?

  1. #1
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117

    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
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  2. #2
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    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?
    Nothing more to tell about me...
    Happy day =)

  3. #3
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    Check out his signature :P
    Away.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    It works on my compiler(gcc 3.2)...

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    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.

Popular pages Recent additions subscribe to a feed