newest questions tagged c - Stack Overflow
Can I create a partial template specialization for enumeration types?
I have a function template powered by a set of class template explicit specializations giving syntax like
abc.GetAs<DesiredType>("Name");
I would like to have a specialization for DesiredType when that type is an enumeration, so that the returned type matches the underlying type of the enumeration (or enum class).
Is that possible or do clients just have to specify the underlying type themselves?
Read More »
How to use boost to extract sub string from a given string
I want to use boost to extract sub-string from a given string which start with ABC and finish with ; Please help me in doing using boost
Ex: Input - ABCiamaguy;sldfjsdlkjfABCwhoareyou;
Output: iamaguy and whoareyou stored in different vector or strings.
Read More »
Why was the addition of trailing-return-types necessary in C++11?
I've finally started to read up on c++11 and I fail to understand why trailing-return-types are required.
I came across the following example, which is used to highlight the problem:
template<class Lhs, class Rhs> decltype(lhs+rhs) adding_func(const Lhs &lhs, const Rhs &rhs) {return lhs + rhs;}
The example is illegal, because decltype(lhs+rhs) does not work, since the identifiers lhs and rhs are only valid after the parsing phase.
I guess my question is about the timing of decltype type resolution. If I am not mistaken, the keyword decltype is used to determine the type of an expression at compile-time.
I fail to see a downside to having decltype perform type resolution after all parsing is completed (which would work fine for the above example). I believe this would have been a simpler way to solve the problem...
Instead, the C++11 standard provides trailing-return-types:
template<class Lhs, class Rhs> auto adding_func(const Lhs &lhs, const Rhs &rhs) -> decltype(lhs+rhs) {return lhs + rhs;}
I have no doubt that I am missing something, since I fail to see the other use of trailing-return-types. Where is the flaw in my reasoning?
The trailing-return-types seem like an overly complex solution to me since having decltype type resolution after parsing the full function body would work just as well?
Read More »
Why I'am getting this error?
C:\Users\Victor\Desktop\0.3.r5958\luascript.cpp|9037|error: 'buttonEnter' may be used uninitialized in this function| C:\Users\Victor\Desktop\0.3.r5958\luascript.cpp|9037|error: 'buttonEscape' may be used uninitialized in this function| C:\Users\Victor\Desktop\0.3.r5958\luascript.cpp|9039|error: 'popup' may be used uninitialized in this function| ||=== Build finished: 3 errors, 0 warnings ===|strong text
This are the lines were I get the error:
std::string str, title, message; uint8_t buttonEnter, buttonEscape; std::vector<ModalChoice> buttons, choices; bool popup;
I'm using c++ and trying to compile in Code Blocks
Read More »
Cannot modify height value from QRect
The follow snippet results in my compilation yielding "error: passing 'const QRect' as 'this' argument of 'void QRect::setHeight(int)' discards qualifiers [-fpermissive]".
How can I fix this and also I've noticed that if I were to replace h -= 80; with h--;, the compiler does not complain.
int h = this->geometry().height(); h -= 80; ui->datumTable->geometry().setHeight(h);
Read More »
Accessing Push Notification on Windows Phone 8 from C++
I am working on an application for Windows Phone 8, and checking the push notification interface. I have found in the documentation the different steps to do it, but I have only found that on the .NET documentation how to create a notification channel and obtain the URL to push the notifications (here [1]), but I can't seem to find it how to access from a C++ application (I am checking here, on Windows Phone Runtime API [2])
Is there any way of creating a notification channel (to be able to send notifications to the app) from a C++ application. If not, the only solution is to create a .NET application? Or there is any way of circumvent this?
Thanks
[1] http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.notification(v=vs.105).aspx [2] http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207212(v=vs.105).aspx
Read More »
Error with calling variable from a class
class Vars{ public: char *appData = getenv("AppData"); string dir = strcat(appData, "\\Adam"); };
This is my class
And this is how I'm trying to use it:
void write(string data){ ofstream outfile(Vars.dir + "\\data"); //here's the error outfile << data; outfile.close(); }
This is the error:
main.cpp|88|error: expected ',' or '...' before '.' token|
Is this not the way to call variables from a class? Classname.variable/functionname ?
EDIT: So after making instances of my Vars class, I'm getting these errors:
main.cpp|19|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]| main.cpp|20|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]| main.cpp||In function 'void write(std::string)':| main.cpp|88|error: no matching function for call to 'std::basic_ofstream<char>::open(std::string&)'| main.cpp|88|note: candidate is:| mingw32\4.7.1\include\c++\fstream|702|note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]| mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\fstream|702|note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'| main.cpp||In function 'std::string read(std::string)':| main.cpp|96|error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)'| main.cpp|96|note: candidate is:| mingw32\4.7.1\include\c++\fstream|531|note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]| mingw32\4.7.1\include\c++\fstream|531|note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'| main.cpp||In function 'int read(int)':| main.cpp|107|error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)'| main.cpp|107|note: candidate is:| mingw32\4.7.1\include\c++\fstream|531|note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]| mingw32\4.7.1\include\c++\fstream|531|note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'| main.cpp|108|error: expected ';' before 'if'|
Read More »
using class templates without passing template parameters
// p1: some value // nt: native type template<int p1, typename nt> struct A {}; // pt: pointer to type i.e. int* // tA: a specialization of A template<typename pt, typename tA> struct B1 {}; // pt: pointer to type i.e. int* // tA: a specialization of A template<typename pt, typename tA> struct B2 {}; // tB: specialization of B? // tA: specialization of A template<typename tB, typename tA> struct C {}; // now i want to create a C partial specialization where: // A<some_value, native_type> // B?<char*, A<some_value, native_type> > template< template<typename, typename> class B, int p1, typename nt > struct C< B<char*, A<p1, nt> >, A<p1, nt> > {}; int main() { C< B1, A<10, int> > c; }
When compiling the above code with clang it gives the error:
error: use of class template B1 requires template arguments C< B1, A<10, int> > c; ^~
I understand the error and to fix it B1 should be B1<char*, A<10, int> >. Should the compiler deduct the value of these parameters from the most matched specialization?
Read More »
Converting old makefile to CMake
I'm trying to convert my old makefile code to CMake. Can you help me? This is the part where I'm currently stuck. I don't know how to pass these arguments to the compiler.
COMPILE_FLAGS = -c -m32 -O3 -fPIC -w -DSOMETHING -Wall -I src/sdk/core ifdef STATIC OUTFILE = "bin/test_static.so" COMPILE_FLAGS_2 = ./lib/ABC.a else OUTFILE = "bin/test.so" COMPILE_FLAGS_2 = -L/usr/lib/mysql -lABC endif all: g++ $(COMPILE_FLAGS) src/sdk/*.cpp g++ $(COMPILE_FLAGS) src/*.cpp g++ -fshort-wchar -shared -o $(OUTFILE) *.o $(COMPILE_FLAGS_2) rm -f *.o
Thank you!
Read More »
9 duplicate symbols for architecture? XCode
I just changed the priority queue in my huffman encoding project. I was trying to debug it, so I copied the priority queue that came in the original exercise files back into it. And then changed it from a template priority queue to a *Node priority queue. It's now failing to build, with clang: error: 9 duplicate symbols for architecture. How to fix? I'm eventually going to rewrite the priority queue anyway, but is there an obvious quick fix for this error?
Read More »
Wrapper cannot instantiate an abstarct class
I have created the Wrapper for the class library in the C++ and when I built the wrapper I got the following error as Wrapper cannot instantiate an abstract class.
Where in the Wrapper.cpp file,
IWrapper *IWrapper::CreateInstance() { IWrapper *instance =(IWrapper *)new Wrapper(); return (instance); }
On the Line , IWrapper *instance = (IWrapper *)new Wrapper();
I get the above error.
Please provide the solution and let me know.
Read More »
Umanaged C++ code call from managed C++
I have linked an unmanaged C++ DLL to managed C++ code.
The problem is that one of the functions takes as argument an CStdioFile variable. I could not find a way to marshal this type to an managed type.
How could I solve this issue ?
This is the DLL function prototype :
bool CreateOpenLogFile(CStdioFile* stdioLogFile, CString strShortFileName);
Wainting for your answers. Thanks.
Read More »
Return a rectangle from an rectangle intersection?
Been struggling to come up with a solution to return a rectangle representing the actual intersection between two rectangles.
What I am looking for is much like this: MSDN - Rectangle::Intersect Method (Rectangle, Rectangle)
As the picture shows I would like to have the rectangle colored in green returned from an intersection between two rectangles. Not some boolean value true or false.
I will accept a code sample or just plain theory as an answer. Thanks in advance!
Read More »
Deploying native c++ project for debugging in vs 2012
It seems to be a new feature for visual studio 2012 remote debugging. Whole procedure is described here: http://msdn.microsoft.com/en-us/library/8x6by8d2.aspx
I have small single exe output projects in native c++ used by me as "Guinea pig" to test/play with vs 2012. When I saw "Deployment Directory" option in Debugging/Remote Windows Debugger I thought that this would be helpful to use it, unfortunately I cannot made it to work. I checked all steps from msdn page twice. Debugging itself works fine, but this deploy seems to do nothing. Running "Deploy" from shortcut menu prints:
1>------ Deploy started: Project: KernelSecurity, Configuration: Debug Win32 ------ ========== Build: 0 succeeded, 0 failed, 1 up-to-date, 0 skipped ========== ========== Deploy: 1 succeeded, 0 failed, 0 skipped ==========
No files are in fact copied. I cannot force studio to produce some more info what happened, etc. Made a quick look into Process monitor output, but found nothing interesting there. Any ideas?
Of course there are old known methods like post-build steps or sharing directories, however it would be nice to make use of that new option.
Read More »
C++ zeromq application to subscribe to entire twitter feed
This is a general architecture question for everyone. How would one generally go about subscribing to every event from everyone on twitter. I was thinking generally the following pseudo code but please give input /critiques
1 populate list of all current twitter users, total count U users
2 Spawn N threads and equally balance subscribe to U/N individual users per thread. Each thread creates a PUB zmq socket type
3 A single SUB zmq thread runs that receives all events
Read More »
Problems with compiling a pimpl idiom code
I've been trying to have a go at a 'pimpl' idiom but I just can't get the darned thing to compile.
On Linux Mint with g++ v. 4.6.3 I get the following error:
$ g++ main.cc /tmp/ccXQ9X9O.o: In function `main': main.cc:(.text+0xd7): undefined reference to `Person::Person(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)' collect2: ld returned 1 exit status
This is my code:
person.hh
#ifndef PERSON_HH #define PERSON_HH #include <tr1/memory> class Person { private: class PersonImpl; std::tr1::shared_ptr<PersonImpl> pImpl; public: Person(const std::string& name, int age=0); const std::string& get_name() const; int get_age() const; };
Can I create a partial template specialization for enumeration types?
I have a function template powered by a set of class template explicit specializations giving syntax like
abc.GetAs<DesiredType>("Name");
I would like to have a specialization for DesiredType when that type is an enumeration, so that the returned type matches the underlying type of the enumeration (or enum class).
Is that possible or do clients just have to specify the underlying type themselves?
Read More »
How to use boost to extract sub string from a given string
I want to use boost to extract sub-string from a given string which start with ABC and finish with ; Please help me in doing using boost
Ex: Input - ABCiamaguy;sldfjsdlkjfABCwhoareyou;
Output: iamaguy and whoareyou stored in different vector or strings.
Read More »
Why was the addition of trailing-return-types necessary in C++11?
I've finally started to read up on c++11 and I fail to understand why trailing-return-types are required.
I came across the following example, which is used to highlight the problem:
template<class Lhs, class Rhs> decltype(lhs+rhs) adding_func(const Lhs &lhs, const Rhs &rhs) {return lhs + rhs;}
The example is illegal, because decltype(lhs+rhs) does not work, since the identifiers lhs and rhs are only valid after the parsing phase.
I guess my question is about the timing of decltype type resolution. If I am not mistaken, the keyword decltype is used to determine the type of an expression at compile-time.
I fail to see a downside to having decltype perform type resolution after all parsing is completed (which would work fine for the above example). I believe this would have been a simpler way to solve the problem...
Instead, the C++11 standard provides trailing-return-types:
template<class Lhs, class Rhs> auto adding_func(const Lhs &lhs, const Rhs &rhs) -> decltype(lhs+rhs) {return lhs + rhs;}
I have no doubt that I am missing something, since I fail to see the other use of trailing-return-types. Where is the flaw in my reasoning?
The trailing-return-types seem like an overly complex solution to me since having decltype type resolution after parsing the full function body would work just as well?
Read More »
Why I'am getting this error?
C:\Users\Victor\Desktop\0.3.r5958\luascript.cpp|9037|error: 'buttonEnter' may be used uninitialized in this function| C:\Users\Victor\Desktop\0.3.r5958\luascript.cpp|9037|error: 'buttonEscape' may be used uninitialized in this function| C:\Users\Victor\Desktop\0.3.r5958\luascript.cpp|9039|error: 'popup' may be used uninitialized in this function| ||=== Build finished: 3 errors, 0 warnings ===|strong text
This are the lines were I get the error:
std::string str, title, message; uint8_t buttonEnter, buttonEscape; std::vector<ModalChoice> buttons, choices; bool popup;
I'm using c++ and trying to compile in Code Blocks
Read More »
Cannot modify height value from QRect
The follow snippet results in my compilation yielding "error: passing 'const QRect' as 'this' argument of 'void QRect::setHeight(int)' discards qualifiers [-fpermissive]".
How can I fix this and also I've noticed that if I were to replace h -= 80; with h--;, the compiler does not complain.
int h = this->geometry().height(); h -= 80; ui->datumTable->geometry().setHeight(h);
Read More »
Accessing Push Notification on Windows Phone 8 from C++
I am working on an application for Windows Phone 8, and checking the push notification interface. I have found in the documentation the different steps to do it, but I have only found that on the .NET documentation how to create a notification channel and obtain the URL to push the notifications (here [1]), but I can't seem to find it how to access from a C++ application (I am checking here, on Windows Phone Runtime API [2])
Is there any way of creating a notification channel (to be able to send notifications to the app) from a C++ application. If not, the only solution is to create a .NET application? Or there is any way of circumvent this?
Thanks
[1] http://msdn.microsoft.com/en-us/library/windowsphone/develop/microsoft.phone.notification(v=vs.105).aspx [2] http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207212(v=vs.105).aspx
Read More »
Error with calling variable from a class
class Vars{ public: char *appData = getenv("AppData"); string dir = strcat(appData, "\\Adam"); };
This is my class
And this is how I'm trying to use it:
void write(string data){ ofstream outfile(Vars.dir + "\\data"); //here's the error outfile << data; outfile.close(); }
This is the error:
main.cpp|88|error: expected ',' or '...' before '.' token|
Is this not the way to call variables from a class? Classname.variable/functionname ?
EDIT: So after making instances of my Vars class, I'm getting these errors:
main.cpp|19|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]| main.cpp|20|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]| main.cpp||In function 'void write(std::string)':| main.cpp|88|error: no matching function for call to 'std::basic_ofstream<char>::open(std::string&)'| main.cpp|88|note: candidate is:| mingw32\4.7.1\include\c++\fstream|702|note: void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]| mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\fstream|702|note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'| main.cpp||In function 'std::string read(std::string)':| main.cpp|96|error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)'| main.cpp|96|note: candidate is:| mingw32\4.7.1\include\c++\fstream|531|note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]| mingw32\4.7.1\include\c++\fstream|531|note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'| main.cpp||In function 'int read(int)':| main.cpp|107|error: no matching function for call to 'std::basic_ifstream<char>::open(std::string&)'| main.cpp|107|note: candidate is:| mingw32\4.7.1\include\c++\fstream|531|note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char; _Traits = std::char_traits<char>; std::ios_base::openmode = std::_Ios_Openmode]| mingw32\4.7.1\include\c++\fstream|531|note: no known conversion for argument 1 from 'std::string {aka std::basic_string<char>}' to 'const char*'| main.cpp|108|error: expected ';' before 'if'|
Read More »
using class templates without passing template parameters
// p1: some value // nt: native type template<int p1, typename nt> struct A {}; // pt: pointer to type i.e. int* // tA: a specialization of A template<typename pt, typename tA> struct B1 {}; // pt: pointer to type i.e. int* // tA: a specialization of A template<typename pt, typename tA> struct B2 {}; // tB: specialization of B? // tA: specialization of A template<typename tB, typename tA> struct C {}; // now i want to create a C partial specialization where: // A<some_value, native_type> // B?<char*, A<some_value, native_type> > template< template<typename, typename> class B, int p1, typename nt > struct C< B<char*, A<p1, nt> >, A<p1, nt> > {}; int main() { C< B1, A<10, int> > c; }
When compiling the above code with clang it gives the error:
error: use of class template B1 requires template arguments C< B1, A<10, int> > c; ^~
I understand the error and to fix it B1 should be B1<char*, A<10, int> >. Should the compiler deduct the value of these parameters from the most matched specialization?
Read More »
Converting old makefile to CMake
I'm trying to convert my old makefile code to CMake. Can you help me? This is the part where I'm currently stuck. I don't know how to pass these arguments to the compiler.
COMPILE_FLAGS = -c -m32 -O3 -fPIC -w -DSOMETHING -Wall -I src/sdk/core ifdef STATIC OUTFILE = "bin/test_static.so" COMPILE_FLAGS_2 = ./lib/ABC.a else OUTFILE = "bin/test.so" COMPILE_FLAGS_2 = -L/usr/lib/mysql -lABC endif all: g++ $(COMPILE_FLAGS) src/sdk/*.cpp g++ $(COMPILE_FLAGS) src/*.cpp g++ -fshort-wchar -shared -o $(OUTFILE) *.o $(COMPILE_FLAGS_2) rm -f *.o
Thank you!
Read More »
9 duplicate symbols for architecture? XCode
I just changed the priority queue in my huffman encoding project. I was trying to debug it, so I copied the priority queue that came in the original exercise files back into it. And then changed it from a template priority queue to a *Node priority queue. It's now failing to build, with clang: error: 9 duplicate symbols for architecture. How to fix? I'm eventually going to rewrite the priority queue anyway, but is there an obvious quick fix for this error?
Read More »
Wrapper cannot instantiate an abstarct class
I have created the Wrapper for the class library in the C++ and when I built the wrapper I got the following error as Wrapper cannot instantiate an abstract class.
Where in the Wrapper.cpp file,
IWrapper *IWrapper::CreateInstance() { IWrapper *instance =(IWrapper *)new Wrapper(); return (instance); }
On the Line , IWrapper *instance = (IWrapper *)new Wrapper();
I get the above error.
Please provide the solution and let me know.
Read More »
Umanaged C++ code call from managed C++
I have linked an unmanaged C++ DLL to managed C++ code.
The problem is that one of the functions takes as argument an CStdioFile variable. I could not find a way to marshal this type to an managed type.
How could I solve this issue ?
This is the DLL function prototype :
bool CreateOpenLogFile(CStdioFile* stdioLogFile, CString strShortFileName);
Wainting for your answers. Thanks.
Read More »
Return a rectangle from an rectangle intersection?
Been struggling to come up with a solution to return a rectangle representing the actual intersection between two rectangles.
What I am looking for is much like this: MSDN - Rectangle::Intersect Method (Rectangle, Rectangle)
As the picture shows I would like to have the rectangle colored in green returned from an intersection between two rectangles. Not some boolean value true or false.
I will accept a code sample or just plain theory as an answer. Thanks in advance!
Read More »
Deploying native c++ project for debugging in vs 2012
It seems to be a new feature for visual studio 2012 remote debugging. Whole procedure is described here: http://msdn.microsoft.com/en-us/library/8x6by8d2.aspx
I have small single exe output projects in native c++ used by me as "Guinea pig" to test/play with vs 2012. When I saw "Deployment Directory" option in Debugging/Remote Windows Debugger I thought that this would be helpful to use it, unfortunately I cannot made it to work. I checked all steps from msdn page twice. Debugging itself works fine, but this deploy seems to do nothing. Running "Deploy" from shortcut menu prints:
1>------ Deploy started: Project: KernelSecurity, Configuration: Debug Win32 ------ ========== Build: 0 succeeded, 0 failed, 1 up-to-date, 0 skipped ========== ========== Deploy: 1 succeeded, 0 failed, 0 skipped ==========
No files are in fact copied. I cannot force studio to produce some more info what happened, etc. Made a quick look into Process monitor output, but found nothing interesting there. Any ideas?
Of course there are old known methods like post-build steps or sharing directories, however it would be nice to make use of that new option.
Read More »
C++ zeromq application to subscribe to entire twitter feed
This is a general architecture question for everyone. How would one generally go about subscribing to every event from everyone on twitter. I was thinking generally the following pseudo code but please give input /critiques
1 populate list of all current twitter users, total count U users
2 Spawn N threads and equally balance subscribe to U/N individual users per thread. Each thread creates a PUB zmq socket type
3 A single SUB zmq thread runs that receives all events
Read More »
Problems with compiling a pimpl idiom code
I've been trying to have a go at a 'pimpl' idiom but I just can't get the darned thing to compile.
On Linux Mint with g++ v. 4.6.3 I get the following error:
$ g++ main.cc /tmp/ccXQ9X9O.o: In function `main': main.cc:(.text+0xd7): undefined reference to `Person::Person(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)' collect2: ld returned 1 exit status
This is my code:
person.hh
#ifndef PERSON_HH #define PERSON_HH #include <tr1/memory> class Person { private: class PersonImpl; std::tr1::shared_ptr<PersonImpl> pImpl; public: Person(const std::string& name, int age=0); const std::string& get_name() const; int get_age() const; };
No comments:
Post a Comment