{"id":3928,"date":"2014-03-30T06:25:14","date_gmt":"2014-03-30T06:25:14","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/declaring-generic-istream-in-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:25:14","modified_gmt":"2014-03-30T06:25:14","slug":"declaring-generic-istream-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/declaring-generic-istream-in-c-collection-of-common-programming-errors\/","title":{"rendered":"declaring generic istream in c++-Collection of common programming errors"},"content":{"rendered":"<p>Try with an <code>istream*<\/code> instead. Note, however, that you have to change your code slightly. Using pointers you have to preserve the memory area of the object that you&#8217;re pointing. In other words, the &#8220;inFile&#8221; variable cannot be declared there, as it won&#8217;t exist out of the else. The code could be, then:<\/p>\n<pre><code> istream* in;\n ifStream inFile;\n\n if(!strcmp(argv[1],\"cin\"))\n {\n      in = &amp;cin;\n }\n else\n {\n      inFile.open(argv[1]);\n      in = &amp;inFile;\n }\n \/\/ use *in\n<\/code><\/pre>\n<p>(Note also the modifications in the string handling. I modified them as an example.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Try with an istream* instead. Note, however, that you have to change your code slightly. Using pointers you have to preserve the memory area of the object that you&#8217;re pointing. In other words, the &#8220;inFile&#8221; variable cannot be declared there, as it won&#8217;t exist out of the else. The code could be, then: istream* in; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3928","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3928"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3928\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}