【问题标题】:how to fix in this case? syntax error: missing ';' before '* "node"?在这种情况下如何解决?语法错误:缺少 ';'在'*“节点”之前?
【发布时间】:2019-05-15 07:19:00
【问题描述】:

我查看了其他类似的问题,似乎找不到相关的原因,这就是我认为的相关代码:(编辑:包括所有请求的代码)

main.cpp

#include <iostream>
#include "Pitri.h"


int main()
{
    std::cout << "Hello World!\n"; 
}

Pitri.cpp

#include "Pitri.h"
#include "node.h"
#include "dood.h"
//#include <cstdlib>
#include <iostream>


Pitri::Pitri()
{
    this->length = 0;
    this->head = NULL;
}


Pitri::~Pitri()
{
}

void Pitri::add(dood data)
{
    node* nnode = new node();
    nnode->data = data;
    nnode->next = this->head;
    this->head = nnode;
    this->length++;
}

void Pitri::remove(node * t, node* p)
{
    if (this->head == t)
    {
        if (t->next == NULL)
            this->head = NULL;
        else
            this->head = t->next;
        t->data.Death();
        free(t);
    }
    else
    {
        if (t->next == NULL)
            p->next = NULL;
        else
            p->next = t->next;
        t->data.Death();
        free(t);
    }
}

void Pitri::printP()
{
    int tallyBr = 0;
    node* head = this->head;
    int i = 1;
    while (head)
    {

    }
}

void Pitri::printS()
{
}

void Pitri::printBL()
{
}

void Pitri::populate(int x)
{
    for (int i = 0; i < x; i++)
    {
        dood* baby = new dood;
        this->add(baby);
    }

}

void Pitri::cycle(int x)
{
    //Initial definition in the event that the head node reproduces
    node* curr = this->head;
    node* mate = this->head->next;

    for (int i = 0; i < x; i++)
    {
        int deaths = 0;
        int births = 0;

        if (head == NULL)
        {
            cout << "Pitri has gone extinct!" << endl;
            break;
        }

        while (curr)
        {
            if (rand() % 100 + 1 <= this->mort && this->mort)
            {
                deaths++;
                remove(curr);
            }
            else if (curr->data.GetAge() == this->ttl && this->ttl)
            {
                deaths++;
                remove(curr);
            }
            else if (rand() % 100 + 1 <= this->birth && this->birth)
            {
                births++;
                add(curr->data.Reproduce(mate->data));
            }
            if (curr->next == NULL)
                break;
            mate = curr;
            curr = curr->next;
        }
    }

}

Pitri.h

    #pragma once


class Pitri
{
public:
    Pitri();
    ~Pitri();
    int length;
    node *head;

    void add(dood data);
    void remove(node* t);
    void printP();
    void printS();
    void printBL();
    void populate(int);
    void cycle(int);
    int getBB();
    int getBL();
    int getLL();

private:
    int ttl,mort,birth;

};

dood.cpp

    #include "dood.h"
//#include <stdio.h>
//#include <stdlib.h>
#include <time.h>

bool dood::init = false;

dood::dood()
{
    if (!init)
    {
        //Set time for random seed
        srand(time(0));
        init = true;
    }

    Primary = rand()%2;
    //Set Blue eyes to always have Blue recessive.
    if (Primary == 0)
        Secondary = rand()%2;
    else
        Secondary = 1;
}

dood::dood(int a, int b)
{
    if (!init)
    {
        //Set time for random seed
        srand(time(0));
        init = true;
    }

    Primary = a;
    Secondary = b;

}


dood::~dood()
{
}

void dood::Mutate()
{


    this->Primary = rand()%2;
    //Set Blue eyes to always have Blue recessive.
    if (Primary == 0)
        Secondary = rand()%2;
    else
        Secondary = 1;
}

dood dood::Reproduce(dood donor)
{
    int dPri = donor.GetPrimary();
    int dSec = donor.GetSecondary();
    int bPri, bSec;

    int punitvalue = rand() % 4 + 1;

    //if at least one dood has Brown/Brown...
    if((Primary == 0 && Secondary == 0) || (dPri == 0 && dSec == 0))
    {
        bPri = 0;
        // if other is Br/Br
        if (Secondary == dSec)
            bSec = 0;
        //if other is Br/Bl
        else if (Primary == dPri)
            bSec = punitvalue % 2;
        //if other is Bl/Bl
        else
            bSec = 1;
    }
    else if ((Primary == 1 && Secondary == 1) || (dPri == 1 && dSec == 1))
    {
        bSec = 1;
        if (dPri == Primary)
            bPri = 1;
        else
            bPri = punitvalue % 2;
    }
    else
    {
        if (punitvalue == 4)
        {
            bPri = 1;
            bSec = 1;
        }
        else
        {
            bPri = 0;
            if (punitvalue % 3 == 0)
                bSec = 0;
            else
                bSec = 1;
        }
    }

    dood baby = new dood(bPri, bSec);

    return baby;
}

void dood::Death()
{

}

void dood::Age()
{
    age = age + 1;
}

int dood::GetPrimary()
{
    return this->Primary;
}

int dood::GetSecondary()
{
    return this->Secondary;
}

int dood::GetAge()
{
    return age;
}

dood.h

   #pragma once
class dood
{
public:
    dood();
    dood(int, int);
    ~dood();
    void Mutate();
    dood Reproduce(dood);
    void Death();
    void Age();
    int GetPrimary();
    int GetSecondary();
    int GetAge();

private:
    // Genetec Traits "0" for brown, "1" for Blue
    int Primary;
    int Secondary;
    int age=0;
    //Has rand() been seeded?
    static bool init;

};

node.cpp

#include "node.h"



node::node()
{
}


node::~node()
{
}

节点.h

#pragma once
class node
{
public:
    node();
    ~node();
    node* next;
    dood data;
};

我没有在任何地方使用 namespace.std 这是我看到人们提到的最常见的问题,并且 AFAIK 节点没有在其他任何地方预定义?我能指点一下吗?

【问题讨论】:

  • class dood { public: } ; ?相关,我希望这不是真正的代码,因为class node 明确使用dood,但没有尝试包含依赖标头dood.h,至少在您发布的内容中没有。同样,Pitri.h 未能尝试包含 node.hdood.h,两者都包含依赖项的声明。
  • 我删除了大部分不应该相关的代码,因为我仍然需要进一步调试需要首先修复的问题。我只包含了来自 dude 类的库,因为它与节点类的定义无关。 Pitri.cpp 包括 dood.h 和 node.h,这还不够吗,因为任何进一步的内容似乎都是多余的?
  • 除非发布的 exact 代码在 we 复制/粘贴/时产生您所描述的 exact 错误编译它,除了猜测,我们不可能做任何事情,而这不是这个网站的目的。帮助我们帮助您。
  • 好的,这需要一秒钟,包括所有内容,它远未完成,我相信有很多新手错误,我想避免一个完整的批评,只关注这个错误. (我最初是在命名空间 std 之外操作并删除它和其他库来解决这个问题。我知道这是一个很大的禁忌。但这是大学教的,我必须遵循这个过程......)
  • 没有人说包括一切。包含足够的内容,以便 我们 可以产生与您相同的问题。如果你的大学教你发送垃圾邮件using namespace std;,尤其是在头文件中,至少要知道在这方面你比终身“教授”更了解,因为那是一种可怕的做法。关于你的问题。我强烈怀疑在遇到 Pitri.h 中的内容时,node 的定义不可用。到目前为止,这肯定是已发布代码中的一个问题。

标签: c++ syntax


【解决方案1】:

谢谢 WhozCraig,完成我的 Pitri 课程所需的简单修复包括:

#include "node.h"
#include "dood.h"

在头文件中,而不是在 CPP 文件中。

【讨论】:

  • 这不是唯一一个坏的。你仍然有隐含的头文件依赖,这是一个 bad 的做法。答案即将到来。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
  • 2021-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多