Syntax Help Please

This is a discussion on Syntax Help Please within the C++ Programming forums, part of the General Programming Boards category; I'm playing around with genetic algorithms, and like always, i'm trying to templatize part of it. Although I'll probably abandon ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    719

    Syntax Help Please

    I'm playing around with genetic algorithms, and like always, i'm trying to templatize part of it. Although I'll probably abandon this code because I don't like where it's going, I'm still curious as to how I can get it to compile.

    The problem I'm having is passing the template argument to "float fitness_score(Chromo<>:naType)". As is, i get the error:

    37 C:\compilers\bloodshed\main.cpp invalid conversion from `float (*)(Chromo<32>:naType)' to `float (*)(Chromo<16>:naType)'

    I've tried just about all I know, but I can't seem to get it to compile.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    
    template<int FieldSize = 32>
    class Chromo
    {
    	public:
    	struct DnaType{	unsigned dt_dna: FieldSize;	};
    
    	Chromo(void) : geneCount(FieldSize){}
    	Chromo(const Chromo<> &s) : geneCount(s.geneCount)
    		{dna = s.dna;}
    	
    	float score(float (*sc)(DnaType));
    
    	private:
    	unsigned dna : FieldSize;
    	const int geneCount;		
    };
    
    
    float fitness_score(Chromo<>::DnaType dt)
    {
          //dummy function
          return 1.2f;
    }
    
    int main(int argc, char *argv[])
    {
        const int x = 16;
        Chromo<x> ccc;          
           
        cout << ccc.score(fitness_score) << endl;
          
        system("PAUSE");    //dev-c++ generated
        return EXIT_SUCCESS;
    }
    Any ideas?
    Last edited by misplaced; 04-22-2005 at 04:05 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Well, the class has a default template argument of 32, so the argument to fitness_score() will always be 32, since it uses the default argument ( <> ). ccc on the other hand is initialized to use 16. These two are, of course, not compatible.

    Perhaps ypou want to make your fitness_score a templated function?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i have tried, but i can't seem to find the right syntax for it
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Just like any templated thingy:
    Code:
    template<int FieldSize = 32>
    float fitness_score(Chromo<FieldSize>::DnaType dt)
    {
          //dummy function
          return 1.2f;
    }
    ...unless my memory fails me...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21