0

I have a piece of code which loads text file contents into a char *. (I know it's an odd mix of C++ and C but please bear with me.)

std::ifstream filestream1(filename);

std::string str1(std::istreambuf_iterator<char>(filestream1), 
                 (std::istreambuf_iterator<char>()) ); 

GLchar * code1 = (GLchar *) malloc(sizeof(GLchar) * str1.length());
strcpy(code1, str1.c_str());  

Previously I was getting an "expression musy have class type" error on str1 variable after this version of the line:

std::string str1(std::istreambuf_iterator<char>(filestream1), 
                 std::istreambuf_iterator<char>() ); 
//str1 causes "expression must have class type" errors 

Clearly, adding extra brackets (which seemed redundant to me, to be honest), fixed the problem. Question is, what exactly changed between one and the other? This isn't one of those "declaring a function instead of instantiating a class" mistakes, is it? I mean can one even declare a function with no name and inside a function call, and no [] like a lambda?

DailyFrankPeter
  • 382
  • 1
  • 13
  • [Can't reproduce.](http://ideone.com/MWkUXy) Perhaps more context is required? Compiler and version? – user4581301 Nov 12 '16 at 17:42
  • 1
    Off-top: `str1.length()` doesn't include `'\0'`. Try `str1.length() + 1`. And, maybe `std::copy`/`std::copy_n`? Is `GLchar` always equal to `char`? – ilotXXI Nov 12 '16 at 17:45
  • Fixed the lenght of buffer, duh, thanks! GLchar is defined as `typedef char GLchar;`, so I think yes, unless it changes in next lib versions. – DailyFrankPeter Nov 15 '16 at 17:34

0 Answers0