w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
  Home » C++ » Page 1
Using a class member as a default argument for a member function
[dcl.fct.default]/8: The keyword this shall not be used in a default argument of a member function. This is a special case of a general problem: You cannot refer to other parameters in a default argument of a parameter. I.e. void f(int a, int b = a) {} Is ill-formed. And so would be class A { int j; }; void f(A* this, int i = this->j) {} Which is basically what the compiler tr

Categories : C++

How to use gmock with xcode?
I was able to get GMock working in Xcode by following these steps: Download this repo somewhere on your machine: https://github.com/macmade/gmock-xcode Open the included Xcode project, build all the targets by pressing the play button, then close the project. Add the GMock Xcode project to your Xcode project as a subproject (can drag the GoogleMock.xcodeproj file to the project browser from

Categories : C++

Out of range vector subscript C++
You have an infinite loop because i is never updated in the for loop. You need to fix the typo: for (int i=0;i<88;i+=2) ^ ^

Categories : C++

One line solution for unused out parameter reference
The most obvious solution would be to use pointers: void Limits( double* min, double* max ) { if ( min != nullptr ) { *min = myMin; } if ( max != nullptr ) { *max = myMax; } } // ... double min; Limits( &min, nullptr ); Alternatively, you could just define separate functions: void Limits( double& min, double& max ); void UpperLimit( double& max

Categories : C++

boost::asio::yield_context: unexpected forced_unwind exception
This is a Boost Coroutine implementation detail. As documented here: exceptions ⚠ Important Code executed by coroutine-function must not prevent the propagation of the detail::forced_unwind exception. Absorbing that exception will cause stack unwinding to fail. Thus, any code that catches all exceptions must re-throw any pending detail::forced_unwind exception. So, you're explicitly

Categories : C++

Forcing the instantiation of static members in a template class
You could just create static instances: struct A : Y<A>{}; struct B : Y<B>{}; struct C : Y<C>{}; static A a; static B b; static C c; int main(){ std::cout << "Number of objects in the registry: " << X::getRegistry().size() << std::endl; } output: Number of objects in the registry: 3

Categories : C++

Linker error 2019 while linking Winusb application and CUDA
The linker looks at the symbols that are required by main.cpp (i.e. that are entry points needed by main.obj), and tries to find definitions for those entry points in other modules in your project. In the case of main.obj, there was one symbol (entry point) that it could not find defined elsewhere: 1>main.obj : error LNK2019: unresolved external symbol "short * __stdcall speccud(short * const

Categories : C++

C++ Reactor using base & derived classes containing functional objects of different types
When I try to compile this (g++ --std=c++11 reactor.cpp -pthread) I get a somewhat cryptic /usr/include/c++/4.9/functional:1665:61: error: no type named ‘type’ in ‘class std::result_of<void (*(int, InstructionsStore))(const int&, InstructionsStore&)>’ This appears to be the result of trying to pass stack references to the thread constructor. These are values that are goin

Categories : C++

eclipse ncurses and xterm, unknown characters printed
These characters are what initscr() outputs to do its job. A terminal knows not to show these characters and interpret them in a special way. Since the Eclipse console is not a terminal, it has not a faintest idea. If you want your program to work in both terminals and non-terminals, you need to check whether your standard output is a terminal, and avoid using ncurses-specific functions if it is

Categories : C++

Boost test expected throw
My psychic debugging powers tell me that your constructor is NOT in fact throwing QueueMessageException. It looks like it (through the function message2send) is throwing std::exception or a different child of it.

Categories : C++

Relationship between boost::exception and std::exception
As you said, boost::exception does not derive from std::exception. For the reason, check the corresponding FAQ : Despite that virtual inheritance should be used in deriving from base exception types, quite often exception types (including the ones defined in the standard library) don't derive from std::exception virtually. If boost::exception derives from std::exception, using the enable_

Categories : C++

Bitfield assignment - is it safe?
My main question is whether it is safe to flags = defaultFlags; in the object constructor, in order to eliminate the 20 lines for assigning each field individually? Yes. The implicitly defined copy constructor for Flags will assign each Bitfield appropriately. [class.copy]/15: Each base or non-static data member is copied/moved in the manner appropriate to its type: if the m

Categories : C++

Best inheritance hierarchy, on example of: Model3D -> ModelAnimated VS ModelAnimated or Model3D -> ModelAnimated
This is a bit subjective depending on your precise specifics. However as a general rule just avoid inheriting from concrete base classes, which leaves us with option 1. For example see why derive from a concrete class is a poor design for a bit of detail. From that question Or, as Scott Meyers puts it in Item 33 of More Effective C++,[8] "Make non-leaf classes abstract." The short version is tha

Categories : C++

Shuffle function does not work using a pointer
There are several errors in your code. Pointer which points nowhere Deck *p; p->shuffle(); //does not work! You've just created a pointer to a deck. Where is the actual deck to which the pointer points to? You should do Deck* p = new Deck; p->Shuffle(); or Deck theDeck; Deck* p = &theDeck; p->Shuffle(); Iterate till size, not capacity for(size_t x = 0;x<cards.capacity()

Categories : C++

How to get all the IPs(or username) from server of the clients connected to a shared print object?
Please check Print Spooler API patriculary EnumJobs function. Also check FindFirstPrinterChangeNotification function. You can create notification object for jobs and that way monitor who submited the job & what is printed on that printer.

Categories : C++

How can I get the values of the local variables via DbgHelp
The address of the variable's value you are looking for is at the address of the start of the function (accessible from EBP register of the execution context), applied from the offset of this variable location from the function in the stack. In your case (variable local to a function), pSymInfo->Address member stores the offset of the variable relative to the stack frame. (An explanation here : h

Categories : C++

How to get My object, derived from wxFrame through wxTheApp->GetTopWindow()?
There are two things here. First, you need to use wxGetApp() function. Unlike wxTheApp, it returns the application of the derived type, i.e. Application and not wxApp in your case (and it also returns it as a reference and not a pointer as it's never supposed to be null). Notice that, as explained in the documentation, you need to use wxDECLARE_APP() macro to declare this function in the header co

Categories : C++

C++ phrase counter with getche() function
After some tries I finally got the correct answer I liked to post it to help anyone interested .. thanks all for your help #include<iostream> using namespace std; #include<conio.h> int main() { int chcount = 0, wdcount =0, count = 0; char ch=' '; cout << "Enter your text : "; while ( ch != ' ' ) { if ( ch !=' ' ) { chcount++; count++;

Categories : C++

Poco AsyncChannel does not exit on forked process exit
Response here: http://pocoproject.org/forum/viewtopic.php?f=10&t=6378 Summary: create threads after fork and not before. Same problem with Poco::Util::Timer and other classes also.

Categories : C++

throwing an instance of std::length_error, example requires boost
The problem is that TestSegment returns vectors by value, e.g. in TestSegment::getChildSegments. That means, you make a copy of the vector. Now, in getCylinderlist you are calling std::vector::insert (which is the function that is mentioned in the error message of the exception) with the parameters segment.getCylinders().begin() and segment.getCylinders().end(). But these two calls to getCylinder

Categories : C++

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++




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