w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
  Home » C++ » Page 5
Why does program continue in while loop for any character entered?
Add one condition to if -else section: if(nextRoman == 'Y') { done = false; } else if(nextRoman == 'N') { done = true; cout << "Thanks for roman numeraling with me. " << endl; } else { done=false; cout<<"see you next time"//or anything you want

Categories : C++

Dynamic Allocation of Array Inside of a Function
Your function return must match the type of pointer you wish to return. If you want to allocate an array of float, then your function must be of type float *. The following is a quick example: #include <stdio.h> #include <stdlib.h> float *allocateBuff(float** buffer, int size) { /* allocate array of floats */ *buffer = calloc(size, sizeof(*buffer)); return *buffer; } i

Categories : C++

C++ automatic pre-processor code generation
Could I have a PRE-PROCESSOR function such that I just write This file: #define read_vector(x) std::string z_##x##_file("z_"#x".txt"); z_##x##_file = input_folder + z_##x##_file.cstr(); Eigen::Matrix<PRECISION, Eigen::Dynamic, Eigen::Dynamic> z_##x##_ = readArbitraryMatrix<PRECISION>(z_##x##_file.cstr()); Eigen::VectorXf z_##x = z_##x##_; read_vector(A); read_vector(B); After

Categories : C++

std::vector emplace_back vs std::deque push_back?
Whether it will improve performance or not will depend greatly on your usage pattern: how much you push, how often you pop, how expensive is element copying (or moving) during vector reallocation and so on and so forth. There's no way to say anything definitive without looking at the specifics. Try it, profile it and see which one works better. BTW, you can specify which container type std::stack

Categories : C++

How to loop std::set with custom .begin() and .end()?
auto i = magic.find(3); if (i != magic.end()) { auto finito = i; do { std::cout << *i << ' '; // or whatever you want to do with elements if (++i == magic.end()) i = magic.begin(); } while (i != finito); }

Categories : C++

How do I define an array in C++ that holds GMP variables?
Well, summing up all the comments above, if you are dealing with a lot of array modification in your code(insertion/remove) it is best to use vector<mpz_class> to hold your mpz_class variables. And the reason that it is better for you to use mpz_class instead of mpz_t is first you don't have to worry about keeping track with memory you use for the variables(mpz_init()/mpz_clear()) and second

Categories : C++

Qt Audiorecorder example - Connection of button
The connection can also be created in a *.ui-file. If you inspect the audiorecorder.ui you'll find the following code: <connection> <sender>recordButton</sender> <signal>clicked()</signal> <receiver>AudioRecorder</receiver> <slot>toggleRecord()</slot> <hints> <hint>...</hint> </hints> <

Categories : C++

Socket Server with Multiple clients. Killing child processes
The hanging child processes are probably zombie processes. You could do for example following for avoiding problem: Use waitpid() of wait() in parent process to get rid of zombies. Use 'double fork' trick to put child processes under init process.

Categories : C++

Browser of file
You've got a ; instead of a : before the initialiser list for your constructor: Browser(USBDevice dev, QWidget* parent = 0); // <--- here QTreeWidget(parent) { ... FWIW, I recommend putting the colon at the start of the line like this: Browser(USBDevice dev, QWidget* parent = 0) : QTreeWidget(parent) { ... That way it's much clearer how the second line relates to the fi

Categories : C++

How can I add many json strings in one JsonArray?
I'm not using json-glib, I couldn't test this code, and I omitted the part to release the builders, but I hope this helps you: // build an object as an element of 'tag' array. // {"one": "value", "two": "value"} JsonNode * objectAsElementOfTag; { JsonBuilder *builder = json_builder_new (); json_builder_begin_object (builder); json_builder_set_member_name (builder, "one"); json_builder_ad

Categories : C++

How can I correctly convert this to hex?
Try using the str member instead of the extract operator, to eject the string from the stringstream. Besides being more direct, you don't have to worry about how the extractor further interprets things.

Categories : C++

nFinding Xcrossing point using Data Points
This appears to be a straightforward implementation. For a given point x,y on the curve, find the slope, draw a line through the slope, find where that line would cross the x axis, and then find the new y value at that point. For well-behaved functions, the new y value is a lot closer to 0 than the initial y value. You iterate until the approximation is good enough. If you look at the graph, you

Categories : C++

Qt : How to get QComboBox item text at an arbitrary index (not the currently selected item)
Use itemText() method to do this. Just set needed index. As Marek R pointed, you can get model and get data from this model, but it will be helpful for you when you want to get something more than text( for example get image with Qt::DecorationRole or font with Qt::FontRole) http://qt-project.org/doc/qt-4.8/qcombobox.html#itemText http://qt-project.org/doc/qt-4.8/qcombobox.html#model

Categories : C++

Embedding Window Media Player in mfc
MSDN says: For full-screen mode to work properly when embedding the Windows Media Player control, the video display area must have a height and width of at least one pixel. If the BSTR specified in IWMPPlayer::put_uiMode is set to "mini" or "full", the height of the control itself must be 65 pixels or greater to accommodate the video display area in addition to the user interface. This assum

Categories : C++

Partial template specialization for variadic template where [Args...] is empty
Your attempt with using R Delegate<R()>::operator()() to specialize even more the member function of a partial specialization of a class template fails due to §14.5.5.3 [temp.class.spec.mfunc]: 1 The template parameter list of a member of a class template partial specialization shall match the template parameter list of the class template partial specialization. In other words: templ

Categories : C++

Using Windows Bluetooth API on OS X/Mac?
The QtBluetooth module currently supports Android, BlackBerry 10 and Linux (Bluez 4.x) and indeed, OS X and Windows are not supported yet. You should write the bluetooth functionality you need in XCode and then import it into your Qt project as a linked library. You can also have a library in which you use platform specific macros like #ifdef Q_OS_WIN and #ifdef q_os_osx and put your platform sp

Categories : C++

Heap error in VS2010
you input whole string into 1 or 2 bytes strings. These allocations: _TCHAR* attString = new _TCHAR(); char* ynAnswer = new char(); Allocate only 1 item array. it should be: _TCHAR* attString = new _TCHAR[MAX_SIZE]; char* ynAnswer = new char[MAX_SIZE]; When MAX_SIZE must beeb defuned as macro. even better is to use: wstring attString ; string ynAnswer; wcin>> attString; cin&g

Categories : C++

Error pointer in C++
The code: P->PGet().XSet(8); Probably does not do what you think it does.. It is most likely that `P->PGet() returns a copy of the "Point" and not the internal structure. The proof of that will be in the definition of PGet().. My guess is that the function looks something like this: CPoint3D Prune::PGet() const This means that the function PGet returns a copy of CPoint3D. and so P->PGet(

Categories : C++

Multiple insertion operator << in C++
You probably meant to write some code like follows #include <iostream> using namespace std; class MyClass { public: int i; MyClass & operator<< ( const string & ); } ; MyClass& MyClass::operator<< ( const string & str) { // Note the parameter symbol cout << str << endl ; // Note usage of the parameter symbol "str", "this" i

Categories : C++

Keep smallest n elements in set, discard others
Option 1 if(baseSet.size() > n) { // Use appropriate case if necessary auto it = base.begin(); // std::set<unsigned>::iterator std::advance (it, n); baseSet.erase(it, baseSet.end()); } Find nth smallest element and erase range from that element to end. Option 2 c++11 onwards, you can also do this as following (Thanks to bames53 for the comment) baseSet.erase(next(begin(baseS

Categories : C++

QTextStream doesn't write before QThread::sleep()
The problem is not related to multithread. The reason is that your input is being stored in buffered data, which has not been written to stdout yet. In order to force the stream to write its data, please call QTextStream::flush() before sleep() =) #include <QThread> #include <QTextStream> QTextStream qout(stdout); int main() { qout << "hello world "; qout.flush();

Categories : C++

Why is a destructor called if it's deleted and not called if it's not deleted?
The problem is that the constructor of B is deleted by the compiler, as otherwise the default definition would be ill-formed. That's because A has no destructor, and the default constructor of B cannot construct a B from A if A cannot be destroyed. That's the error you are getting if you compile with g++: note: 'B::B()' is implicitly deleted because the default definition would be ill-formed: or

Categories : C++

Premake not working with g++ on Linux?
That $(ARCH) variable has been in the generated makefiles for quite some time, but it appears it has outlived its usefulness. I have removed it for now; if there is still a need to inject flags into the build we can find a safer name. You can get a fixed version from the Premake 4.x BitBucket repository.

Categories : C++

Converting std::int to System::Single
If you're in C++/CLI, you can do it the same way as you do in C++: float myFloat=*(float*)&myInt; In pure managed-land, there are built-in methods to do this for double & Int64 (DoubleToInt64Bits and Int64BitsToDouble, but not for single & Int32. However, if you look at the implementation of those methods (MS Reference Source), you'll see that they're doing the exact same thing as you

Categories : C++

bubble sort random numbers C++
Wikipedia has a complete article about bubble sort. There you can find a complete description of the algorithm as weel as pseudo-code. http://en.wikipedia.org/wiki/Bubble_sort Note that bubble sort is by no way efficient. In c++11 sorting array is already implemented in an efficient way using std::sort in algorithm http://www.cplusplus.com/reference/algorithm/sort/

Categories : C++

TLS enumerable_thread_specific in TBB
The idea of enumerable_thread_specific is to provide a container around the concept of TLS or thread_local in C++11 so that a value assigned by one thread can be later combined/enumerated in another thread. What actually contributes to the performance benefit is this common property of the aforementioned concepts. Generally, TLS allows to avoid contention between threads for the processor cache o

Categories : C++

Algorithm to determine that a 2x2 square contains the numbers 1-4 (no repeats)
I would probably start with an unsigned int set to 0 (e.g., call it x). I'd assign one bit in x to each possible input number (e.g., 1->bit 0, 2->bit 1, 3->bit 2, 4->bit 3). As I read the numbers, I'd verify that the number was in range, and if it was, set the corresponding bit in x. At the end, if all the numbers are different, I should have 4 bits of x set. If any of the numbers was repeated, s

Categories : C++

Define a dynamic matrix with EIGEN
I can compile and run this without any issue (g++ 4.9), so this is not the source of the error. Although this is not an answer, I posted as I cannot post code in a comment. template< class T, int cols > class RowDynamicMatrixRowMajor : public Eigen::Matrix< T, Eigen::Dynamic, cols, Eigen::RowMajor | Eigen::AutoAlign > { public: typedef Eigen::Matrix< T, Eigen::Dynamic, col

Categories : C++

Getting an additional "0" after for loop
Replace the line: cout << printrecord(students[k]); with printrecord(students[k]); The first one prints out the value the function returns, 0 in your case. In fact your print function actually doesn't need a return value so you should probably change its signature to void printrecord(gRec record) and remove the return statement at the end.

Categories : C++

std::call_once vs std::mutex for thread-safe initialization
If you read this you'll see that std::call_once makes no guarantee about data-races, it's simply a utility function for performing an action once (which will work across threads). You shouldn't presume that is has anything close to the affect of a mutex. as an example: #include <thread> #include <mutex> static std::once_flag flag; void f(){ operation_that_takes_time(); std

Categories : C++

How to access a subclass members
The problem is that you are trying to access non-static members through the class type Node instead of accessing them through an instance of Node. Although you did not include enough relevant portions of BinarySearchTree it appears you have a member called _root which based on other code seems to be the root node in your tree. Assuming that is correct the following lines will need to be updated.

Categories : C++

Creating arrays in C++ without knowing the length ahead of time
You could either use a C++ container, like std::vector, or use dataPoint *data = malloc(LENGTH * sizeof(dataPoint)). In C++, it's probably better practice to use std::vector. C99 has variable-length arrays, but you're question seems to be C++. The real problem, though, is that you want to slurp the whole file all at once. Unless you're using a library that requires data to be in memory, it'll pr

Categories : C++

Why do we need a default constructor to pass an object by reference in C++?
It would be helpful to know what compiler you are using, which standard of c++ it complies to and if you have other constructors for MyClass. What I can imagine is that the compiler does not generate a default constructor MyClass() because you have defined another constructor. Should the compiler ever want to generate a default object of class MyClass like in class MyClass mc; The only way t

Categories : C++

Cant pinpoint where "cannot convert 'double' to 'std::string*' in assignment" also yyparse() and yylex() error
Here's one place where the double to std::string* error will be produced: assignment : identifier '=' exp {$$ = vars[*$1] = $3; delete $1; } The type of assignment is <index>, which is std::string*. The type of exp is <num>, which is double. So in the action above, $$ is a std::string*, and $3 is a double (as is vars[*$1]). My best guess is that you didn't intend the semantic type

Categories : C++

Calling a function from a funtion pointer in a map C++
Contrary to your belief, your map doesn't hold function pointers. So you cannot call the elements in the map. Rather, your map contains pointers to member functions. Non-static member functions aren't functions and cannot be called; rather, they have to be invoked on an object. You can invoke a member function on an object given by a pointer p via a function pointer ptfm like this: (p->*ptmf)

Categories : C++

Use code in DLL from the loading binary
The compile flag -export-dynamic will solve this problem. Here is the description from the gcc documentation: Pass the flag -export-dynamic to the ELF linker, on targets that support it. This instructs the linker to add all symbols, not only used ones, to the dynamic symbol table. This option is needed for some uses of dlopen or to allow obtaining backtraces from within a program.

Categories : C++

reading from a file lines and numbers in c++
Can just read line by line, using std::getline string strline; while(getline(file, strline)) { cout << strline; } If you need the actual value of the number, can use a std::istringstream (must #include <sstream>), like string strline; if(getline(file,strline)) { // do something with the first line } if(getline(file,strline)) // read the second line { istringstream

Categories : C++

build c++ in debug mode with biicode cmake
From the biicode docs, it looks like you need to use the bii cpp:configure command to pass arguments to CMake, not bii cpp:build. As @ruslo commented, it's not good practice to set the CMAKE_BUILD_TYPE inside the actual CMakeLists.txt (see his answer for more details), so you probably want to do: bii cpp:configure -DCMAKE_BUILD_TYPE=Debug bii cpp:build I realise you said you are working with U

Categories : C++

Cannot call bool function with array program
you say bool validateSums(int, int); then bool validateSums(int row, int square[][10]) These are not the same. The compiler and linker is trying to find the validateSums function that takes 2 ints. You didn't supply one - hence the error message Not wanting to read all your code I don't know which one you actually need: int,int or int,[][10].

Categories : C++




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.