简体   繁体   English

编译类模板时出现许多错误

[英]Many errors when compiling class template

I'm trying to program a generic genetic algorithm in C++ using Visual Studio. 我正在尝试使用Visual Studio在C ++中编写通用遗传算法。 To accomplish this, I'm making a couple of template classes. 为此,我要制作几个模板类。 The template class I've made works fine when I put it in main.cpp, but I want to put it in a seperate header and cpp file. 将模板放入main.cpp时,我制作的模板类工作正常,但我想将其放在单独的头文件和cpp文件中。 However I get many errors when I try to do it that way including "C2988 unrecognizable template declaration/definition". 但是,当我尝试这样做时会遇到很多错误,包括“ C2988无法识别的模板声明/定义”。 I'm hoping you can help with this. 希望您能对此有所帮助。

Here's the header file: 这是头文件:

//geno.h
#pragma once
//#include <iostream>
//#include "geno.cpp"

template <uint8_t NoChromo, uint32_t ChromoLength>
class geno
{
private:
    static const uint8_t NoMuTerms = 5;
    const float muTerms[3][NoMuTerms] = { { 1, 0, 0, 0, 0 },{ 0.02f, 0, 0, 0, 0 },{ -655.36f, 1, 1, 1, 1 }, };
    uint16_t mDNA[NoChromo][ChromoLength];
    uint8_t mExp[ChromoLength];
public:
    //constructor
    geno();
    //destructor
    ~geno();
    //copy constructor
    geno(const geno& og);
    //assignment operator
    void operator=(const geno& og);
private:
    //functions
    void print();
public:
    void genRand();
private:
    geno crossover() const;
    static geno recombine(const geno parents[NoChromo]);
    geno mutate() const;
public:
    static geno reproduce(const geno parents[NoChromo]);
};

#include "geno.cpp"

Here's geno.cpp 这是geno.cpp

//#include "geno.h"
#include <iostream>
#include <random>
#include <ctime>

using namespace std;

mt19937 rng(std::time(0));
uniform_int_distribution<uint16_t> distr(0, 65535);

//Constructor
template <uint8_t NoChromo, uint32_t ChromoLength> 
geno<NoChromo, ChromoLength>::geno()
{}

//Destructor
template <uint8_t NoChromo, uint32_t ChromoLength> 
geno<NoChromo, ChromoLength>::~geno()
{}

//Copy constructor
template <uint8_t NoChromo, uint32_t ChromoLength> 
geno<NoChromo, ChromoLength>::geno(const geno<NoChromo, ChromoLength>& og)
{
    for (uint32_t j = 0; j < ChromoLength; ++j)
    {
        mExp[j] = og.mExp[j];
        for (uint8_t i = 0; i < NoChromo; ++i)
        {
            mDNA[i][j] = og.mDNA[i][j];
        }
    }
}

//Assignment operator
template <uint8_t NoChromo, uint32_t ChromoLength> 
void geno<NoChromo, ChromoLength>::operator=(const geno<NoChromo, ChromoLength>& og)
{
    for (uint32_t j = 0; j < ChromoLength; ++j)
    {
        mExp[j] = og.mExp[j];
        for (uint8_t i = 0; i < NoChromo; ++i)
        {
            mDNA[i][j] = og.mDNA[i][j];
        }
    }
}

//print
template <uint8_t NoChromo, uint32_t ChromoLength> 
void geno<NoChromo, ChromoLength>::print()
{
    for (uint32_t i = 0; i < ChromoLength; ++i)
    {
        for (uint8_t j = 0; j < NoChromo; ++j)
        {
            cout << mDNA[j][i] << " ";
        }
        cout << int(mExp[i]);
        cout << endl;
    }
    cout << endl;
}

//genRand
template <uint8_t NoChromo, uint32_t ChromoLength> 
void geno<NoChromo, ChromoLength>::genRand()
{
    for (uint32_t j = 0; j < ChromoLength; ++j)
    {
        for (uint8_t i = 0; i < NoChromo; ++i)
        {
            mDNA[i][j] = distr(rng);
        }
        mExp[j] = distr(rng) % NoChromo;
    }
    ///*
    cout << "random:" << endl;
    print();
    //*/
}

//crossover
template <uint8_t NoChromo, uint32_t ChromoLength> 
geno<NoChromo, ChromoLength> geno<NoChromo, ChromoLength>::crossover() const
{
    geno PH;
    uint8_t die;
    for (uint32_t i = 0; i < ChromoLength; ++i)
    {
        die = distr(rng);
        for (uint8_t j = 0; j < NoChromo; ++j)
        {
            PH.mDNA[j][i] = mDNA[(j + die) % NoChromo][i];
        }
    }
    ///*
    cout << "crossover:" << endl;
    PH.print();
    //*/
    return PH;
}

//recombine
template <uint8_t NoChromo, uint32_t ChromoLength> 
geno<NoChromo, ChromoLength> geno<NoChromo, ChromoLength>::recombine(const geno<NoChromo, ChromoLength> parents[NoChromo])
{
    geno PH;
    uint8_t die;
    for (uint8_t i = 0; i < NoChromo; ++i)
    {
        die = distr(rng) % NoChromo;
        for (uint32_t j = 0; j < ChromoLength; ++j)
        {
            PH.mDNA[i][j] = parents[i].mDNA[die][j];
        }
    }
    ///*
    cout << "recombine:" << endl;
    PH.print();
    //*/
    return PH;
}

//mutate
template <uint8_t NoChromo, uint32_t ChromoLength> 
geno<NoChromo, ChromoLength> geno<NoChromo, ChromoLength>::mutate() const
{
    geno PH;
    uint16_t die;
    for (uint8_t i = 0; i < NoChromo; ++i)
    {
        for (uint32_t j = 0; j < ChromoLength; ++j)
        {
            die = distr(rng);
            PH.mDNA[i][j] = 1;
            for (uint8_t k = 0; k < NoMuTerms; ++k)
            {
                PH.mDNA[i][j] = PH.mDNA[i][j] * (muTerms[0][k] * mDNA[i][j] + muTerms[1][k] * die + muTerms[2][k]);
            }
        }
    }
    ///*
    cout << "mutate:" << endl;
    PH.print();
    //*/
    return PH;
}

//reproduce
template <uint8_t NoChromo, uint32_t ChromoLength> 
geno<NoChromo, ChromoLength> geno<NoChromo, ChromoLength>::reproduce(const geno<NoChromo, ChromoLength> parents[NoChromo])
{
    geno PH1[NoChromo];
    geno PH2;
    for (uint8_t i = 0; i < NoChromo; ++i)
    {
        PH1[i] = parents[i].crossover();
    }

    PH2 = recombine(PH1);
    PH2 = PH2.mutate();
    return PH2;
}

And here are the errors I'm getting: 这是我遇到的错误:

Severity    Code    Description Project File    Line    Suppression State
Error   C2182   'geno': illegal use of type 'void'  evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 51  
Warning C4244   'argument': conversion from 'time_t' to 'unsigned int', possible loss of data   evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 8   
Error   C2988   unrecognizable template declaration/definition  evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 13  
Error   C2143   syntax error: missing ';' before '<'    evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 13  
Error   C2059   syntax error: '<'   evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 13  
Error   C2588   '::~geno': illegal global destructor    evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 18  
Error   C2143   syntax error: missing ';' before '{'    evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 19  
Error   C2447   '{': missing function header (old-style formal list?)   evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 19  
Error   C2988   unrecognizable template declaration/definition  evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 23  
Error   C2143   syntax error: missing ';' before '<'    evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 23  
Error   C2059   syntax error: '<'   evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 23  
Error   C2143   syntax error: missing ';' before '{'    evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 38  
Error   C2447   '{': missing function header (old-style formal list?)   evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 38  
Error   C2988   unrecognizable template declaration/definition  evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 51  
Error   C2143   syntax error: missing ';' before '<'    evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 51  
Error   C2059   syntax error: '<'   evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 51  
Error   C2143   syntax error: missing ';' before '{'    evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 68  
Error   C2447   '{': missing function header (old-style formal list?)   evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 68  
Error   C2988   unrecognizable template declaration/definition  evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 85  
Error   C2143   syntax error: missing ';' before '<'    evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 85  
Error   C2059   syntax error: '<'   evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 85  
Error   C2143   syntax error: missing ';' before '{'    evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 107 
Error   C2447   '{': missing function header (old-style formal list?)   evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 107 
Error   C2988   unrecognizable template declaration/definition  evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 127 
Error   C2143   syntax error: missing ';' before '<'    evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 127 
Error   C2059   syntax error: '<'   evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 127 
Error   C2143   syntax error: missing ';' before '{'    evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 153 
Error   C2447   '{': missing function header (old-style formal list?)   evolution1  c:\users\dehim\documents\windesheim\2de jaar!\software\projecten\evolution1\evolution1\geno.cpp 153 

Here's my main.cpp: 这是我的main.cpp:

#include <iostream>
#include <random>
#include <ctime>
#include "geno.h"

using namespace std;

int main()
{
    const uint8_t NoChromo = 3;
    const uint32_t ChromoLength = 10;


    geno<NoChromo, ChromoLength> a[NoChromo];
    for (uint8_t i = 0; i < NoChromo; ++i)
    {
        a[i].genRand();
    }
    geno<NoChromo, ChromoLength> b = geno<NoChromo, ChromoLength>::reproduce(a);
    cin.get();
}

I've already tried a lot of things and searched the web, but can't figure out what I'm doing wrong. 我已经尝试了很多事情并搜索了网络,但无法弄清楚我在做什么错。

Thanks in advance, 提前致谢,

Dehim 德希姆

I've deleted the implementation file and added it again as a .hpp file under headers in the solution explorer. 我已经删除了实现文件,并再次将其作为.hpp文件添加到了解决方案资源管理器标题下。 I also changed #include "geno.cpp" at the bottom of my .h file into #include "geno.hpp" and now it works! 我还将.h文件底部的 #include "geno.cpp"更改为 #include "geno.hpp" ,现在可以使用了! (I had to add geno.hpp as a .hpp file and not as a .cpp file that I changed into .hpp file.) Thanks to everyone who commented on this question for their time, effort and ideas. (我必须将geno.hpp添加为.hpp文件,而不是添加为.hpp文件中的.cpp文件。)感谢所有对此时间,精力和想法发表评论的人。

Nevermind, doing it the way described above, doesn't allow me to change the implementation once it's compiled. 没关系,按上述方式进行操作,一旦编译后就不允许我更改实现。 I have to delete the .hpp file, add it again, change the implementation and then compile it, just to recompile. 我必须删除.hpp文件,再次添加它,更改实现,然后编译它,以便重新编译。 A friend of mine recommended me CLion, so tomorrow I'll check if CLion does it any better. 我的一个朋友向我推荐了CLion,所以明天我将检查CLion是否做得更好。

EDIT: Now I can change the implementation for some reason... 编辑:现在我可以出于某些原因更改实现...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM