c++

C macro computing the number of bytes that a given compile-time constant requires

Often I have some compile-time constant number that is also the upper limit of possible values assumed by the variables. And thus I'm interested in choosing the smallest type that can accomodate those values. For example I may know that variables will fit into <-30 000, 30 000> range, so when looking for a suitable type I would start wit...

The most elegant way to write an abstraction layer

Hi! I'm curious how to write an abstraction layer. By abstraction layer, I mean a wrapper above one or more 3rd party libraries. Or do I have to solve it like this? #include<an3rdpartyl> #include<another3rdpartyl> class layer { private: an3rdpartyl* object1; another3rdpartyl* object2; public: //... int loadModel(char* ...

[C++] deleting while iterating

Possible Duplicates: Vector.erase(Iterator) causes bad memory access iterate vector, remove certain items as I go. Hi, I wrote this but I am get some errors when running it for (vector< vector<Point> >::iterator track = tracks_.begin(); track != tracks_.end(); track++) { if (track->empty()) { // if track is empty, r...

How to reduce a number of parameters of a template template parameter

Hi, I need to adapt a two parameter template to a one parameter template. I would like to bind the first parameter of a template: template<class T1, class T2> struct Two_Parameter_Template { // Two ctor's Two_Parameter_Template() { }; template<class Param> Two_Parameter_Template(Param) { /* ... */ } }; by using antoher ...

Proper way to #include when there is a circular dependency?

I'm using #pragma once, not #include guards on all my h files. What do I do if a.h needs to #include b.h and b.h needs to #include a.h? I'm getting all sorts if errors because by doing this, the pragma once takes effect and one of them is missing each other. How should I do this. Thanks ...

c++ performance issue beginner (size function in for loop)

hi when having a vector<int> var; for(int i=0; i< var.size();i++) , is the size() function called each time or only once ? from the answers I guess I better use iterators , or just have variable before the loop ...

Sorting Numerical Data Alphabetically

i am trying to solve this task: http://stackoverflow.com/questions/719614/sorting-numerical-data-alphabetically here is my code #include <iostream> #include <math.h> #include <vector> #include <algorithm> #include <iterator> #include <ostream> using namespace std; float count(int n) { float total=0; while (n!=0) { n/=1...

Why am I getting these linker errors?

I'm getting the following linker errors: Error 1 error LNK2001: unresolved external symbol "public: __thiscall AguiEvent<class AguiEmptyEventArgs>::AguiEvent<class AguiEmptyEventArgs>(void)" (??0?$AguiEvent@VAguiEmptyEventArgs@@@@QAE@XZ) AguiWidgetBase.obj Error 2 error LNK2001: unresolved external symbol "public: void __this...

Detecting array type do not works.

template<typename T> class CompoundT { // primary template public: enum { IsPtrT = 0, IsRefT = 0, IsArrayT = 0, IsFuncT = 0, IsPtrMemT = 0 }; typedef T BaseT; typedef T BottomT; typedef CompoundT<void> ClassT; }; template<typename T, size_t N> class CompoundT <T[N]> { // partial special...

How can I create a C++ project/solution in Visual Studio 2010?

I want to write simple C++ code for adding two integers (in a command line window). How do I do this in Visual Studio 2010? (I know the code for adding the numbers.. I don't know how to prepare the files) @Armen Tsirunyan I did just that, then I added the following code to the c++ file:- #include <iostream.h> main() { int intege...

Specify Array from Command Line Argument

I realize that each argument passed from the command line is stored as a string in 'char *argv[]' I'd like to pass $ progname array[500] and pull the string from argv[1] subsequently specifying an array based on what I read in. Any ideas? ...

How do compilers optimize our code?

Hi, I ran into this question when i was answering another guys question. How do compilers optimize the code? Can keywords like const, ... help? Beside the fact with volatiles and inline functions and how to optimize the code all by your self! ...

C++ String Array, Loading lines of text from file

I have a problem. When I try to load a file into a string array nothing shows. Firstly I have a file that on one line has a username and on the second has a password. I haven't finished the code but when I try to display what is in the array nothing displays. I would love for this to work. Any suggestions? users.txt user1 password use...

combine two list into one list

I am trying to solve this problem: http://stackoverflow.com/questions/268079/search-multiple-list-for-missing-entries. I used a multimap for duplication of keys. Here is my code: #include <iostream> #include <map> #include <list> #include <utility> using namespace std; int main(){ list<char>a; list<int> b; multimap<char,int...

What's the difference between new char[10] and new char(10)

In C++, what's the difference between char *a = new char[10]; and char *a = new char(10); Thanks! ...

How to call Java methods from C++ in JNI

So I'm writing an Android app which uses a large c++ library. I have everything working so that the java app can call the c++ delegation methods, but I'm finding myself wishing I could log messages from c++ to the Android log. This is easy from java, but I'm at a loss as to how to call a java method from c++. My searches found methods...

Platform Invoke, bool, and string

Hi. suppose a dll contains the following functions extern "C" __declspec(dllexport) void f(bool x) { //do something } extern "C" __declspec(dllexport) const char* g() { //do something else } My first naive approach to use these functions from C# was as follows: [DllImport("MyDll.dll")] internal static extern void f(bool x); [Dl...

thread contention on dynamic memory allocation

Hi All I have just learnt that in C language malloc function comes with the issue of thread contention when used in a multi-threaded applications. In C++ does operator new suffer from the same problem? If yes what tecnhique can I use to avoid this that sounds like a big penalty in the application performance? Thaks AFG ...

Opening a connection on Android and using it via the NDK

Hey all, this is briefly what im trying to do: Im making an Android application that uses C++ libraries compiled from the Android NDK. Some of these C++ libraries create connections and act as servers to report data over a wifi connect (i am not trying to use the phones 3g connection or anything similar). The connection will be created u...

Using memcpy to copy a range of elements from an array

Say we have two arrays: double *matrix=new double[100]; double *array=new double[10]; And we want to copy 10 elements from matrix[80:89] to array using memcpy. Any quick solutions? Thanks ...