I am updating my gcc 4.4 to gCC 4.7, I will do this to use 4.7.
My problem is when I use unique_ptr. I wrote this code
#include <iostream>
#include <memory>
#include <CL/cl.h>
using namespace std;
/*
*
*/
int main(int argc, char** argv) {
std::unique_ptr<cl_platform_id[]>yt;
yt = std::unique_ptr<icl_platform_id[]> (new cl_platform_id [3]);
/* yt.get()[0] = ...... */ this is error no member found
return 0;
}
but I want to use yt member such as uique_ptr::get() and the only function that I get is operator*, so what is the problem?
Edited:
here is my problem:
http://image-load.biz/?di=6FBY
That would be a problem with your IDE's member suggestions. std::unique_ptr certainly does have a member pointer get() const noexcept;, in both the default template and the partial specialisation for arrays.
If you type get() yourself, then the compiler should be happy. Unfortunately, I've never used that IDE, so I can't help you fix its autocompleter.
Whether you should be calling get() is another question; you can access the array elements as yt[0] etc. get() is only for those rare occasions when you actually need a raw pointer.
You want something like:
std::unique_ptr<cl_platform_id[]> yt ( new cl_platform_id[3] );
yt[0].some_member();
Related
I am trying to map a string to a function. The function should get a const char* passed in. I am wondering why I keep getting the error that
*no match for call to ‘(boost::_bi::bind_t<boost::_bi::unspecified, void (*)(const char*), boost::_bi::list0>) (const char*)’*
My code is below
#include <map>
#include <string>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/function.hpp>
typedef boost::function<void(const char*)> fun_t;
typedef std::map<std::string, fun_t> funs_t;
void $A(const char *msg)
{
std::cout<<"hello $A";
}
int main(int argc, char **argv)
{
std::string p = "hello";
funs_t f;
f["$A"] = boost::bind($A);
f["$A"](p.c_str());
return 0;
}
In your example, using boost::bind is completely superfluous. You can just assign the function itself (it will converted to a pointer to a function, and be type erased by boost::function just fine).
Since you do bind, it's not enough to just pass the function. You need to give boost::bind the argument when binding, or specify a placeholder if you want to have the bound object forward something to your function. You can see it in the error message, that's what boost::_bi::list0 is there for.
So to resolve it:
f["$A"] = boost::bind($A, _1);
Or the simpler
f["$A"] = $A;
Also, as I noted to you in the comment, I suggest you avoid identifiers which are not standard. A $ isn't a valid token in an identifier according to the C++ standard. Some implementations may support it, but not all are required to.
I'm trying to implement a thread-safe random number generator using an answer to a different post on this site. Xcode gives me a compile error in some system-provided source code. I've cut all the cruft out and this is the minimum code that will reproduce the error on the latest up-to-date Xcode.
#include <random>
#include <climits>
using namespace std;
mt19937 * _generator = NULL;
template <typename T> T ts_rand(void)
{
uniform_int_distribution<T> distribution(0, INT_MAX);
static bool fInited = false;
if (!fInited)
{
_generator = new mt19937();
fInited = true;
}
return distribution(_generator);
}
int main(int argc, const char * argv[])
{
int random_number = ts_rand<int>();
return random_number;
}
When I try to compile it, I get an error in the file 'algorithm', lines 2843 and 2865: "Semantic issue Type 'std::__1::mersenne_twister_engine * cannot be used prior to '::' because it has no members".
If I change _generator to be an actual instance instead of a pointer, it compiles fine. So that makes me think that there is something about this use of a templating that I don't understand, rather than an error in a system-provided file. FWIW, this same construct compiles and runs fine on VS 2013.
StackOverflow's suggested similar questions would seem to indicate that this might be related to VS' generous interpretations of incompletely-defined template classes, but I'm at a loss.
Any tips appreciated.
Your _generator is a pointer, but uniform_int_distribution::operator() expects a reference to a UniformRandomNumberGenerator.
You just need to do:
return distribution(*_generator);
I am trying to include a function from another file inside a "main" file. I'm following this paradigm:
http://www.learncpp.com/cpp-tutorial/18-programs-with-multiple-files/
Here is my main file, digispark.cpp:
#include <iostream>
using namespace std;
int send(int argc, char **argv);
int main()
{
char* on;
*on = '1';
char* off;
*off = '0';
send(1,&on);
return 0;
}
And here is my send.cpp:
#include <stdio.h>
#include <iostream>
#include <string.h>
#if defined WIN
#include <lusb0_usb.h> // this is libusb, see http://libusb.sourceforge.net/
#else
#include <usb.h> // this is libusb, see http://libusb.sourceforge.net/
#endif
// I've simplified the contents of send for my debugging and your aid, but the
// complicated arguments are a part of the function that will eventually need
// to be here.
int send (int argc, char **argv)
{
std::cout << "Hello";
return 0;
}
I'm compiling on Ubuntu 12.10 using the g++ compiler like so:
g++ digispark.cpp send.cpp -o digispark
It compiles successfully.
However, when I run the program, "Hello" does not come up. Therefore I don't believe the function is being called at all. What am I doing wrong? Any help would be great! Thanks!
EDIT:
How I dealt with the issue:
int send(int argc, char **argv);
int main()
{
char* on[4];
on[0] = (char*)"send";
on[1] = (char*)"1";
char* off[4];
off[0] = (char*)"send";
off[1] = (char*)"0";
send(2,on);
return 0;
}
For those of you who were confused as to why I insisted doing this, as I said before, the send function was already built to accept the char** argv (or char* argv[]). My point was to try to mimic that in my main function.
It would have been much more difficult to rewrite the function that actually goes in the send function to take a different type of argument than just to send in what it wanted. Thanks everyone!
So if this helps anyone trying something similar feel free to use it!
Your problem is not the one you think it is. It's here:
char* on;
*on = '1';
You declared a char pointer, but did not initialize it. Then you dereferenced it. Bang, you're dead. This is what is known as Undefined Behavior. Once you invoke U.B., anything can happen. If you're lucky, it's a crash. But I guess you weren't lucky this time.
Look, if you want to start storing things in memory, you have to allocate that memory first. The best way, as hetepeperfan said, is to just use std::string and let that class take care of all the allocating/deallocating for you. But if for some reason you think you have to use C-style strings and pointers, then try this:
char on[128]; //or however much room you think you'll need. Don't know? Maybe you shoulda used std::string ...
*on = '1';
*(on+1) = '\0'; //if you're using C-strings, better null terminate.
char off[128];
*off = '0';
*(off+1) = '\0';
send(1,&on);
ok I think you try to do something like the following, I tried to make it a bit more in the Style of C++ and prevent the use of pointers since they should not be necessary in the code that you showed.
digispark.cpp
#include "send.h"
int main (int argc, char** argv){
string on = "1";
string off = "0";
send ( on );
send ( off );
return 0;
}
send.cpp
#include <iostream>
#include <string>
void send( const std::string& s) {
std::cout << s << std::endl;
}
send.h
void send(const std::string& s);
0Dear StackExchange Community,
I have for two hours tried to find the source of the problem but failed completly. Research=google search also did not provide any viable solutions. At least I was able to discover that under VS 6.0 one cannot split the declaration and implementation of a template function between header and .cpp-file.
Perhaps my approach is inherently flawed or it is VS 6.0 that is being particulary obnoxious this time.
Here is the test code I wrote.
#include "stdafx.h"
#include <string>
#include <iostream>
class TestClass{
public:
template<class T> inline bool isNull(T& inObject){
return 0; // edited because of the answer by Joachim Pileborg :)
// initial code was: return (inObject != NULL) ? 0:1;
}
};
using namespace std;
int main(int argc, char* argv[])
{
cout<<TestClass::isNull<string>("test");
return 0;
}
Running this code causes the following error:
fatal error C1001: INTERNER COMPILER- FEHLER
(Compiler-File "msc1.cpp", Row 1794)
Does anybody have an idea what I am doing wrong here?
P.S. : this time i really endevoured to ask the question as precisly as possible also providing a concrete example. Please let me know if there is anything else I should have added.
P.SS: I know that visual studio 6.0 ist pretty old but I am forced to use it at work. Running the same code with the a new compiler (at home) did not cause any errors. This is why I assume that the problem is mainly caused by the whims of VS 6.0.
Thanks in advance for you help !!
JD
Unless you define a custom casting operator that returns a pointer, an object instance can never be equal to NULL.
Apart from facts noted in comments and answers, internal compiler error happens in situation, when there's a bug in the compiler, that prevents it from compiling valid code.
Microsoft usually fixes these bugs in IDE hotfixes or in newer versions of compilers. Try to modify the structure of code such that it does the same thing, but looks differently - it's the only way to avoid the Internal Error problem.
There are several troubles in your code:
I rewrote it this way:
comparing the adress of the reference you're passing (you have edited your question but you wrote inObject==NULL in the body of your function and it couldn't compile either)
using const string& so has to be able to call TestClass::isNull<string>("test");
you must define your function as static if you want to call it the way you do
I'm not sure but the character '<' following your word template looked badly encoded in my IDE, so I replaced it with a commonplace <, it compiled better
it's a way of coding, but prefer using typename than class when defining templates
prefer using true and false instead of 1 and 0 ( you edited your question but you still return 0...)
=>
#include <string>
#include <iostream>
class TestClass{
public:
template<typename T>
static bool isNull(const T& inObject)
{
return (&inObject == NULL) ? true : false;
}
};
using namespace std;
int main(int argc, char* argv[])
{
cout<< TestClass::isNull<string>("test");
return 0;
}
Now it compiles fine.
So I was doing some simple C++ exercises and I noticed an interesting feat. Boiled down to bare metal one could try out compiling the following code:
class nice
{
public:
nice() {}
};
int main()
{
nice n;
return 0;
};
The result is a compilation error that goes something like this:
<file>.cpp: In function ‘int main()’:
<file>.cpp:11: error: expected `;' before ‘n’
<file>.cpp:11: warning: statement is a reference, not call, to function ‘nice’
<file>.cpp:11: warning: statement has no effect
And this was using regular g++ on Max OS X, some of my friends have tried in on Ubuntu as well, yielding the same result.
The feat seems to lie in the word "nice", because refactoring it allows us to compile. Now, I can't find the "nice" in the keyword listings for C++ or C, so I was wondering if anyone here had an idea?
Also, putting
class nice n;
instead of
nice n;
fixes the problem.
P.S. I'm a relative C++ newbie, and come from the ActionScript/.NET/Java/Python world.
Update:
Right, my bad, I also had an
#include <iostream>
at the top, which seems to be the root of the problem, because without it everything works just fine.
Maybe the problem is somehow caused by function nice in libc. It is similar to trying to name your class printf.
using namespace std, by any chance?
Edit:
The standard says that standard headers define all their symbols in namespace std (see 17.4.1.2.4).
A footnote, however, also says that the <.h> variants dump their names into the global namespace - but of course no one should be using these ;)
It is a namespace problem but not with namespace std. The header <iostream> is pulling in <unistd.h>
If you try
class nice
{
public:
nice() {};
};
int main(int argc, char *argv[])
{
nice n;
return 0;
}
there is no problem.
Simply add
#include <unistd.h>
and you will get the "expected ‘;’ before ‘n’" error. Namespace std does not enter the picture.
So the solution is the same as before - put class nice in its own namespace and it will not clash with the global ::nice().
Try this version:
#include <iostream>
namespace test
{
class nice
{
public:
nice() {}
};
};
using namespace std;
int main()
{
test::nice n;
cout << "well I think this works." << endl;
return 0;
}
In this case I've defined my own namespace test. Doing so, I can use whatever class names I like, including functions already defined like printf. The only things I can't re-use are reserved words like int or namespace.
Note: if you say:
using namespace test;
As well and refer to nice alone, you'll get this error:
nice.cpp: In function ‘int main()’:
nice.cpp:18: error: reference to ‘nice’ is ambiguous
/usr/include/unistd.h:593: error: candidates are: int nice(int)
nice.cpp:7: error: class test::nice
Which I think explains nicely what's going on - nice now exists in two namespaces and the compiler can't work out which one you mean.
It works fine for me. Did you try the exact code you posted?
extern "C"
{
#include <unistd.h>
}