spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories
  Home » C++ » Page 1
Why the macro is not defined by cmake?
As in the comment, use target_compile_definitions http://www.cmake.org/cmake/help/v3.0/command/target_compile_definitions.html However, as for the problem you encountered, CMAKE_CXX_FLAGS is a string, not a list. You can add content to it by dealing with it as a string: set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUGGING ")

Categories : C++

Qt udp socket, what will trigger the socket's readyRead signal?
I'm not sure but I have the feeling you mix up AnyAddress with broadcast then my understanding is that all the device on my LAN with port 14550 will trigger the readyRead signal if it writes something out using UDP. No. It triggers a readyRead when it sends an UDP packet on any address of your server it can reach. The UDP package has to be addressed to your computer. In this case ::Any m

Categories : C++

Should I return a pointer to beginning of an array or particular element?
It completely depends on what your code is supposed to do, and likely has no effect on optimisations at all. Either your class loads the pointer and dereferences it, or your call site does. Either way if you're doing it within the loop the result is exactly the same. You should measure it to be sure but I'd be surprised to see any difference here.

Categories : C++

C++11 implicit copy constructor while implementing explicitly a constructor
You did not declare a move constructor, but a regular constructor : no implicit constructor will be deleted. A move constructor would be of the form Foo(Foo&& arg) (with any cv-qualifier on the argument) Also note that this statement is not valid C++ : Foo::Foo(f); Maybe you meant : Foo g = Foo(f);

Categories : C++

Cast function pointers that differs by argument type
Yes, but you're trying to do it the other way around. You cannot do void takeDerived(Derived *c) { } ... Base b; takeDerived(&b); The function pointer cast you're trying to do would enable these shenanigans; you could do void (*ptr)(Base*) = takeDerived; Base b; ptr(&b); // Oops. Then things would explode, and that would be bad.

Categories : C++

C++ Ogre Runtime error 'Assertion failed'
This error usually appears if you are mixing release and debug builds. Most likely your application was built in release mode while you are trying to use debug build DLLs in this case of Ogre. You can use a tool such as Dependency Walker to check what DLLs exactly are loaded during your application's run. Another reason could that your Ogre DLLs where generated by a different IDE/compiler versio

Categories : C++

Check whether two elements have a common element in C++
You can use a well-defined algorithm called as std::set_intersection to check if there is any common element between these vectors. Pre-condition :- Both vectors be sorted.

Categories : C++

Why can I bind an int to a reference of a class with an int constructor?
it seems like the int from myList is being implicitly converted into a Foo instance via the int constructor, which is then being bound to the reference variable c. Yes, that's exactly what happens. But where does this object live? Unspecified, but it must behave like an automatic variable with the same scope as the reference, so probably on the stack just like an automatic variable.

Categories : C++

Segmentation fault sending a struct through TCP socket
The problem here is that one of the fields in struct student is a std::string object. This can't be sent across the wire directly like you're doing. Instead, either change it to a fixed-size character array something like this... struct student { int id; char name[100]; } ... and write your name into there. Or you'll have to send it across the wire in a different way.

Categories : C++

OpenMP: 'parallel for loop' bottle neck
Actually I have figured out why is this happening! It is because of the memory. I was trying to save results for each point in quadrant for all time steps. I was saving results in vector of double's and because of that quadrant structures where becoming too large. When I saved only the current time step data everything worked fine! Thanks everybody for the help.

Categories : C++

Overloading less than operater with different types
std::lower_bound is looking for: bool operator< (const double& cC1, const ClassA& cC2) You should simply define that in your ClassA and you'll be golden: bool operator< (const double& cC1, const ClassA& cC2) { return cC1 < cC2.one; } EDIT: leemes is right this needs to be defined outside your Class A so it accessable to std::lower_bound.

Categories : C++

Raw socket not picking up ARP requests
As ARP doesn't use IP packets, you can't use recvfrom, you have to use recv. See e.g. this example.

Categories : C++

C++03 Resolve a circle composition when calling a member function
Assuming E and C are adequately declared/defined, then what you have is almost fine. The problem is that you define the member function of A before you have the definition of the B class, make sure that class B is fully defined (the actual class, not the full implementation of its member functions) before you you have the A member functions implemented. So something like this: class B; class A

Categories : C++

C++ Pattern for a list of objects to be played sequentially
One solution would be simple composition #include <algorithm> #include <functional> #include <iostream> #include <iterator> #include <vector> class Exercise { public: Exercise( const unsigned int id ) : id_{ id } {} void play() { std::cout << "Playing excercise #" << id_ << ' '; /* play logic */ std

Categories : C++

Reassign an object on the stack in c++
I want to initialize the cell without using the "new" keyword. Initialization is done by the constructor. new is used to allocate dynamically (on the heap). These two concepts are orthogonal. So obvisouly, yes, you can do it. newEvent1 = new Event(*daughterCell, time, newDir); If you don't want to create it on the heap, create an object on the stack: Event newEvent1(*daughterCell, time, n

Categories : C++

C++:error syntax error
Accoording to Microsoft the C++ compiler of Visual Studio 2010 does not support range-based for loops. The error messages are about its inability to parse such.

Categories : C++

Unresolved External Symbol C++
You will have to specify in your executable projects that they reference the static lib. May be http://msdn.microsoft.com/en-us/library/vstudio/ms235627%28v=vs.110%29.aspx#uselibinapp helps (lower third of the article). Before you can use the math routines in the static library, you must reference it. To do this, open the shortcut menu for the MyExecRefsLib project in Solution Explorer, an

Categories : C++

Linking error: undefined reference
Running c++filt over the mangled name you identified from the library gives: $ c++filt _ZN7KWallet6Wallet10openWalletERK7QStringmNS0_8OpenTypeE KWallet::Wallet::openWallet(QString const&, unsigned long, KWallet::Wallet::OpenType) Note that the type of the second parameter here (unsigned long) does not match the declaration you provided for KWallet::Wallt::openWallet, which expects an unsign

Categories : C++

Reading values from a file and using classes
From a quick look it seems that the code opens the file "money.txt" three times. The first time in the function readMoney, the second time in getdollars and the third time in getcents. Probably this the problem. Open the file only once in readMoney function and pass the opened stream to the instance of Money.

Categories : C++

string to vector conversion throws std::bad_alloc
They're all bad, you just happened to get what looks like correct behaviour in the second case. The problem is that keySecret returns a different std::string object each time. You can't call begin on one and end on another and expect them to be related in any way. You should instead call keySecret once, make a local copy, then call begin and end on that single local object.

Categories : C++

Temporary copy exists in memory after multibyte to wide char conversion
Considering it's just (multi)byte to wide, a WCHAR lookup[256] may be a viable solution. You can init that table using a dummy string passed to MultiByteToWideChar - that's no security leak. Downside: this doesn't work for actual multibyte encodings.

Categories : C++

C++ show vowel and consonant and count it
Your if statement does not do what you expect it to. The correct syntax for this is if (let=='a' || let=='e' || let=='i' || let=='o' || let=='u') The reason the current versions is incorrect is because it is equivalent to if ((let=='a') or ('e') or ('i') or ('o') or ('u')) So 'e', 'i', etc are being evaluated for truthiness and ord to the first condition. Only an empty string in this case wi

Categories : C++

How to use string::erase (it adds garbage)
You might be seeing this bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60278 It's interesting though, I was able to get this to compile in gcc 4.8.1: data.erase(data.begin()); If it is the bug you can just implement the code that it's doing under the hood: copy(next(data.begin()), data.end(), data.begin()); data.pop_back();

Categories : C++

Error : No match for the operand
QSize is a class that describes a two-dimensional size (width and height). I suspect you meant to use realLeft % gridSize.width() and realTop % gridSize.height() respectively.

Categories : C++

Unordered map containing an Iterator to a Vector - Iterator not Dereferencable C++
The reason this code doesn't work, is that vector::push_back invalidates the iterators, that is, an iterator you had for a vector of size 3, might not work if you make the vector larger by adding a new element. From cppreference: If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated. Otherwise only the past-the-end iter

Categories : C++

unbalanced avl tree check function
Your balanceFactor function is correct. You just apply it to nodes starting from the root, going down the chain of unbalanced nodes, as described here, for example.

Categories : C++

Double header inclusion in C++?
The way the C language (and C++) is designed is that the compiler processes each .c file in isolation. You typically launch your compiler (cl.exe or gcc, for example) for one of your c files, and this produces one object file (.o or .obj). Once all your object files have been generated, you run the linker, passing it all the object files, and it will tie them together into an executable. That's

Categories : C++

non-destructive atomic add?
if(a + 4 < another_variable) // ... This is the best you can get with a single atomic. You are data-race free, as the reading of the atomic is safe against concurrent writes, and all subsequent operations happen on a copy of the original atomic value. A more verbose but functionally equivalent version would be: int const copy_of_a = a.load(); if(copy_of_a + 4 < another_variable) // ...

Categories : C++

Define datablock in C with "pointers"
Bottom line: you cannot have what you asked for in exactly the form you asked for. The preprocessor can do all sorts of amusing tricks, but it works left-to-right, top-to-bottom, so there is no way to make it look ahead to determine a relative offset to something it has yet to process. You should consider writing a function to perform this initialization. That would be by far the best alternati

Categories : C++

seekg, fail on large files
Changing the line in.seekg(index*length*sizeof(float), in.beg); into in.seekg((streamoff)index*length*sizeof(float), in.beg); solved the problem.

Categories : C++

recursive call should output reverse string
I believe this should work, but it's untested. void rev_out(istream& is){ char a; is >> a; if(is.bad() || is.eof()){ return; } rev_out(is); cout << a; } Essentially you check if the end of the stream has been reached before trying to output. If you haven't made it to the end, get the next one, then the output comes out as you fall back through th

Categories : C++

Write an algorithm to find the most frequently occurred element in the array. Give Time complexity of algorithm
Since this is an assignment, I will give you a hint only about upper bounds of complexity and a similar problem. This problem is a bit more difficult than the Element Distinctness Problem1. The element distinctness problem is known as cannot be solved better than O(nlogn) worst case. The solutions for element distinctness are: Sort and iterate - O(nlogn) Create a set/histogram of the elements a

Categories : C++

Video Mixing Renderer 9 (VMR9): get FPS from the input pin's AM_MEDIA_TYPE
You are coming from the assumption that filter and pin, interfaces of which you hold, are connected and they are exactly the objects you are interested in. This is not necessarily true and quite a few question in past showed that people incorrectly understand the topologies they create. You need to review the filter graph and ensure you have what you expect to have. See on this: How can I reverse

Categories : C++

Flipable issue with onEntered and onExited in QML Qt5
First I must blame You that your example is not working. Ok, so do not put MouseArea inside front and back as their width is dynamically changed and borders are touching your mouse generating entered and exited signals. You must set MouseArea as parent or sibling. Then you can use entered and exited signals or containsMouse property. import QtQuick 2.0 MouseArea { id: mouseArea width: 2

Categories : C++

How to turn on GDI+ 1.1 instead of 1.0 in MFC project?
I fought with a similar issue for a while on one project. For me, my precompiled header has this: #define GDIPVER 0x0110 // Use more advanced GDI+ features but the precompiled header does not #include "gdiplus.h". That only occurs in the .cpp files which actually make GDI+ calls. I forward declare GDI+ classes for the headers which have GDI+ object pointers as members. As Hans and other c

Categories : C++

C++ OGRE3D VS2013: Error LNK2038: mismatch detected for '_MSC_VER': value '1700' doesn't match value '1800'
As the error message states you are using incompatible binary versions. You cannot simply mix'n'match (neither the boost libraries nor the Ogre binaries). Downloading the correct binaries for boost should help: Boost pre-built VS2013 The most flexible and quite easy option for Ogre on Windows however is to simply compile Ogre yourself. You can even disable to boost integration completely via CMa

Categories : C++

C++ - Latitude and Longitude Distance Calculator
Something like this should work (not tested though): double toRad(double degree) { return degree/180 * pi; } double calculateDistance(double lat1, double long1, double lat2, double long2) { double dist; dist = sin(toRad(lat1)) * sin(toRad(lat2)) + cos(toRad(lat1)) * cos(toRad(lat2)) * cos(toRad(long1 - long2)); dist = acos(dist); // dist = (6371 * pi * dist) / 180; //g

Categories : C++

Checking if an C++ Pointer is valid (in Objective-C(++))
C++ pointers simply reference an address in memory. You could look at what's there in memory using a memory viewer tool, but that wouldn't even guarantee that the memory is still valid. For example: char* test = new[13]; strcpy(test, "Hello World!"); delete[] test; . . . printf("%s", test); In some cases this will print successfully. Sometimes it will print a garbage string. And sometimes it w

Categories : C++

Does passing reference to itself to constructor of base class causes undefined behavior?
As jrok pointed out, this is a compiler error. The compiler does not recognize the ambiguous call of the copy constructor and the explicit one. Further, a static_cast<A*> would have been the right choice there. So indeed, the cast is necessary according to the standard and MSVC2010 lacks in standard conform behavior. It just calls the copy constructor without even considering the actually

Categories : C++


Recently Add
GBA C++: How to draw multiple animating pixels using arrays?
Why the macro is not defined by cmake?
Qt udp socket, what will trigger the socket's readyRead signal?
Should I return a pointer to beginning of an array or particular element?
C++11 implicit copy constructor while implementing explicitly a constructor
Cast function pointers that differs by argument type
C++ Ogre Runtime error 'Assertion failed'
Check whether two elements have a common element in C++
Why can I bind an int to a reference of a class with an int constructor?
Segmentation fault sending a struct through TCP socket
OpenMP: 'parallel for loop' bottle neck
Overloading less than operater with different types
Raw socket not picking up ARP requests
C++03 Resolve a circle composition when calling a member function
C++ Pattern for a list of objects to be played sequentially
Reassign an object on the stack in c++
C++:error syntax error
Unresolved External Symbol C++
Linking error: undefined reference
Reading values from a file and using classes
string to vector conversion throws std::bad_alloc
Temporary copy exists in memory after multibyte to wide char conversion
C++ show vowel and consonant and count it
How to use string::erase (it adds garbage)
Error : No match for the operand
Unordered map containing an Iterator to a Vector - Iterator not Dereferencable C++
unbalanced avl tree check function
Double header inclusion in C++?
non-destructive atomic add?
Define datablock in C with "pointers"
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.