Solved

An error C2664: related problem, concerning (__cdecl *)

Posted on 2004-09-07
36
948 Views
Last Modified: 2013-11-20
I essentially get this error when i try the following:

This is in my code. For those familiar with GAlib, this should be instantly recognizable (dimensionSize in this case gives the number of genes the genome will have, while the objectiveFunction is the function it will use to calculate its fitness

GA1DArrayGenome<float> genome(dimensionSize, objectiveFunction);

That line however generates the following error:

error C2664:

'__thiscall GA1DArrayGenome<float>::GA1DArrayGenome<float>(unsigned int,float (__cdecl *)(class GAGenome &),void *)' : cannot convert parameter
 2 from 'float (class GAGenome &)' to 'float (__cdecl *)(class GAGenome &)'



And for reference, this is the default constructor from the GA1DArrayGenome.cpp class:


template <class T>
GA1DArrayGenome<T>::
GA1DArrayGenome(unsigned int length, GAGenome::Evaluator f, void * u) :
GAArray<T>(length),
GAGenome(DEFAULT_1DARRAY_INITIALIZER,
       DEFAULT_1DARRAY_MUTATOR,
       DEFAULT_1DARRAY_COMPARATOR) {
  evaluator(f);
  userData(u);
  nx=minX=maxX=length;
  crossover(DEFAULT_1DARRAY_CROSSOVER);
}


ONE LAST PIECE of information. My line worked jsut fine when I had used a console for VC++. I tried now using it in a proper Win Application (a dialog) and I get that error. Any help will be greatly appreciated, but please, try to be specific and as elaborate as possible... thanks.
0
Comment
Question by:Gene_Cyp
  • 19
  • 9
  • 6
  • +1
36 Comments
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
I personally think, it has something to do with the difference of calling this from a main() function and from any other function.

In the console, I call it from main(), whereas in the dialog, there is no main and besides I call it when the user takes a certain action (eg press button "mybutton")

(just trying to steer whoever is looking to help, hopefully to the right direction)
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
OK.. I feel like I am talking to myself.. :)

I think THIS is essentially the issue:

Function Pointers To Class Function Members

Unfortunately, I am using GAlib, so I have to use what's available. The only thing elft for me to do, is figure out what I need to do in my declaration, so that the conflict seizes to exist...
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Project - settings.
C/C++ tab
Category: Code generation

what is calling convention set to?  (if it isn't __cdecl* try changing it to that and recompiling)
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
It is already setup as __cdecl*

0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Check the settings for the console app.  How was it set there?
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
Same settings and all the Project Options are also identical:

/nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Fp"Debug/SWGRGA.pch" /Yu"stdafx.h"
/nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /Fp"Debug/TestGALibWin.pch" /Yu"stdafx.h"


In the console, I call it in the main() method, in the windows one, I call it through a button (event) method.

Both were made using the MFC wizard generation. I use VC++ 6

Here are the relevant parts of my code:


      GA1DArrayGenome<float> genome(dimensionSize, objectiveFunction);
      genome.initializer(CTestGALibWinDlg::Initializer);
      // Sharing Scaling
      if(selectedScaling = 2)
            genome.comparator(CTestGALibWinDlg::Comparator);


Here's the full error report:


C:\Program Files\Microsoft Visual Studio\MyProjects\TestGALibWin\TestGALibWinDlg.cpp(1076) : error C2664: '__thiscall GA1DArrayGenome<float>::GA1DArrayGenome<float>(unsigned int,float (__cdecl *)(class GAGenome &),void *)' : cannot convert parameter
 2 from 'float (class GAGenome &)' to 'float (__cdecl *)(class GAGenome &)'
        None of the functions with this name in scope match the target type
C:\Program Files\Microsoft Visual Studio\MyProjects\TestGALibWin\TestGALibWinDlg.cpp(1077) : error C2664: 'void (__cdecl *__thiscall GAGenome::initializer(void (__cdecl *)(class GAGenome &)))(class GAGenome &)' : cannot convert parameter 1 from 'voi
d (class GAGenome &)' to 'void (__cdecl *)(class GAGenome &)'
        None of the functions with this name in scope match the target type
C:\Program Files\Microsoft Visual Studio\MyProjects\TestGALibWin\TestGALibWinDlg.cpp(1080) : error C2664: 'float (__cdecl *__thiscall GAGenome::comparator(float (__cdecl *)(const class GAGenome &,const class GAGenome &)))(const class GAGenome &,cons
t class GAGenome &)' : cannot convert parameter 1 from 'float (const class GAGenome &,const class GAGenome &)' to 'float (__cdecl *)(const class GAGenome &,const class GAGenome &)'
        None of the functions with this name in scope match the target type
C:\Program Files\Microsoft Visual Studio\MyProjects\TestGALibWin\TestGALibWinDlg.cpp(1114) : error C2664: '__thiscall GASharing::GASharing(float (__cdecl *)(const class GAGenome &,const class GAGenome &),float,float)' : cannot convert parameter 1 fr
om 'float (const class GAGenome &,const class GAGenome &)' to 'float (__cdecl *)(const class GAGenome &,const class GAGenome &)'
        None of the functions with this name in scope match the target type
C:\Program Files\Microsoft Visual Studio\MyProjects\TestGALibWin\TestGALibWinDlg.cpp(1161) : error C2664: '__thiscall GASharing::GASharing(float (__cdecl *)(const class GAGenome &,const class GAGenome &),float,float)' : cannot convert parameter 1 fr
om 'float (const class GAGenome &,const class GAGenome &)' to 'float (__cdecl *)(const class GAGenome &,const class GAGenome &)'
        None of the functions with this name in scope match the target type
Generating Code...
Error executing cl.exe.
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
This (__cdecl *) makes me think it is something to do with compile settings but if they are the same as with one that works then I don't know what else it could be.
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
Is there a way to remove (__cdecl *) ?

If i change it to one of the other two, __fastcall or __stdcall, then the error is the same (just replace  (__cdecl *) with the respective change.


Any ideas could really help. I have been looking into Functors and Function Pointers To Class Function Members for the last 24 hours... :(
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Is there a way to remove (__cdecl *) ?

That is an instruction to the compiler to use a particular method of handling functions (and parameters).  You have tried all the alternatives without success.


What puzzles me is the console app compiling without a problem but this dialog based one failing.

(It is the same function call in your console based app I take it)
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
Identical... as I said, the only difference is that, one of them is called through the main() function (console), whereas the windows one calls it through some other function
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
This is a compile time error, not runtime.

Would it be a lot of work to make another dlg based project and copy the files across?  (rename this original and have the new one with the same name, copy .cpp, .h files and res directory)
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
What would that accomplish? Isn't it better to just delete the objects created by MFC and rebuild all?
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
OK Try a rebuild all first.

<What would that accomplish?>
What that does it create all the support files again (.dsw, .dsp ....) and you then copy your code and resources.
It is a long shot but MAYBE there is a problem in one of these support files that you aren't spotting in the project settings.  (Seriously I am not putting a lot of hope on it but if it only takes you a couple of minutes it could be a couple of minutes well spent).  

This all boils down to the console app working but this new dlg based one not.  My reasoning - what is different - the new project - is that somehow corrupt?
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
There is no reason for it to be corrupt... :(

Other than that, as I said above, there is only one difference between the two and that's the method which calls them...

OK I did a new one. It still generated the exact same mistakes...
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
I'm increasing the points to 200...
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
Increasing to 300...  no-one can help out there!??!?!
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 100 total points
Comment Utility
One more thing comes to mind.
Your console app I assume was a .c file but this dialog based one uses a .cpp file.
If that is the case maybe you could test the compile by having another function where you have this line of code

extern "C" void TestIt();

extern void TestIt()
{
      GA1DArrayGenome<float> genome(dimensionSize, objectiveFunction);
      return;
}

where you currently have this line that fails comment it out and replace by
TestIt();
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
the console was cpp as well, BUT, i thought of the same point -> even though it's a cpp console it 's more of a C one than a cpp one... especially in the way I coded it.

I'll give it a go now.. wouldn't this cause problems further down?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
Hmm, it generates additional errors, relating to extern...
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Different errors or extra errors?
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
Oh - missed a comment.  If the console was a cpp file then it probably isn't worth going further with this extern "C".
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
Extra errors... any suggestions that pop up in your mind, jsut throw them here bud. Even if it turns out that they don't work, they might lead to a solution.
0
 

Expert Comment

by:gfaraj
Comment Utility
Hmm,
try:

GA1DArrayGenome<float> genome(dimensionSize, &objectiveFunction);

(note the & before objectiveFunction)...

Cheers,
George Faraj
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
Hi George, no it didn't work
0
 
LVL 2

Expert Comment

by:aleric
Comment Utility
What is the declaration look like of objectiveFunction?  You never showed the part of the code.
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
float myClass::objectiveFunction(GAGenome& g)
{

/*code*/
return "genome's value"
}


It's a way of "evaluating" the genome
0
 
LVL 2

Expert Comment

by:aleric
Comment Utility
So, what happens if you change that into:

 float __cdecl myClass::objectiveFunction(GAGenome& g)

?
0
 
LVL 2

Expert Comment

by:aleric
Comment Utility
By the way, the _declaration_ is != the definition.  What is the declaration look like? (In the class definition thus).
I hope it is:

class myClass {

...
  static float __cdecl objectiveFunction(GAGenome& g);
};

?

Note the static.
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
no, it's not a static, besides, from what i read, u CAN have a pointer point to non-static C++ member functions
0
 
LVL 2

Expert Comment

by:aleric
Comment Utility
You STILL didn't show the declaration.  You also didn't answer what happens if you
add that __cdecl to both the declaration and definition. ......

Also, the only correct way to CALL it is then:

GA1DArrayGenome<float> genome(dimensionSize, &myClass::objectiveFunction);
0
 
LVL 2

Assisted Solution

by:aleric
aleric earned 200 total points
Comment Utility
Isn't GAGenome::Evaluator static then?  Because the error is
cannot convert parameter 2 from 'float (class GAGenome &)' to 'float (__cdecl *)(class GAGenome &)'
The latter is NOT a member function (pointer).  It is a normal function pointer,
therefore you cannot pass a member function pointer here.  Make myClass::objectiveFunction
static and it should work.
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
Hi aleric, I assume you are the mirc channel's OP. If not my apologies. If yes, please read below:

Firstly, as you can see, I'm by far not very experienced with C++. I'm simply wondering when that became a crime... You showed your immaturity by banning me on the mirc channel, just because, my knowledge of programming and in particular.. C++ is minimal.

Secondly, it doesn't matter how well YOU know your stuff, if you CAN'T "transfer" it to someone who doesn't know. It requires patience and behaving in a civilized way. You demonstrated you had neither...


Andy, even though my console was a cpp file. What you said about c and cpp was right on the money. Aleric was right ofcourse. turning the member functions into static solved the problem, but I want to split the points between the two of you.
0
 

Expert Comment

by:gfaraj
Comment Utility
Gene, aleric is not the op in #c++, he is the one that had a nickname of Run...
0
 
LVL 2

Expert Comment

by:aleric
Comment Utility
Thanks for the early clarification gfarai.

Gene, as gfarai said - no I am not the one who banned you.  I don't even have ops on that channel.
I have seen like 4 kick/bans on that channel today that I disagree with.  In the first two cases
I''ve publically challenged the op - stating that the ban was 'uncalled for'.  But I just know that
if I keep doing that all that will happen is that I will be banned myself. (The only reason I wasn't
in the first two cases is probably because I am an IRC Operator on that network :/).

Unfortunately for you, IRC is anarchistic.  A channel op has the "right" to ban whoever they want for
whatever reason they want.  Personally I think it is very sad that a general channel like #C++ has
an operator who kick/bans people for being ignorant; even if the op rightfully feels that someone
has been wasting their time (which is very annoying I agree) and even if then it looks like that
the people asking for help are arrogant and clueless (none of which I think you were), then still,
kick/banning is immature and ethically not acceptable on a channel like that.

I am only an op on my own channels (were, although everyone else on the channel is also op, I have
"total power" basically (enforce by "social interactions" (everyone is my friend)).  But in the early
days when I was an op on non-secret channels, I never banned people, except when they were
flooding or when they were so disrupting that multiple people asked me to ban them.

That being said, if you want to avoid this in the future then I am afraid you will have to approach
IRC in a different way.  If you are the person asking a question then everyone else is giving their
time to you (even if they are wrong). Therefore look at IRC as a source of information that you can
or cannot use.  If it doesn't work, just ignore the suggestions.  Under no circumstance make the fact
that you don't seem to understand something ("it doesn't work" - but you feel insecure about why) the
problem of someone else on the channel (not even of non-ops). So, don't challenge them.  Never say
things like "I did what you said, but it doesn't work".  That is not a question namely. Instead you
could say: "Ok, I did that.  The error is still the same.".   I hope you see the difference.  Sorry that it
has to be that subtle.

0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
Hi Run, yeah, you were actually cool about it bud. The only person in there that was rather pathetic was Asher (the OP) or whatever his nick was... To be fair to him, he did try to help which is more than what I got from other places, but the guy needs to learn that not everyone starts as an expert... especially those who seek help. And newbs require patience to deal with.. esp if it's someone like me. ;P

gfaraj: are you "yuy..." ?

And you are right to say that I wasn't very welcoming in some cases. The problem was, it was the 3rd day in a row that I was seeking to find a solution to this prob. So, I was getting frustrated and alot of the suggestions I had already tried. :(  

It's a typical  "easy when you know why / how" problem... :(  

I came to C/C++ from a JAVA background. I learned some JAVA from when I was an Undergraduate at Uni.. and now I am just "teaching myself" C++ (last 2 months or so) ...  some of the differences still don't come quite natural to me :( and I have some gaps on some very BASIC stuff unfortunately. (both in C++ and programming in general). I try to improve with time and "experiences" such as this ;). The program works now btw... it's done and dusted.

My ICQ is 4506289, if you guys ever want to catch up for a chat. It looks like I won't be able to come to the C++ channel for a while ;)
0
 
LVL 7

Author Comment

by:Gene_Cyp
Comment Utility
And thanks for the help btw...
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now