w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
  Home » C++ » Page 2
foo({0, 0}): Is this using initializer lists?
As stated by Piotr S., this is copy list initialization. The braced-init-list is used to initialize the function parameter. In copy-list-initialization, only non-explicit constructors are considered. This means that if Vec::Vec(int, int) was explicit, {0, 0} would cause a compiler error and Vec{0, 0} would be required.

Categories : C++

C++ elegant way to mark index which doesn't belong to a vector
The most elegant way to indicate that something you're looking for wasn't found in a vector is to use the C++ Standard Library facilities the way they were intended -- with iterators: std::vector<type>::iterator it = std::find (vec.begin(), vec.end(), something_to_find); if (it != vec.end()) { // we found it } else { // we didn't find it -- it's not there }

Categories : C++

C++ Accessing a variable that has been declared/ defined in one source file from another source file
There are a number of problems here. You define the variable TYPE (struct DataMessage....) in the header file but don't declare the variable itself The variable cannot be defined in the header file, only declared for external reference outside of the namespace. extern struct STEERDataIDL::DataMessage Data; in Module.cpp you don't seem to reference a variable named "Data" of type struct DataMessa

Categories : C++

Access QCustomPlot Variable
This was an isolated problem, but if anyone else has a similar problem my suggestion is this: Make sure that your GraphWidget variable (the variable to creates a QCustomPlot) is accessible by the entire class that is trying to use it. I was trying to pass it a bunch of different ways, and none of them were correct. Also make sure you create a public function in the GraphWidget class in order to

Categories : C++

how to enable __BEGIN_NAMESPACE_STD in stdlib.h
The macros expand to namespace std { and } respectively if the code is pulled in through a C++ standard library header. This leads me to believe that you're not #including stdlib.h directly (which is good!). Earlier versions of libstdc++ pulled symbols from C legacy headers into the global namespace even if the C++ versions of these headers (e.g. <cstdlib> instead of <stdlib.h>) were

Categories : C++

Can I assign void* pointer to char* pointer?
You have to use explicit cast to cast void* to any other pointer whereas other way is implicit. char* temp = static_cast<char*> (p); One thing to note here is your initial pointer ( which currently is represented by void* ) should be of type char* to avoid any issues.

Categories : C++

Specifying full library name in CMake
There are a few properties you can set on the target itself. PREFIX and SUFFIX are the ones you need: set_target_properties(foo PROPERTIES PREFIX "" SUFFIX ".mytest") You can also mess with the base name using the property OUTPUT_NAME, and should you ever find yourself on Windows and needing import libraries, the matching properties IMPORT_PREFIX and IMPORT_SUFFIX.

Categories : C++

What is the best way to build a C++ string using C format strings?
This breaks type safety, a feature that was one of the major benefits of moving from C to C++ in the first place. If you send the wrong data, you get runtime crashes or unexpected output, rather than nice cosy compilation failures. Try Boost.Format, which does what you're doing, but better: The format library provides a class for formatting arguments according to a format-string, as does prin

Categories : C++

Are sizers a good way to deal with a form?
wxGridSizer might not fulfil your needs but more flexible classes such as wxGridBagSizer should do what you want. This class allows you to set items to span multiple rows and columns by setting wxGBSpan when calling wxGridBagSizer::Add.

Categories : C++

Accessing private instances of classes from another class
In the header, your Texture2D* is called _pacmanTexture, whereas in your implementation, you've called it _playerTexture. Similarly, you've misspelled _playerPosition in the header.

Categories : C++

How to edit an element in a streaming char array
From your sample code it is not clear what is the expected output. Is it an array of integer ? a formatted text string ? a byte array ? we can't know. Assuming you have a text formatted input and want a text formatted output, a simple solution is to write a new string with the correct values and not try to modify your input buffer. If you know the exact format of your input records you could use

Categories : C++

GLSL: Rendering a 2D texture
After mulling over it for a few days, the issue was with how to send sampler2D uniforms into GLSL: glBindTexture( GL_TEXTURE_2D, mTextureID ); glContext.textureShader->bind(); glContext.textureShader->setTextureID( mTextureID ); was corrected to: glBindTexture( GL_TEXTURE_2D, mTextureID ); glContext.textureShader->bind(); glContext.textureShader->setTextureID( 0 ); setTextureID()

Categories : C++

Worker class for multithreading in Qt
have a look at: what is the correct way to implement a QThread... (example please...) and in addition to my comment, i do not think it is save to allocate an object on the stack and move it to a different thread.

Categories : C++

Why pointer from override class use operator== and operator!= from base class
I've not looked at the details in your code, but... It looks like you're trying to make the iterators polymorphic. The standard constantly passes them by value, however, and pass by value doesn't support polymorphism; it slices. Thus (I assume), in: for ( int j : *i2 ) the compiler creates local variables for the iterator, whose type is determined statically. And although it calls the corre

Categories : C++

Structures for dummies
struct in C++ is basically the same as classes with one difference. In class defualt access specifier is private whereas in struct it's public. So what you're seeing in that code are:- constructors, overloaded operators +,* for you struct and a method to compute magnitude of a complex number.

Categories : C++

Reading arrow keys with C++
You have such problem because you just ask the wrong question. If you application is a command line tool and is accessible from a terminal, than it's just impossible to know which keys are pressed at the moment because the terminal can be far away from the machine where your application runs and which is more important, there is no reason for terminal to send you the arrow key presses because term

Categories : C++

Data file handling modify function error c++
Instead of fstream fp("flight.dat",ios::binary); write: fstream fp("flight.dat",ios::binary|ios::in|ios::out); P.S.: Encountered Same Problem A minute ago..

Categories : C++

Syntax error on using gtest with Android NDK
It looks like C++11 + stlport is the bad combination for gtest. This isn't terribly surprising, given that stlport doesn't actually have any support for C++11 (some parts of it will work, others apparently won't). If this only causes problems when building gtest, you could just not use C++11 when building gtest, but still for your code. If you can't use it from C++11 either, you're probably out o

Categories : C++

How to convert vector iterator to int in C++
You just dereference the iterator like this index = *it; However you should first see if you actually found something it = find(myvector.begin(), myvector.end(), x); if (it != myvector.end()) { index = *it; } To find the index in that the match was found, you can use subtraction of the found pointer from the start of the vector. it = find(myvector.begin(), myvector.end(), x); if (it

Categories : C++

How Critical Section object works exactly for multiple methods
Your both assumptions are correct as per requirements. Basically critical section is a code where you want only single thread to enter. Till that thread finishes no other thread is allowed to enter into that critical section.And that critical section could contain multiple functions as well. You hold mutexes at the begining of critical sections which you release after leaving that section.

Categories : C++

Is a static member initialized before the constructor is called?
This is what the standard says :- The storage for objects with static storage duration (basic.stc.static) shall be zero-initialized (dcl.init) before any other initialization takes place. Zero-initialization and initialization with a constant expression are collectively called static initialization; all other initialization is dynamic initialization. Objects of POD types (basic.types) with static

Categories : C++

Can't link ncurses with Qt
http://www.lucidarme.me/?p=3961 Here You can find a solution. Main reason is You incorrectly set project: LIBS += -lncurses Check a link I sent You. Everything is there clear.

Categories : C++

How Read Urdu text file and then write it to other file in c++
Oh I got answer. The answer is you have to put spaces between Urdu characters because Urdu language has Space Omission Problem so at while loop while(file>>word) { myfile<<word; myfile<<" "; // Put spaces between words. }

Categories : C++

How to cast char array to int at non-aligned position?
No you can't do that in a portable way. The behaviour encountered when attempting a cast from char* to int* is undefined in both C and C++ (possibly for the very reasons that you've spotted: ints are possibly aligned on 4 byte boundaries and data is, of course, contiguous.) (The fact that data+3 works but data+p doesn't is possibly due to to compile time vs. runtime evaluation.) Also note that

Categories : C++

Inconsistent parameter pack deduction with variadic templates
The way you are calling your function template, the template parameter pack Args will be deduced from two sources: The type of the pointer to member function - int (Calculator::*)(const int&, const int&) The actual types of the arguments (*a, *b) you passed for the function parameter pack - int &, int & For deduction to succeed, the result deduced must exactly match. They obvio

Categories : C++

Why am I permitted to declare an object with a deleted destructor?
It's that B's destructor is generated by the compiler at the line of your error and it has a call to A's destructor which is deleted, hence the error. In the first example nothing is trying to call A's destructor hence no error.

Categories : C++

Connecting to Serial port in QT
First of all, you should check is open returns true. If no, then tell to user about error and call errorString() if(serial.open(QIODevice::ReadWrite)) serial.write("ok*"); else { //error qDebug() << serial.errorString(); } You try to open one port in different programs. It is forbidden in Windows. So you can't use this. In your video author open com3 in Qt but com4 in teraterm

Categories : C++

delete (free) elements from stringstream
Since std::istringstream uses std::string to store its data, the contents of buffer is copied to a string that is stored inside the stream. So to answer your question, yes you can free the buffer as soon as you created and initialized the string stream. However, a more interesting question back to you is why you're using C file handling instead of C++ streams directly? And why read the whole file

Categories : C++

Can conversion functions be non-member functions
Conversion operators are specific to class i.e they provide a means to convert your-defined type to some other type. So, they must be the member of a class for which they are serving purpose :- for e.g:- class Rational { public: operator double (); }; Here operator double provide a means to convert Rational object to double.

Categories : C++

How can I use std::lower_bound when I'm only interested in comparing part of the value and can't assume a default constructor for the other part
You can pass a heterogeneous comparator (i.e. one which takes arguments who's types are not the same) to lower_bound. The first argument will come from a dereferenced iterator from the range, the second argument will come from the value passed as the third argument to lower_bound. So, you can use something like this: struct Compare { template<typename T1, typename T2> bool operator(

Categories : C++

How do I get the Polynomial Interpolation coefficients using gsl_interp?
After a quick scan over the documentation, it doesn't seem that such a feature is available in the GSL. This could be caused by two reasons: first, getting polynomial coeffcients is special to this interpolation method doesn't fit well into the general design (which can handle arbitrary functions). Second, citing Numerical Recipes: Please be certain, however, that the coefficients are what you

Categories : C++

Kernel Resource Leak BIO_do_connect
BIO_gethostbyname is not (cannot be) threadsafe, since it returns a pointer to a static buffer. Indeed, there is mention of this in openssl's bio.h, reading struct hostent *BIO_gethostbyname(const char *name); /* We might want a thread-safe interface too: * struct hostent *BIO_gethostbyname_r(const char *name, * struct hostent *result, void *buffer, size_t buflen); * or something similar (

Categories : C++

Is there a penalty to using char variables in CUDA kernels?
Arithmetic It's not possible to say in the generic sense whether it'll be faster/slower/unchanged, though usually I'd not expect much difference. You're correct in saying that arithmetic for chars will be in 32-bit, but whether this requires a type conversion will depend on the problem. In the question's example, I'd expect to see the compiler store a and b in 32-bit registers, and in my experime

Categories : C++

Changing the title of the MessageBox for DDX_Text()
Internally AfxMessageBox is called. This function always uses the application title as a title for the message box. The value is stored in CWinApp::m_pszAppname. It is just a LPCTSTR. Just create a class like this. class CChangeAppTitle { public: CChangeAppTitle(LPCTSTR pszNewTitle) : m_strTitle(pszNewTitle) , m_pszOldTitle(AfxGetApp()->m_pszAppTitle) { AfxGetApp()->m_pszAp

Categories : C++

Return the wrong type in C++
This could be a result of non-explicit constructor of Color class accepting unsigned char as its argument. That means you have constructor in Color class with single argument OR multiple arguments rest being default parameters.

Categories : C++

What the point of using std::ios_base::binary?
The differences between binary and text modes are implementation defined, but only concern the lowest level: they do not change the meaning of things like << and >> (which insert and extract textual data). Also, formally, outputting all but a few non-printable characters (like ' ') is undefined behavior if the file is in text mode. For the most common OSs: under Unix, there is no dis

Categories : C++

Setting a callback function which is non static member function of a class
You should think about what a callback really is and how a member function is called. When you give a callback function, you just give the address of a function that will later be called with parameters on which you normally have little control. When a member function is called, its first parameter is the this pointer, that is a reference to the object on which the method is called. That's the

Categories : C++

cmake can't find boost libraries because it looks for the wrong file names
Note: boost_thread-vc120-mt-1_56.lib is an import library allowing dynamic linking with boost_thread-vc120-mt-1_56.dll, while libboost_thread-vc120-mt-s-1_56 is a static library (s letter means it's also statically linked with CRT). The application you try to build expects static Boost libraries having static CRT, so you should provide them. To build such libraries, invoke b2 with the appropriate

Categories : C++

parsing command-line options with sequence containers?
Actually what you have there is more akin to an expression grammar. I'd suggest writing a grammar/parser for that instead of (ab?)using program_options for this. If your program takes options: use program options. If your program takes an expression: use an expression parser. An example: Live On Coliru // #define BOOST_SPIRIT_DEBUG #include <boost/fusion/adapted/struct.hpp> #include &

Categories : C++




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