Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6783861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:55:17+00:00 2026-05-26T16:55:17+00:00

I can’t seem to open a file without storing it in a variable. I

  • 0

I can’t seem to open a file without storing it in a variable. I can do this:

ifstream blob("somefile");
string line;
blob >> line;

But when I try this:

string line;
ifstream("somefile") >> line;

The compiler (clang) gives this error:

t.cpp:7:23: error: invalid operands to binary expression ('ifstream' (aka 'basic_ifstream<char>') and 'string' (aka 'basic_string<char>'))
    ifstream("thing") >> i;
    ~~~~~~~~~~~~~~~~~ ^  ~
In file included from t.cpp:1:
In file included from /usr/include/c++/4.6/iostream:39:
In file included from /usr/include/c++/4.6/ostream:39:
In file included from /usr/include/c++/4.6/ios:42:
In file included from /usr/include/c++/4.6/bits/ios_base.h:42:
In file included from /usr/include/c++/4.6/bits/locale_classes.h:41:
In file included from /usr/include/c++/4.6/string:53:
/usr/include/c++/4.6/bits/basic_string.h:2679:5: note: candidate function [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>] not
      viable: no known conversion from 'ifstream' (aka 'basic_ifstream<char>') to 'basic_istream<char> &' for 1st argument
    operator>>(basic_istream<char>& __is, basic_string<char>& __str);
    ^
In file included from t.cpp:1:
In file included from /usr/include/c++/4.6/iostream:40:
/    usr/include/c++/4.6/istream:121:7: note: candidate function not viable: no known conversion from 'string' (aka 'basic_string<char>') to
      '__istream_type &(*)(__istream_type &)' for 1st argument
      operator>>(__istream_type& (*__pf)(__istream_type&))
      ^
/usr/include/c++/4.6/istream:125:7: note: candidate function not viable: no known conversion 

... a few more hundred pages of crap ...

1 error generated.

So, what’s the difference between the two? With other classes, calling it directly works just fine. Is there some template magic involved that makes it ambiguous?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:55:18+00:00Added an answer on May 26, 2026 at 4:55 pm

    The code that you post is not valid in C++03 (i.e., what was the C++ language standard until September 2011). In C++03, there is only one overload of operator>> that might be considered here [I’ve removed all of the template code because it is not relevant]:

    istream& operator>>(istream&, string&);
    

    Note that the std::istream parameter is a non-const reference, so a temporary std::istream object cannot be used. In your code, you are trying to use a temporary object there.

    In C++11 (i.e., the current C++ language standard), there is an additional overload which takes the std::istream object by rvalue reference. This allows either a temporary to be passed via that parameter.

    Visual C++ already supports this feature of C++11, which is why, as Benjamin Lindley points out in a comment, your code will indeed compile if you use Visual C++ 2010 or later. Your build of Clang may support this if you use the -std=c++0x flag.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
Can I set a label (text) in popup menu (gtk.Menu) but without hovering effect,
Can you cast a List<int> to List<string> somehow? I know I could loop through
Can anyone recommend a good library for generating an audio file, such as mp3,
I want to count how many characters a certain string has in PHP, but
Can I use the following across all browsers? <a href=# onclick=doSomething()>Click here.</a> Is this
Can anyone tell me what's wrong with this robots.txt? http://bizup.cloudapp.net/robots.txt The following is the
Can anyone recommend a fast way to sort the contents of a text file,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Can somebody help me construct this regular expression, please... Given the following strings... "April

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.