vijayan121 1,152 Posting Virtuoso
void SnakeSeg::kill()
{
    if (alive)
    {
        alive = false;
        erase();
        links = 0;
        if (next)
        {
            delete next; 
            next = 0 ; // isn't this required?
        }
    }
}
vijayan121 1,152 Posting Virtuoso

> My thought was to make a .h file with a namespace. Would that work?
yes, provided that all the functions in the header have internal linkage. ie. they are inline or put in an anonymous namespace (or have the deprecated static linkage specifier). and you have an include guard for your header file.

> Or would I have to make a .dll?
if you have a header and seperate implementation files (.c .cc .cpp), you would need to make a library of some kind. either a static library or a shared library (dll).

> If so, how would I go about doing that?
depends on the toolset (microsoft/gnu/borland etc) that you are using.

vijayan121 1,152 Posting Virtuoso

> printing out the values to make sure they are correct they now look like this ...
the initial default precision for a stream is 6. to print out more decimal digits, do something like

std::cout << std::fixed << std::setprecision(8) 
          // 8 digits *after* decimal point
          << value ;

> I need absolute precision.
you will not get absolute precision with floating point values.
if the precision of a long double is not sufficient, you would either use integral types (numerator,denominator) or use a high precision arithmetic library.

vijayan121 1,152 Posting Virtuoso

all literals (including literal strings) are immutable. they are const.
and therefore cannot be sorted (or modified in any way)

//char* a = "acbacb"; // bad programming practice
const char* a = "acbacb"; // the correct way
char b[] = "acbacb"; // also ok. a literal can be used to initialize
                    // an array of modifiable chars
vijayan121 1,152 Posting Virtuoso
int eig( int num )
{
  if( num<10 ) return num;
  else return eig( eig( num/10 ) + (num%10) ) ;
}

cool!
though you spoiled it by void main() and #include<iostream.h>

vijayan121 1,152 Posting Virtuoso

String s2 = <some_expression> ; initializes one String with another String (copy construct). if <some_expression> is not a String, it must be implicitly convertible to one. it is semantically like int i = <some_expression> ; initializes one int with another int. if <some_expression> is not an int it is implicitly converted to one.
so, to be able to write String s2 = "Whatever"; requires a non-explicit constructor and an accessible copy constructor ( 'even if it may not be called' ).
to write String s2(" Whatever" ) ; or String s2 = String( "Whatever" ) ; does not require the constructor to be non-explicit.

vijayan121 1,152 Posting Virtuoso
#include <cliext/vector>
using namespace cliext ;

// ...
// create an empty vector
cliext::vector< String^ >^ Box1 = gcnew cliext::vector< String^ >;

// this will not work; there is no space in the vector
// Box1[0] = comboBox1->SelectedItem->ToString();

// this would; make space for the element
Box1->push_back( comboBox1->SelectedItem->ToString() ) ;

// fine; Box1 now has one element and Box1[0] retrives it
String^ path = "C:\\Glob\\Groups\\" + Box1[0] + ".txt";
// however Box1[1] is an error here; there is only one element right now.

it would be a good idea to give your variables more meaningful identifiers than ComboBox1, Box23 etc. like file_names_combobox or select_file_combo instead of ComboBox1.

vijayan121 1,152 Posting Virtuoso

it would be easier (and also more efficient) if you use a cliext::vector<> instead of a std::vector<> note: requires VC++ 9 (2008)

#include <cliext/vector>
using namespace cliext ;

// ...
{
  // ...
  vector< String^ >^ vec = gcnew vector< String^ > ;
  vec->push_back( comboBox1->SelectedItem->ToString() ) ;
  String^ Box1 = vec[0] ;
  // ...
}
vijayan121 1,152 Posting Virtuoso

> How is that the "My Name.." bit, which I believe to be a const char* or string literal
> be somehow resolved to a String object for the purposes of the function.
the String class has a (non-explicit) single argument constructor which takes a const char * . this also acts as an implicit conversion operator.
an anonymous String is constructed and passed to String operator+(const String &s1, const String &s2); > Originally, my prototypes were ...
these are just fine and for something like s2 = "My name is " + s3; is more efficient.
the creation of an anonymous temporary String object is avoided.

vijayan121 1,152 Posting Virtuoso

> Only in MS-Windows and MS-DOS do we run into trouble with text mode seeks
> due to the two-byte line terminators.
> I have no idea how it works on non-English file systems, presumably the same.
in reality, all we would want is to use tellg to mark a position in an fstream and use seekg to get to that precise location sometime later. this will always work correctly on all streams. problems arise with seek on fstreams (in windows (text mode) or locales using MBC character sets) if we seek to a position that was not earlier returned by a tell. for example, this code (VC++,windows; locale names are not portable) would always work correctly; we are not seeking to arbitrary positions.

#include <iostream>
#include <fstream>
#include <string>
#include <locale>
using namespace std ;

int main()
{
 { ofstream temp("a.txt") ; } // create a new file

 const locale german ( "german" );
 fstream file( "a.txt", ios::in|ios::out ) ;
 file.imbue( german ) ;
 file << "some \n random \n lines\n\n" ;
 
 fstream::pos_type namepos = file.tellg() ;
 file << "name string" << '\n' << '\n' ;
 
 fstream::pos_type phonepos = file.tellg() ;
 file << 12345678 << '\n'<< '\n' << '\n' ;
 
 fstream::pos_type addresspos = file.tellg() ;
 file << "address string\n" ;

 file << "a few more \n\n random \n lines\n\n\n" ;

 string str ;
 cout.imbue( german ) ;

 file.seekg( namepos ) ;
 getline( file, str ) ; 
 cout << str << '\n' ;

 file.seekg( addresspos ) …
vijayan121 1,152 Posting Virtuoso

> file.clear() and file.seekg(...) shouldn't work correctly either?
>If that's the case, is there any way I can return to the beginning of the file without opening it in binary?
file.clear() will work in all cases.
file.seekg(...) will work in all cases if you seek to the beginning of the file.
otherwise, if the streampos that you use for the seek wasn't one returned by an earlier tellg on the same stream/filebuff, all bets are off. c++ throws in locale dependent character code translation, even when the char_type of the stream is just a char. you migh end up on the second byte of a multibyte character sequence or on the lf of a cr-lf sequence.
if you open a file stream (where the char_type of the stream is just a char) in binary mode and imbue the "C" locale, which will always result in a constant character width equal to one, you can seek to an arbitrary position.

vijayan121 1,152 Posting Virtuoso

> I also found an example at cplusplus.com that uses the file pointers on a .txt file (in addition to a binary file), here's their example ...
AARGH!
1. stream::tellg() returns an streampos, which isn't guaranteed to be convertible to an integral type
2. even if streampos is convertible to an integral type, there's no guarantee that the numeric value of this type has any real significance (eg. high order bits contain the sector number, low order bits the offset in the sector).
3. even if it represents some numerical offset within the system, it isn't guaranteed to fit in a long.
4. subtracting one streampos from another gives a streamoff. streamoff is some integral type, but will not necessarily fit in a long.

even if streampos and streamoff are long values, there would be problems when multi-byte encodings are involved.
even if the locale imbued by the stream is the classic "C" locale with single byte encoding, there would be problems because of escape sequence translations during input/output.

all that seekg on an fstream does is call filebuf::seekpos or filebuf::seekoff. and the standard says:
"If the position has not been obtained by a previous successful call to one of the positioning functions (seekoff or seekpos) on the same file the effects are undefined."

try the following on a windows machine (with single-byte ascii encoding for the default locale):

#include <fstream>
#include <iostream>
#include <string>
int main() …
vijayan121 1,152 Posting Virtuoso

the file is not in a good state after your loop executes file.eof() == true clear the eofbit first.

file.clear() ;
  file.seekg( 0, std::ios::beg ) ;

the seekg you are doing is ok because you are seeking to the beginning of the file. for a seekg to some non-zero offset (or a seekg from the end) to work reliably (portably), the file has to be opened in binary mode.

vijayan121 1,152 Posting Virtuoso

yes. something like

int max_so_far = array[0] ;
for( int i=1 ; i<num_elements ; ++i )
{
  // if the element in question is greater than max_so_far, make 
  // max_so_far equal to that element
}
// max_so_far now contains the value of the largest element in the array
henpecked1 commented: V was very patient and very helpful +1
vijayan121 1,152 Posting Virtuoso

yes. the problem is the same. fill the array that is passed to the function, not something else.
your main would look like:

// ...
const int N = 24 ;
int what_ever_you_want_to_call_it[N] ; // array of temperatures
gettemps( what_ever_you_want_to_call_it, N ) ;
double avg = averagetemp( what_ever_you_want_to_call_it, N ) ;
// ...
vijayan121 1,152 Posting Virtuoso

in your code,

double averagetemp( int temp[], int numtemp)
{
sum = 0.0;
for (i = 0; i < Count; i++)
   sum = sum + hourlytemps[i];

if (count > 0)
   return sum / count;
else
   return 0.00;
}

you are using some other array than the one passed to the function. the array (for which average is to be determined is temp not hourlytemps. and the number of elements are numtemp, not count

> So does this mean my prototypes are wrong in the header?
no. the prototypes are correct. the array called temp in the prototype is the same array that you want to call hourlytemps. and perhaps you are under the impression that these are two different arrays. giving rise to the confusion.

vijayan121 1,152 Posting Virtuoso
double averagetemp( const int hourlytemps[], int numtemp)
{
  // the first parameter is the array containing the temperatures
  // the second parameter is number of elements of this array
  // to do: return the average of all temperatures in the array.

  double sum = 0.0;
  for (i = 0; i < numtemp; i++)
    sum = sum + hourlytemps[i];

  if (numtemp > 0)
    return sum / numtemp;
  else
    return 0.00;
}

again, use the information that is passed to the function

vijayan121 1,152 Posting Virtuoso

> he does say to pass the array to a function ...
i'm almost certain that he means 'pass the hourlytemps array to the function'. ignoring the array that is passed does not make sense. perhaps write it as the second example in post #15.

> I also have to average all 24 temps in a separate function, thats what I'm trying to figure out now
write another function to which you pass the array:

double average( const int hourlytemps[], int numtemp )
{
   double total = 0.0 ;
   // loop thru the array; in the loop add the array element to total
   return total / numtemp ;
}
vijayan121 1,152 Posting Virtuoso

well, you could simply write

void gettemps(  int numtemp )
{
  int count = 0;
  int temp ;
  while (count != numtemp)
  {
    cout << " Enter " << count << " :00 " << " temperature "<< endl;
    cin >> temp ;

    if (temp >= -51 && temp <= 131)
    {
      hourlytemps[count] = temp ;
      count ++; 
    }
  }
}

or better still, pass the array to be filled up (hourlytemps) as an argument

void gettemps(  int hourlytemps[ /* numtemp */ ], int numtemp )
{
  int count = 0;
  int temp ;
  while (count != numtemp)
  {
    cout << " Enter " << count << " :00 " << " temperature "<< endl;
    cin >> temp ;

    if (temp >= -51 && temp <= 131)
    {
      hourlytemps[count] = temp ;
      count ++; 
    }
  }
}
vijayan121 1,152 Posting Virtuoso
void gettemps(int temp[], int numtemp)
{
              int count = 0;

	while (count != numtemp)
	{
		cout << " Enter " << count << " :00 " << " temperature "<< endl;
		cin >> temp[count] ;

		if (temp[count] >= -51 && temp[count] <= 131)
			{
				hourlytemps[count] = temp[count];
				count ++; 
			}
	}
}

in void gettemps(int temp[], int numtemp) temp is a pointer. (presumably points to the first element of an array of size numtemp)
since the array hourlytemps is what you seem to be attempting to fill up, is there a need for a separate temp array?

vijayan121 1,152 Posting Virtuoso
std::string runasc "runas /profile /user:tsc\\staffuser" ;
system( ( runasc + " " + programc ).c_str() ) ;
vijayan121 1,152 Posting Virtuoso

> why is that the only variable that gets the double in the expression itself?
(f-n) and 100 are automatically converted to double by the compiler. you could also write ( double(f - n) / n ) * 100; or ( double(f - n) / double(n) ) * 100.0 ;

vijayan121 1,152 Posting Virtuoso
#include <iostream>

using namespace std;

int main(int, char**) 
{   
    long long n = 6647817899LLU;
    long long f = 10299685494LLU;
    int i = ((f - n) / double(n) ) * 100;
    cout << "Part 4: " << f << " is " << i 
         << "% more than " << n << endl;
    
system("pause");
return 0; }
vijayan121 1,152 Posting Virtuoso
// ...
public:
  Form2(void)
  {
      InitializeComponent();
  }
  // ..
protected: 
  virtual void OnActivated( EventArgs^ e ) override
  {
    // clear all strings in combobox
    // reload fresh strings into combobox   
  }
// ...

looks like you need a refresher on basic c++ (and c++/cli) syntax.

vijayan121 1,152 Posting Virtuoso

I have 2 Forms in my Windows Form application. Form1 creates .txtfiles.
I open Form2 from Form1 and can see the .textfiles that is created in a comboBox3.

If I open the application from the beginning and go to Form2 I will see all .txt files that exists in a folder in this comoBox3. While the application is open I close Form2 and is then on Form1 where I now will create a new .text file.
I open Form2 and see if this is in the comboBox3 and it isn´t ?
If I close the whole application, open it again and go to Form2 then it will appear in the comboBox3.
Why do I have to close the application and open it again to receive the changes.

I load in the .txt files to the comboBox3 with this code under Form2(void):

Form2(void)
{
	InitializeComponent();
	//
	//TODO: Add the constructor code here
	//

System::String ^ Files10 = "C:\\Globejennife\\Groupjennif\\"; 
array<System::String ^> ^ files4 = System::IO::Directory::GetFiles(Files10,"*.txt");

array<Char>^ chars = {'.'};
array<System::String ^> ^ splits;
			
    for (int count1 = 0; count1 < files4->Length; count1++)
   {
    splits = files4[count1]->Split(chars);	
    comboBox3->Items->Add(splits[0]->ToString()->Substring(29));

   }
}

override the virtual function OnActivated in Form2. protected: virtual void OnActivated( EventArgs^ e ) ; move the code in the constructor for populating comboBox3 to this function. (and also call the base class OnActivated.) http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.onactivated.aspx

vijayan121 1,152 Posting Virtuoso

> The data is basically alot of text and integer values.
> It needs to be accessed, sorted and filtered
> Not much data will be added maximum size will go to 400K

just use a simple xml file to store the data.
and an xml parser library eg. http://www.grinninglizard.com/tinyxml/

vijayan121 1,152 Posting Virtuoso

the exception thrown is of type float, not int
you have to catch it as a float. catch( float nValue ) { /* ... */ }

vijayan121 1,152 Posting Virtuoso
array<System::String ^> ^ files4 = System::IO::Directory::GetFiles(Files,"*.txt");
if( files4->Length > 0 )
{
  System::String ^ text =  files4[0] ;
  for( int i = 1 ; i < files4.Length ; ++i )
      text += System::Environment::NewLine + files4[i] ;
  this->textBox1->Multiline = true ; // if not multiline
  this->textBox1->WordWrap = false ; // if required
  this->textBox1->Text = text ;
}
vijayan121 1,152 Posting Virtuoso

> What is missing in my code ?
the Text property of the ListControl is a System::String.
(it represents the currently selected string of the list control).
the variable file4 is an array of System::String ie. cli::array< System::String ^ > ^
if you want to set the selected string into the first element of this array, you could write

if( file4.Length > 0 )
     this->textBox1->Text = files4[0] ;
vijayan121 1,152 Posting Virtuoso

> how do i make the final calculation become 41.6 ?

#include <iostream>
#include <iomanip>
#include <sstream>

int main()
{
  std::cout << 374.0 / 9.0 << '\n' ;
  std::cout << std::fixed << std::setprecision(1) 
            << 374.0 / 9.0 << '\n' ;
  
  std::stringstream stm ; 
  stm << std::fixed << std::setprecision(1) 
      << 374.0 / 9.0 << '\n' ;
  stm.seekg( 0, std::ios::beg ) ;
  double result ;
  stm >> result ;
  std::cout << result << '\n' ;
  // *note* result == ( 41.6 += epsilon )
  // http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.16
  // http://en.wikipedia.org/wiki/Floating_point
}
vijayan121 1,152 Posting Virtuoso

> I have: Directory::GetFiles(Files,"*.txt");
> Files is in a format: cli::array<Type,dimension> ^
> How could Files be converted to System::string ^

presumably, the Type in cli::array<Type,dimension> is wchar_t and the dimension is 1.
all cli array types derive from System::Array and you could directly use the constructor of a System::String that accepts a Char[] . http://msdn2.microsoft.com/en-us/library/ttyxaek9.aspx

alternatively, you could use the static array<String^>^ GetFiles( String^ path ) http://msdn2.microsoft.com/en-us/library/07wt70x2.aspx which will return an array of *all* files in the directory. you could then test each file name to see if it ends in ".txt" by using bool System::String::EndsWith( System::String^ ) http://msdn2.microsoft.com/en-us/library/2333wewz.aspx

vijayan121 1,152 Posting Virtuoso

> I have seen some code where some variable gets inserted into string through the % sign
the code you saw was probably using the Boost.Format library to insert stuff into a stringstream. http://www.boost.org/libs/format/doc/format.html

vijayan121 1,152 Posting Virtuoso
template<typename DataType>
class Value
{
   // ...
   Value& operator +(const Value& aValue);
   // ..
};
template<typename DataType>
Value<DataType>& Value<DataType>::operator +(const Value<DataType>& aValue)
{ /* ... */ }

this operator+() only allows two Value template instantiations for the same DataType to be used. ie. you could add a Value<int> to another Value<int>, but not a Value<int> to a Value<short>.

also, the operator+ is not const-correct and tries to return a reference to an anonymous temporary.

perhaps this is what you intended

template<typename DataType>
class Value
{
public:
  Value(DataType val);
  // ...
  template< typename other >
  Value<DataType> operator+ ( const Value<other>& that ) const
  { return Value<DataType>( _value + that.GetValue() ); }
  // ...
  const DataType& GetValue() const { return _value; }
  //...
 protected:
  DataType _value;
};
vijayan121 1,152 Posting Virtuoso

> Hmmm, is there any significant, practical difference?
> I think not. Remember, that was 40 billion iterations.

for standard types like int or double, it would not make any difference. the compiler knows everything about these types and can see that a = a + b ; and a += b ; are equivalent and generate identical (optimized) code.

the case could be different for user defined types; for one the overloaded + or += operators may not be inline and the compiler cannot make any assumptions about the equivalence between a = a + b ; and a += b ; . if the operatos are inline, a good compiler can still optimize; even in this case, avoiding the creation of anonymous temporaries can improve performance.

int func_int_plus( int a, int b )
{
  return a = a + b ;
}

int func_int_plus_assign( int a, int b )
{
  return a += b ;
}

struct A
{ 
  A( int xx, int yy ) : x(xx), y(yy) {}
  A operator+ ( const A& that ) const 
  { return A( x+that.x, y+that.y ) ; }
  A& operator+= ( const A& that ) 
  { x += that.x ; y += that.y ; return *this ; }
  int x ;
  int y ;
};

A func_A_plus( A a, A b )
{
  return a = a + b ;
}

A func_A_plus_assign( A a, A b )
{
  return a += b ;
}

A& func_A_plus_assign_byref( …
vijayan121 1,152 Posting Virtuoso
char* soundex( const char word[], char sound[] )
{
   // ...
   sound="Z0000"; // *** trouble ****
   // ...
}

a literal string (eg "Z0000") consists of non-modifiable chars. if you had enabled all warnings, the compiler would have alerted you that something was amiss here.
eg. warning: deprecated conversion from string constant to 'char*' (g++)
the array name sound decays to a pointer (char*) when it is passed to a function and after this assignment points to non-modifiable chars.

strcpy( sound, "Z0000" ) ;

should fix it.

vijayan121 1,152 Posting Virtuoso
std::ifstream file( "filename.txt" ) ;
std::string line ;
while( std::getline( file, line ) )
{
   // parse the line which was just read
}
vijayan121 1,152 Posting Virtuoso

> which compiler is the most popular
the two mainstream c++ compilers are g++ (part of gcc) and vc++ (microsoft). and it is probably a good idea to stick to one of these; they are the most popular and attract the best support from third-party library and tool developers. both are also good as far as standards compliance is concerned. both (the express version of microsoft; the compiler is identical to that in the professional version) are free (well, neither is really free in terms of what you can do with them; but both are free in that you do not have to pay for them).
if you do not mind paying for a compiler, the comeau c++ compiler http://www.comeaucomputing.com/ would be a good option.

> if it can be used with .net so that once I develop an application I would like to to be portable to different platforms.
the notion of portability in standard c++ is source code portability; all these compilers would give you that.
the only c++/CLI compiler (that generates CIL which could run on many platforms) is the one from microsoft. other CLI implementations (like mono) do not provide c++ compiler support.

vijayan121 1,152 Posting Virtuoso

>> I have since realized that, given this problem, I can just as well use a single "instance manager" that simply manages a set of void pointers to data instances of a specific size. Since I have to do a cast anyway, I might as well do it "outside" the "instance manager".

>> The "void pointer instance manager" concept is also pretty much how I intend to manage the memory heap anyway, so I may be able to consolidate this code and save even more space.

>> I have considered that all of my "instance managers" could be in a list that is a static member of my base class. That would make them available to a base object without down-casting. But, I prefer to also make the type ID a static member of each derived class, so that wouldn't be available.

i'm not absolutely certain about what you are driving at; but would using an exemplar pattern be a solution to this problem?

the instance_manager_base class

// **** instance_manager_base.h ****
struct instance_manager_base
{
  static instance_manager_base* 
       get_instance_manager_for_type( int type_number ) ;
  virtual int type_number() const = 0 ;
  virtual ~instance_manager_base() ;
  // virtual other methods = 0 ;
  protected: instance_manager_base() ;
  private: instance_manager_base* This() { return this ; }   
};

an example of using the instance_manager_base

// **** test_it.cc ****
#include "instance_manager_base.h"
#include <iostream>
int main()
{
  instance_manager_base* mgr = 0 ;
  mgr = instance_manager_base::get_instance_manager_for_type(101) ;
  std::cout << mgr << '\n' ;
  mgr = …
vijayan121 1,152 Posting Virtuoso
Move Move::add( const Move &m ) const 
{
   return Move( x + m.x, y + m.y ) ;
}
superjacent commented: Concise, to the point, very helpful. +1
vijayan121 1,152 Posting Virtuoso

if your program is console based (no gui), you could use telnet. server versions of windows ship with a telnet server. all current versions of windows ship with a telnet client. see http://technet2.microsoft.com/windowsserver/en/library/ddf8b035-9035-475c-ae50-1d97bde83dba1033.mspx

you could also use several third party telnet server programs. eg. http://www.kpym.com/en/Overview.htm
putty is a very poular telnet client http://www.chiark.greenend.org.uk/~sgtatham/putty/

for gui access, you could use Remote Desktop (available only with professional/business versions). http://www.microsoft.com/windowsxp/using/mobility/getstarted/remoteintro.mspx

vijayan121 1,152 Posting Virtuoso

FileSystemWatcher (in CLR) uses the win32 api functions FindFirstChangeNotification and FindNextChangeNotification http://msdn2.microsoft.com/en-us/library/aa364417.aspx along with a wait function to get notifications of changes. to retrieve information about the changes, you coud use ReadDirectoryChangesW http://msdn2.microsoft.com/en-us/library/aa365465(VS.85).aspx

vijayan121 1,152 Posting Virtuoso

> .. obviously not writing in binary out mode because this is just text
ios_base::binary only disables the control sequence translations ( for '\n', '\r' etc.) that take place during formatted input or output. to write raw binary data to a file, you will have to use unformatted output functions. eg. basic_ostream<>::write

vijayan121 1,152 Posting Virtuoso

> what I intend is so simple, if (ARG::metrn) has some value declare EventStream evntstrTrain
> calling with the complex template, else declare it with the Lambda template.

if there are only a limited number of possibilities (like two in the example), you could use a variable of type boost::any http://www.boost.org/doc/html/any.html to hold the variable and do a type check later to figure out what it is.

#include <iostream>
#include <boost/any.hpp>
#include <typeinfo>

template< typename T > struct event_stream
{
  explicit event_stream( const T& v ) : member(v) {}
  void set_random_order(bool) { std::cout << member << '\n' ; }
  T member ;
  // ...
};

int main()
{
  int a = 8 ;
  boost::any any ;
  if( a > 5 ) any = event_stream<double>(2.34) ;
  else any = event_stream<int>(73) ;

  // ...

  if( any.type() == typeid( event_stream<double> ) )
    boost::any_cast< event_stream<double> >(
           any).set_random_order(true) ;
  else if( any.type() == typeid( event_stream<int> ) )
    boost::any_cast< event_stream<int> >(
           any).set_random_order(true) ;
}
vijayan121 1,152 Posting Virtuoso

well, for the simple example you posted, you could write

if(ARG::metrn==metrnSMD)
{
     EventStream<complex<Lambda>> evstrTrain(fileTrain, evlsTrain);
     if (ARG::FRandomEventOrder()) evstrTrain.SetRandomOrder(true);
}
else
{
     EventStream<Lambda> evstrTrain(fileTrain, evlsTrain);
     if (ARG::FRandomEventOrder()) evstrTrain.SetRandomOrder(true);
}

but this may not be pragmatic in more complex cases (where the second if is not proximate to the first if-else)

vijayan121 1,152 Posting Virtuoso

> I think the original problem (causing the compiler error) is this:
> EventStream<complex<Lambda>> evstrTrain( ...
he is using Visual Studio C++ 2005. which accepts this (perhaps in anticipation of c++0x).

vijayan121 1,152 Posting Virtuoso

the code in the if-else is equivalent to

if(ARG::metrn==metrnSMD)
{ EventStream<complex<Lambda>> evstrTrain(fileTrain, evlsTrain); }
else
{ EventStream<Lambda> evstrTrain(fileTrain, evlsTrain); }

so the variable is not visible outside the scope of the if or else blocks.

one commonly used work around is to have a polymorphic base class. this works if the method signatures are identical for all the template specializations. eg.

#include <iostream>

struct event_stream_base
{
  virtual ~event_stream_base() {} // this is the destructor; 
   // there is a problem with the daniweb code formatter
  virtual void set_random_order(bool) = 0 ;
} ;

template< typename T > struct event_stream : event_stream_base
{
  explicit event_stream( const T& v ) : member(v) {}
  virtual void set_random_order(bool) 
  { std::cout << member << '\n' ; }
  T member ;
  // ...
};

int main()
{
  int a = 8 ;
  event_stream_base* pesb = 0 ;
  if( a > 5 ) pesb =  new event_stream<double>(2.34) ;
  else pesb =  new event_stream<int>(73) ;

  pesb->set_random_order(true) ;

  delete pesb ;
}

(another is to use a meta-function; but it may be prudent to ignore this option for now.)

vijayan121 1,152 Posting Virtuoso

change cout << os ; to cout << os.str() ;
os.str() gets the string from the stringstream.
cout << os ; is equivalent to cout << (const void*)os ; prints the pointer value.

vijayan121 1,152 Posting Virtuoso
Salem commented: good answer +15
vijayan121 1,152 Posting Virtuoso

see: More Effective C++: 35 More Ways to Improve Your Programs and Designs (Scott Meyers)
ITEM 27. Requiring or Prohibiting Heap-Based Objects.
and http://www.aristeia.com/BookErrata/M27Comments_frames.html

vijayan121 1,152 Posting Virtuoso

check this library out. http://code.jellycan.com/simpleini/
i've not used it; but the reports that i've come across are good.
and the license is non-viral (MIT)

jaepi commented: uh huh! +2