[英]Many errors when compiling class template
我正在嘗試使用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();
}
我已經嘗試了很多事情並搜索了網絡,但無法弄清楚我在做什么錯。
提前致謝,
德希姆
我已經刪除了實現文件,並再次將其作為.hpp文件添加到了解決方案資源管理器標題下。
我還將.h文件底部的
#include "geno.cpp"
更改為
#include "geno.hpp"
,現在可以使用了!
(我必須將geno.hpp添加為.hpp文件,而不是添加為.hpp文件中的.cpp文件。)感謝所有對此時間,精力和想法發表評論的人。
沒關系,按上述方式進行操作,一旦編譯后就不允許我更改實現。 我必須刪除.hpp文件,再次添加它,更改實現,然后編譯它,以便重新編譯。 我的一個朋友向我推薦了CLion,所以明天我將檢查CLion是否做得更好。
編輯:現在我可以出於某些原因更改實現...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.