|
Hi,
when im trying...
void function(int y,int w)
{
printf("int function");
}
void function(float y,float w)
{
printf("float function");
}
int main()
{
function(1.2,2.2);
return 0;
}
im getting error like..
error C2668: 'function' : ambiguous call to overloaded function
and when im trying to call function(1.2,2) or function(1,2.2) it is printing as "int function"
Please clarify..when the function(float y,float w) will be called...
|
|
|
|
|
kumar sanghvi wrote: void function(int y,int w)
{
printf("int function");
}
void function(float y,float w)
{
printf("float function");
}
The problem is that the compiler is not going to understand which overload of this function are you wanting to call (1.2 can be truncated to integer, therefore the confusion). You will need to be more explicit.
kumar sanghvi wrote: function(1.2,2.2);
Try function((float)1.2, float(2.2)); instead. To make things clearer:
int main()
{
float f1=1.2, f2=2.2;
int n1=22, n2=23;
function(f1, f2);
function(n1, n2);
function((int)f1, (int)f2);
function((float)n2, (float)n1);
return 0;
}
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Look at the error message from gcc:
a.cpp:16: error: call of overloaded ‘function(double, double)’ is ambiguous
a.cpp:3: note: candidates are: void function(int, int)
a.cpp:9: note: void function(float, float)
A call to either function would require truncation, which is why neither is preferred over the other. I suspect you really want void function(double y,double w) . Remember that in C/C++, the default floating-point type for literals and parameter passing is double, NOT float.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Hi,
if i have a code like...
int* p;
int w=10;
p=&10;
the memory allocation for both w and p will be on stack only ...?
Please correct me if im wrong as im a newbie to c++..
and please let me know when it is really useful to allocate memory dynamically using pointer than allocating on the stack..?
|
|
|
|
|
kumar sanghvi wrote:
int *p;
int w=10;
p=&w;
the memory allocation for both w and p will be on stack only ...?
Yes, in this case, the pointer is pointing to a memory location, which is on the stack.
kumar sanghvi wrote: when it is really useful to allocate memory dynamically using pointer than allocating on the stack..?
Pointers are more useful when you need to allocate memory dynamically (typically you don't know how much memory you are going to need).
It is a crappy thing, but it's life -^ Carlo Pallini
modified on Sunday, August 23, 2009 2:11 PM
|
|
|
|
|
Thanks for ur Reply..
if possible can u explain with example when the dynamic memory allocation is really useful...
|
|
|
|
|
kumar sanghvi wrote: if possible can u explain with example when the dynamic memory allocation is really useful...
Any case where you won't know (at compile time) the amount of memory that would be needed to store data is an ideal example. If you know how many bytes would be needed at compile time, you can put it on the stack. However, if this is unknown, you could use the heap to dynamically allocate memory.
An example could be data arriving on a socket. You would know that data would arrive, but you won't know the exact 'amount' (as in 'N' bytes) at compile time. So, you allocate memory at run time (using new , malloc , or a C++ class which would do the allocation for you, etc.,).
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
|
Post fixed. Thanks, I hadn't bothered looking into each line and had assumed that the OP should have compiled. Have your points.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
My previous reply to you is modified, please look at it again. Also, stop "typing" code into browser and always copy and paste.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
kumar sanghvi wrote:
int* p;
int w=10;
p=&10;
p=&w would do, assuming p is used: give (if it didn't have one yet) w a place on the stack (previously it would probably reside in a register), and set p to that address (eg lea esi, dword ptr[esp+24] ), but if the operations on p are simple enough (for the specific implementation, not every theoretically simple enough sequence of operations) to evaluate at compile time it may be optimized to something weird and/or scary.
But it's fine to think of them as being on the stack, the compiler will guarantee that this assumption is true whenever you make it (and if you don't make it, it doesn't matter). They're definitely not on the heap, if that's what you were wondering about.
notes: many simplifications and omissions were made to avoid writing a king-sized post, if you really want to know every little detail you could ask the same question on comp.lang.c++[^], they will be happy to tell you every pedantic little detail. And I don't guarantee that anything in this post was actually true, use the information at your own risk (etc etc)
|
|
|
|
|
harold aptroot wrote: And I don't guarantee that anything in this post was actually true, use the information at your own risk
You are actually right, I had made a mistake while correcting it as well (completely stressed out?). Have another vote from me.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
Thanks, but hey mistakes happen to everyone (except JSOP and Chuck Norris of course)
|
|
|
|
|
Hi
I am using a C lib and its corresponding header file in my C++ class library project. After a lot of linkage errors I could finally build the project. I want to use this C++ dll in a C# application. There is no debug error, but there is a runtime error like this:
The specified module could not be found. HRESULT: 0x8007007E
Could anyone please help me?
Thanks in advance
|
|
|
|
|
You probably also need a DLL aside of the lib which either cannot be found or it requires registration but was not registered yet. If you have the DLL, try regsvr32.exe the.dll . If there is no DLL, you could try checking around the site you got the lib from if there is one, or you could also try searching for ".dll" inside the lib, maybe you can find the name of it and then you can search for it online.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
Your Dll might require some other dlls or it's location is not correct.You would better check it's dependencies with Dependencywalker.
Life is a stage and we are all actors!
|
|
|
|
|
Thanks. I use dependency walker and I found it. There were 2 dlls missing:
msvcr80.dll
msvcrd80.dll
I copied them in the exe folder and the problem was solved. But I am surprised that once I ran the program, a folder was built in WINDOWS folder and the dlls were copied there. So there is no need to those dlls in the exe folder. I mean I delete the dlls in the exe folder and the program still works fine. Why this happens and what should I do when I want to release the application?
Thanks
|
|
|
|
|
msvcr80.dll and msvcrd80.dll are parts of VC++ 2005 redist.When the redist is installed on the client computer it's being used by all applications ,so into this case you needn't private dlls for your app.But it's not you should bring some parts of it with your app.It's recommended also to create app manifest in order to add support of OS themes.
See here[^] for details.
Life is a stage and we are all actors!
|
|
|
|
|
Hello,
So i am sure that this is a commonly understood thing, but me and my friend have a bad feeling that there is a reason NOT to do the following.
Example:
a class is defined like this:
class Lock {
public:
Lock (Mutex& m) : mutex (m) {...use mutex...}
...
private:
Mutex& mutex;
}
then we were thinking if it was safe to create a lock like this:
...
Lock l (Mutex m);
...
correct me if I am wrong, but then m is scoped with l. This works if I only want m to exist for l.
It is similar to doing:
Mutex m;
Lock l (m);
but in this case I have a ref to m and i have to make sure they are scoped together.
Is there any taboo no-nos to doing the prior method? Some pitfalls I am not seeing?
|
|
|
|
|
thelonesquirrely wrote: then we were thinking if it was safe to create a lock like this:
...
Lock l (Mutex m);
...
correct me if I am wrong, but then m is scoped with l. This works if I only want m to exist for l.
No, that's incorrect. You're declaring a function, l, which takes a Mutex parameter called m and returning a lock. You cannot declare and define a variable in a function call. To demonstrate this, run this code:
#include <iostream>
class Mutex
{
public:
Mutex() { std::cout << "Mutex::Mutex\n"; }
~Mutex() { std::cout << "Mutex::~Mutex\n"; }
};
class Lock
{
public:
Lock(Mutex& m) : m_(m) { std::cout << "Lock::Lock\n"; }
~Lock() { std::cout << "Lock::~Lock\n"; }
private:
Mutex& m_;
};
int main()
{
Lock l(Mutex m);
}
You'll get no output, because no objects are being created or destroyed!
thelonesquirrely wrote: Is there any taboo no-nos to doing the prior method? Some pitfalls I am not seeing?
The main pitfall is that you never define a Lock variable, so you're not going to get any locking behaviour.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
How to join search two data tables in different database by ado?
Thanks!
|
|
|
|
|
By using database.owner.table notation.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
First,thank you,my friend!
However,how many _connection objects and _recordset objects should I use?
With great pleasure if you can show me some simple C++ code!
|
|
|
|
|
jiabin_007_love wrote: However,how many _connection objects and _recordset objects should I use?
One of each should suffice.
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
DavidCrow wrote: One of each should suffice.
However,a _recordset object can only correspond to one _connection object, and one _connection object can only link one database.
I need operate two data table in different database in one sql operator.
|
|
|
|