编译类模板时发生许多错误

时间:2018-07-27 14:18:11

标签: c++ templates header

我正在尝试使用Visual Studio在C ++中编写通用遗传算法。 为此,我要制作几个模板类。 将模板放入main.cpp时,我制作的模板类工作正常,但我想将其放在单独的头文件和cpp文件中。但是,当我尝试这样做时会遇到很多错误,包括“ C2988无法识别的模板声明/定义”。希望您能对此提供帮助。

这是头文件:

//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"

这是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;
}

这是我遇到的错误:

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 

这是我的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();
}

我已经尝试了很多事情并在网上搜索,但无法弄清楚我做错了什么。

预先感谢

Dehim

1 个答案:

答案 0 :(得分:0)

我已经删除了实现文件,并再次将其作为.hpp文件添加到了解决方案资源管理器标题下。我还将.h文件底部的#include "geno.cpp"更改为#include "geno.hpp",现在可以使用了! (我不得不将geno.hpp添加为.hpp文件,而不是添加为我更改为.hpp文件的.cpp文件。)感谢大家在这个问题上发表了自己的时间,精力和想法。 < / p>

Nevermind按照上述方法进行操作,一旦编译完成,便不允许我更改其实现。我必须删除.hpp文件,再次添加它,更改实现,然后编译它,以便重新编译。我的一个朋友向我推荐了CLion,所以明天我将检查CLion是否做得更好。

编辑:现在我可以出于某些原因更改实现...