1,247 Posted Topics
Re: Parse the type string (int, char, float etc.) and the data in text form into strings. If the possible types are known before hand, a simple if/else if construct would do the job. [CODE]#include <string> #include <vector> #include <boost/lexical_cast.hpp> #include <stdexcept> template< typename T > void do_push_back( std::vector<T>& v, const … | |
Re: [url]http://msdn2.microsoft.com/en-us/library/ms533265.aspx[/url] [url]http://msdn2.microsoft.com/en-us/library/ms533235.aspx[/url] | |
Re: See [url]http://weblogs.asp.net/kennykerr/archive/2008/01/03/parallel-programming-with-c-part-4-i-o-completion-ports.aspx[/url] | |
Re: Add a const qualifier to the string in [ICODE]void *getfile( string url, [COLOR="Red"]const[/COLOR] char *file );[/icode] [B]CUROPT_URL[/B] requires a c-style string for the url. [ICODE]curl_easy_setopt(curl, CURLOPT_URL, [COLOR="Red"]url.c_str()[/COLOR] );[/ICODE] | |
Re: [QUOTE]Templating allows me to fill that vector with anything, not just classes derived from Node, which could screw things up. As such, this seems like a sloppy and potentially error-prone workaround.[/QUOTE] Use SFINAE to restrict the types to just Node or derived classes of Node. [QUOTE]Also, in the example, AddValues() … | |
Re: > The problem lies in the declaration of the lookup table. > All the functions are already defined in an external DLL, and I can't touch them. So write wrappers of your own that call the functions in the DLL. > The problem now is that the "WriteMemory" commands take … | |
Re: > you have to make sure is the the second thread is not just constantly checking the state The efficient (and elegant) way to do this is to use a condition_variable. [url]http://www.boost.org/doc/libs/1_42_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref[/url] | |
Re: > I want default value to be NULL so that whenever I encounter NULL > I know that there is no implementation for this function. > I wonder If NULL will be suitable for function that returns some type of value There are two ways that you can simulate this … | |
![]() | Re: Change [B]std::stringstream&[/B] to [B]std::ostream&[/B] in the overloaded operator and you would be ok. [CODE]class A { public: int num; A(int x) : num(x) {}; friend [COLOR="Red"]std::ostream&[/COLOR] operator<<( [COLOR="Red"]std::ostream&[/COLOR] stream, const A& someobject ); }; [COLOR="Red"]std::ostream&[/COLOR] operator<<( [COLOR="Red"]std::ostream&[/COLOR] stream, const A& someobject ) { stream << someobject.num; return stream; };[/CODE] Why … ![]() |
Re: > now that I've opened my eyes, the crash happens when I try to handle the objects contained in the vector. If you open your eyes a wee bit more, you would also notice that after [ICODE]delete temp ;[/ICODE] the vector contains (a copy of) a pointer which points to … | |
Re: modify the function to [code] int countchar( const char list[], char what ) { int i=0, count=0 ; for( ; list[i] != 0 ; ++i ) if( list[i] == what ) ++count ; return count ; } [/code] | |
Re: Use include guards. [url]http://en.wikipedia.org/wiki/Include_guard[/url] [url]http://www.steveheller.com/cppad/Output/homeward6.html[/url] | |
Re: IIRC, the a subsequent chapter of the book deals with completing this program. You would learn there about most of these issues (input and output, modulus operations, recovering from errors and so on), and be asked to refine the first draft of the program. So, perhaps the intent is to … | |
Re: For C++, you could base the course on Stroustrup's 'Software: Principles and Practice Using C++'. This is a book that is based on a course he designed for engineering freshmen at Texas A&M U. The support site for the book [url]http://www.stroustrup.com/Programming/[/url] also has some content for teachers - Instructor's Guide, … | |
Re: If you are just starting out on C++, these two would be high on the list of books: Stroustrup's 'Programming: Principles and Practice using C++' [url]http://www.stroustrup.com/Programming/[/url] Koenig and Moo's 'Accelerated C++: Practical Programming by Example' [url]http://www.acceleratedcpp.com/[/url] | |
Re: > That is the whole problem - I am not allowed to [B]#include <vector>[/B] in any header file You don't have to [B]#include <vector>[/B] in any header file, you just have to declare [B]std::vector<>[/B] in the header. 1. Declare [B]std::vector<>[/B] in [B]Myclass.h[/B] 2. [B]#include <vector>[/B] (define [B]std::vector<>[/B]) in MyClass.cc and … | |
Re: [CODE]class Cat : public Animal{ double weight; public: Cat(string name, double weight) : Animal(name), weight(weight) {this-> weight = weight;} void print() const { [COLOR="Red"]Animal::print() ;[/COLOR] [COLOR="Green"]// *** added *** [/COLOR] cout << "Weight = " << weight << endl; }[/CODE] | |
Re: > My first question is why is static used? > My understanding is that static objects are instantiated once and persist untill the end of the program In this particular case specifying a static storage duration does not make any perceptible difference. [CODE]int main() { int a[] = { 7, … | |
Re: [CODE]#include <iostream> template < int N > struct S : S<N-1> { S() { if( std::cout << N << ". Hello World\n" ) {} } }; template <> struct S<0> {} ; int main() { S< sizeof("twenty Hello Worlds") >() ; } [/CODE] > Try this one on for size... … | |
Re: [CODE]//set<*Node> neighbours; set<Node*> neighbours;[/CODE] | |
Re: > As far as I understand, each node in a list contains an element as well as pointers to both the following and preceding elements. > Both an integer and a pointer to an integer are 4 bytes, so one would think that a node would use up 12 bytes, … | |
Re: [url]http://www.icce.rug.nl/documents/cplusplus/[/url] [url]http://www.informit.com/guides/content.aspx?g=cplusplus[/url] [url]http://www.devx.com/cplus/Door/7042[/url] [url]http://www.cprogramming.com/tutorial.html[/url] [url]http://www.cplusplus.com/doc/tutorial/[/url] [url]http://www.learncpp.com/[/url] | |
Re: > Out of curiosity, is this even possible? Something like this would be possible in C++0x (after a constructor that takes a [icode]std::initializer_list<>[/icode] is added to the ublas matrix). [ICODE]boost::numeric::ublas::matrix mtx { {1,1,1}, {2,2,2}, {3,3,3} } ;[/ICODE] Right now, one work around would be to put the initializers for the … | |
Re: > Is there any way of checking all elements in a queue without calling pop(). Yes, if you have used a [icode]std::queue<T>[/icode] with the default [icode]std::deque<>[/icode] for the second template parameter. [CODE]template< typename T > void foobar( const std::queue<T>& queue ) { typename std::deque<T>::const_pointer begin = &queue.front() ; typename std::deque<T>::const_pointer … | |
Re: [B]> man setsockopt[/B] Or if you are not on Unix, see: [url]http://www.freebsd.org/cgi/man.cgi?query=setsockopt&apropos=0&sektion=0&manpath=FreeBSD+8.1-RELEASE&format=html[/url] And then look for the option [B]SO_RCVTIMEO[/B] | |
Re: [CODE]else { if(j >= i) //this small section makes sure that 'i' cant breed with itself and the selection is corrected for array re-arrangement [COLOR="Red"]// Don't we need to recalibrate the roulette wheel here?[/COLOR] iPopulation[iPopDensity - 1][iChromeLength + 2] = j + 1; else // ... }[/CODE] | |
Re: > can we pass arrays to functions You couldn't pass arrays to functions in C, and couldn't pass them in C++98 either. And you won't be able to do that in C++0x too. However, in C++0x you could pass a [icode]std::initializer_list<>[/icode] to a function; that is what [icode]someFunction({{1,2},{3,4},{5,6}});[/icode] attempts to … | |
Re: > i am not understanding about ,Preprocessor,Compiler ,linker , Loader ,CPU If you are just starting out, don't bother all that much about the gory details of what these different entities are right now. Just get a rough idea of what #include means, write the first hello world program, compile … | |
| |
Re: AFAIK, the main advantage of nested functions in languages that have them is to provide support for closures. [url]http://en.wikipedia.org/wiki/Closure_%28computer_science%29[/url] [quote]Closures typically appear in languages that allow functions to be first-class values, in other words, such languages allow functions to be passed as arguments, returned from function calls, bound to variable … | |
Re: > Thats exactly What I meant. put the declarations in the *.cpp file and it'll work. For starters, that will be a gross violation of ODR. see: [url]http://en.wikipedia.org/wiki/One_Definition_Rule[/url] You could of course write your own string class, and use it in any way that you please, as long as you … | |
Re: Don't really know what you are up to, but are you looking for something like this? [CODE]#include <iostream> #include <string> #include <cassert> #include <stdint.h> // C99, <cstdint> in C++0x uint64_t as_number( const std::string str ) { const unsigned int MAX_BYTES = sizeof(uint64_t) ; assert( str.size() <= MAX_BYTES ) ; union … | |
Re: you need to have a user defined type for performing arithmetic operations (atleast a plus) on integers with large precision. either write a big_integer class or use a library eg:class bigint from the LiDIA library ([URL]http://www.cdc.informatik.tu-darmstadt.de/TI/LiDIA/[/URL]) | |
Re: > I was creating the derived class method definitions in a cpp file > and couldn't get the project to progressively compile correctly. > At my first method definition, a default constructor, > the compiler kept spitting out this error message: this is perhaps the most obscure error message that … | |
Re: [CODE] class Test{ public: Test()=default; Test(char in1,char in2):char1(in1),char2(in2){} char char1; private: char char2; }; Test obj={'a','b'};//invalid in c++0x [/CODE] It is valid in C++0x. The final proposal is here: [url]http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2640.pdf[/url] | |
Re: > which kinds of constructors can be applied during compile time ? None in C++98; though constructors can be elided as in the case of RV optimization. Objects constructed via the the constexpr mechanism in C++0x. [url]http://www2.research.att.com/~bs/C++0xFAQ.html#constexpr[/url] | |
Re: Typically it doesn't, unless you force it to. For example: [CODE]> g++44 -Wall -std=c++98 -pedantic -Werror -fomit-frame-pointer -fstrict-aliasing -fconserve-stack -fno-defer-pop[/CODE] The frame of a function is transient, and the memory is reclaimed as soon as the function returns. It could also cause most debuggers to stumble. | |
Re: > Why does C++ seem more complicated than Visual Basic? Because it *is* more complicated than Visual Basic. But you don't need to be extraordinarily brilliant to learn C++ and to use C++ effectively. Almost all programmers who make a genuine attempt to become good C++ programmers do succeed in … | |
Re: [B]isnan()[/B] is from C99; and is not part of C++98. See [url]http://www.parashift.com/c++-faq-lite/newbie.html#faq-29.15[/url] In C++0x, [B]isnan()[/B] is part of TR1. You could easily roll out isnan() and isinf() on your own, though: [CODE]#include <limits> template< typename T > inline bool isnan( T value ) { return value != value ; } … | |
Re: > I asserted the flag -static-libgcc on compilation of my hello.cpp file (hello world program). > Apparently this is not enough though. Because there is still a segment in my executable which contains the name/path of the dynamic loader. > What flags do I use to generate an executable which … | |
Re: > as far as i understand the if statment should stop evaluating if the first condition is false > [as it is an && operation] (ie no player occupies the seat, therefore the pointer is invalid) > so it should not try to evaluate the player->inRound bit Your understanding is … | |
Re: Use [icode]std::getline[/icode] to read line by line. Use a [icode]std::istringstream[/icode] to extract the words in the line. [code]while( std::getline( file, line ) ) // read line by line { if( line == "" ) // just a '\n' in file { // an empty line, new paragrah logic } else … | |
Re: Use [icode]std::vector<bool>[/icode] for space efficiency (if N is large). And [icode]std::transform[/icode] and [icode]std::count[/icode] for programmer efficiency. [code]#include <iostream> #include <vector> #include <functional> int main() { const bool TAIL = false, HEAD = true ; enum { FLIP_THEM = 0, COUNT_HEADS = 1 } ; std::vector<bool>::size_type N, Q ; std::cin >> … | |
Re: Open the stderr log file for writing (with [icode]CreateFile[/icode]). Call [icode]SetStdHandle( STD_ERROR_HANDLE, log_file_handle ) ;[/icode] to redirect stderr for the process to the log file. | |
Re: Write a constructor for A [code]class A { public: A( /* ... */ ) : fv( length, fix_fac ) {} private: fixvec fv ; };[/code] | |
Re: > I have the number of paragraphs and now I want to store the number of lines in each paragraph in a vector. You do not need to calculate these separately - once you have the number of lines in each paragraph in a vector, the [icode]size()[/icode] of the vector … | |
Re: For MIPS, each ELF object module keeps track of its own OMF flags. The linker copies the appropriate flags from the original object module; if two different files have different sets of flags, unless they are conflicting flags, the more restrictive one is applied and a dignostic is generated: "xxx.o … | |
Re: > My operator+= works > However i cant get the operator+ to work The problem seems to be in your copy constructor; operator+ needs to make a copy. First, verify that the copy constructor [icode]Str2::Str2( const Str2& )[/icode] is working correctly. | |
Re: > warning C4293: '<<' : shift count negative or too big, undefined behavior 56, 48, 40 are greater than the number of bits in an int on the implementation you are using (which seems to have a 32 bit int). Convert (or cast) to a 64 bit integral type first. … | |
Re: > What am I missing? Making get_fields() a const member function. |
The End.