繁体   English   中英

C ++类型无法解析

[英]c++ type could not be resolved

我正在Eclipse CDT中使用c ++程序。 我在Ubuntu 13上使用gcc编译器。该程序有两个类,Function和Trial。 在Trial类的一行中,我尝试使用new分配一组Function。 但是,Eclipse告诉我“类型'功能'无法解析”。 奇怪的是,用相同的方法,用new创建Function的实例确实可行。 我已经包括了所有文件,以防它们有用,尽管可能只是Trial.cpp。 该错误在Trial :: run方法中标记

试用版

#include "Trial.h"
#include "Function.h"
#include <string>
using namespace std;

Trial::Trial(long double* xs, long double* ys, int length, int popSize, double mutRate, int seed){
    this->xs=xs;
    this->ys=ys;
    this->length=length;
    this->popSize=popSize;
    this->mutRate=mutRate;
    this->seed=seed;
}
void Trial::run()
{
    Function* newArray =
            new Function[popSize]; //error here
    Function* f=new Function('x',0,0); //no error here
}


Trial::~Trial() {
    // TODO Auto-generated destructor stub
}

试用版

#ifndef TRIAL_H_
#define TRIAL_H_

#include "Function.h"

class Trial {
public:
    Trial(long double* xs, long double* ys, int length, int popSize, double mutRate, int seed);
    void run();
    virtual ~Trial();
private:
    long double* xs;
    long double* ys;
    int length;
    int popSize;
    double mutRate;
    int seed;
};

#endif /* TRIAL_H_ */

功能.h

#include <string>
#include <sstream>
using namespace std;

#ifndef FUNCTION_H_
#define FUNCTION_H_

class Function {
public:
    Function(char op, Function* l, Function* r);
    long double getValue(long double x);
    virtual ~Function();
    virtual string toString();
    static Function* makeFunction(int maxDepth);
    Function* clone();
    Function* breed(Function* other);
    void setNode(int i, Function* node);
    static double mutateChance;
    long double getFitness(long double* xs,long double* ys, int length);
private:
    Function* left;
    Function* right;
    char op;
    long double fitness;
    int getNumNodes();
    Function* getNode(int i);
    void mutate();
};

#endif /* FUNCTION_H_ */

Function.cpp //这很长,可能并不重要

#include "Function.h"
#include <sstream>
#include <iostream>
#include <cmath>
#include <cstdlib>
#define NUMBER_WEIGHT 1
#define OP_WEIGHT 5
#define X_WEIGHT 5
#define NUM_SWAPS 3
#define TOTAL 39
//Total is 5*OP_WEIGHT+X_WEIGHT+9*NUM_WEIGHT
using namespace std;

double Function::mutateChance=.01;

bool isOp(char c)
{
    return c=='+'||c=='-'||c=='*'||c=='/'||c=='^';
}
Function::Function(char op, Function* l, Function* r) {
    this->left=l;
    this->right=r;
    this->op=op;
}

Function::~Function() {
    // TODO Auto-generated destructor stub
    delete left;
    delete right;
}

string Function::toString()
{
    stringstream stream;
    if(isOp(op))
    {
        stream<<'('<<left->toString()<<op<<right->toString()<<')';
    }
    else
    {
        stream<<op;
    }
    return stream.str();
}
long double Function::getValue(long double x)
{
    switch(op)
    {
    /*case '0'://Zeroes are stupid.  adding/subtracting does nothing.  Dividing makes an error or zero.  Multiplying makes 0.  exponentiation makes 1, 0 or an error.
        return 0;*/
    case '1':
        return 1;
    case '2':
        return 2;
    case '3':
        return 3;
    case '4':
        return 4;
    case '5':
        return 5;
    case '6':
        return 6;
    case '7':
        return 7;
    case '8':
        return 8;
    case '9':
        return 9;
    case 'x':
        return x;
    case '+':
        return left->getValue(x)+right->getValue(x);
    case '-':
        return left->getValue(x)-right->getValue(x);
    case '*':
        return left->getValue(x)*right->getValue(x);
    case '/':
        return left->getValue(x)/right->getValue(x);
    case '^':
        return pow(left->getValue(x),right->getValue(x));
    }
    return 0.0;
}
char toOp(int i)
{
    char c=(char)i;
    bool found=false;
    for(int i=1;i<=9;i++)
        if(c<i*NUMBER_WEIGHT)
        {
            c=i+48;
            found=true;
            break;
        }
    if(!found)
    {
        c-=9*NUMBER_WEIGHT;
        if(c<X_WEIGHT)
        {
            c='x';
            found=true;
        }
    }
    if(!found)
    {
        c-=X_WEIGHT;
        for(int i=1;i<=5;i++)
            if(c<i*OP_WEIGHT)
            {
                switch(i)
                {
                case 1:
                    c='+';
                    break;
                case 2:
                    c='-';
                    break;
                case 3:
                    c='*';
                    break;
                case 4:
                    c='/';
                    break;
                case 5:
                    c='^';
                    break;
                }
                break;
            }
    }
    return c;
}
Function* Function::makeFunction(int maxDepth)
{
    char c;//first are numbers, then x, then the operators, +-*/^
    if(maxDepth==0)
        c=rand()%(10*NUMBER_WEIGHT);
    else
        c=rand()%(10*NUMBER_WEIGHT+5*OP_WEIGHT);
    c=toOp(c);
    if(isOp(c))
        return new Function(c,makeFunction(maxDepth-1),makeFunction(maxDepth-1));
    else
        return new Function(c,0,0);
}
Function* Function::clone()
{
    if(isOp(op))
        return new Function(op,left->clone(),right->clone());
    else
        return new Function(op,0,0);
}
Function* Function::getNode(int i)
{
    if(i==0)
        return this;
    int l=left->getNumNodes();
    if(i<=l)
        return left->getNode(i-1);
    else
        return right->getNode(i-l-i);
}
void Function::setNode(int i, Function* node)
{
    node=node->clone();
    int l=left->getNumNodes();
    if(i==1)
        right=node;
    else if(i==l+1)
        left=node;
    else if(i<=l)
        left->setNode(i-1,node);
    else
        right->setNode(i-l-1,node);
}
int Function::getNumNodes()
{
    if(isOp(op))
        return 1+left->getNumNodes()+right->getNumNodes();
    else
        return 1;
}
double randDouble()
{
    return 1.*rand()/RAND_MAX;
}
void Function::mutate()
{
    if(isOp(op))
        do
            op=toOp(rand()%TOTAL);
        while(!isOp(op));
    else
        do
            op=toOp(rand()%TOTAL);
        while(isOp(op));
}
Function* Function::breed(Function* other)
{
    Function* child;//The mom is copied, then functions form the dad are switched in
    Function* dad;
    if(rand()%2)
    {
        child=this->clone();
        dad=other;
    }
    else
    {
        child=other->clone();
        dad=this;
    }
    int num=dad->getNumNodes();
    for(int i=0;i<NUM_SWAPS;i++)
        child->setNode(1+rand()%(child->getNumNodes()-1),dad->getNode(rand()%num));
    num=child->getNumNodes();
    for(int i=0;i<num;i++)
        if(randDouble()<mutateChance)
            child->getNode(i)->mutate();
    return child;
}

long double Function::getFitness(long double* xs, long double* ys, int length)
{
    long double total=0;
    for(int i=0;i<length;i++)
        total+=abs(ys[i]-this->getValue(xs[i]));
    total/=length;
    this->fitness=total;
    return total;
}

new[]运算符需要一个公共默认构造函数。 Function不提供一个(由于Function具有用户定义的构造函数,因此编译器不会自动生成默认的构造函数)。 更改它,它将起作用。

顺便说一句,您不需要在Trial.h #include "Function.h" :不需要。

暂无
暂无

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

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