submit value to ostringstream does not work

//!> Convert Int, Real etc. to string
template<typename ty>
string VTKWriter::convert(const ty val) {


ostringstream buffer;
string str;

buffer << val;
cout << buffer << endl;
str = buffer.str();
return str;
}

The compiler stops at "buffer << val" but why? The funny stuff it has already worked a couple of days ago!!
Try to clean and re-build the project...
Apparently there's no error in this program...
May try initializing "buffer" when creating it... like this...

ostringstrem buffer("");
#include <sstream>
since val is a templated parameter, is the type of the variable you are passing streamable? (ie, does it have operator<< defined?)

Also, it is pointless to pass val by const value. Either pass by value or by const reference.
Also, it is pointless to pass val by const value. Either pass by value or by const reference.


While it may be the case for this example, I wouldn't agree that it is always pointless. Consider the following quick example.

1
2
3
4
5
6
7
8
9
10
11
12
void func(const int value)
{
    // the compiler wouldn't catch this error if value wasn't a const int
    if(value = 5) // should be value == 5
    {
       // do some things
    }
    else
    {
      // do other things
    }
}


The point is that even when passing by value the const keyword is useful for situations where the parameter isn't going to be modified. There are plenty of situations where we create constants on the stack within the function so it is equally useful to simply declare the function parameter to be a const isn't it? I know that this is a rather old thread but I actually ran across a problem like this not long ago where the compile caught a silly mistake for me but only because I specified the input params to be const. I never used to specify const when passing by value but recently changed my mind about that. I figure that if a parameter is truly a read only parameter why not specify it to be a const? What's wrong with it?
1
2
3
4
5
6
#include <iostream>

int main( int argc, char* argv[] ) {
   if( argc = 2 )  // OOPS
       std::cout << "Here\n";
}


g++ -Wall foo.cpp
foo.cpp: In function `int main(int, char**)':
foo.cpp:4: warning: suggest parentheses around assignment used as truth value

Does your compiler not catch this at all, even with warnings turned on?
To be fair, g++ only complains with -Wall.

Though it certainly doesn't hurt; I just don't think it helps, to warrant the extra
typing.
No, my compiler never catches those types of things. If the extra typing bothers you, then you don't have to do it. I'm just saying that it isn't necessarily pointless to do it. I never used to do it until I saw it done by others and then realized that there is some value in doing it sometimes.
Of course, you can always use left-hand comparison. And, of course, you know what they say about people who use left-hand comparison and sailors.
Topic archived. No new replies allowed.