【问题标题】:C++ Why the void function is not working eventhough I called it? [duplicate]C++ 为什么即使我调用了 void 函数也不起作用? [复制]
【发布时间】:2019-04-07 11:53:26
【问题描述】:

我不知道如何使用调试器,我想我没有 所以我试图在 C++ Called Product 中创建简单的链表 此列表将用于存储产品... 我已经做了一个函数,可以在列表的开头添加一个新节点 并且还制作了一个名为“Afficher1”的无效函数,它应该向我显示列表中的产品总数及其总价(不含增值税) 以及增值税总额和最终总额(含增值税) 但是当我在主函数中调用 void 时,它不会运行它只是用返回值完成主执行!=0

当我删除函数内部的一些操作时,例如:

double total_TVA=((total)*(temp->TVA))/(100.0); 双TTC=total+total_TVA;

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

struct Product{
    string code_prod;
    string designation;
    string UM;
    double PUA_HT;
    double QTE;
    double TVA;
    Product *next;
};

    Product *head=NULL;


    Product *Add_Product(Product* &head, string code, string des, string um, double pua, double qte, double tva){
        Product *prod=new Product;
        prod->code_prod=code;
        prod->designation=des;
        prod->UM=um;
        prod->PUA_HT=pua;
        prod->QTE=qte;
        prod->TVA=tva;
        prod->next=head;
        head=prod;

        return head;
    }

    void Afficher1(){

        if(head != NULL){
            Product *temp=head;
            double total=0;
            int i=0;
            while(temp != NULL){
                total=total + ((temp->PUA_HT)*(temp->QTE));
                i++;
                temp=temp->next;
            }
            double total_TVA=((total)*(temp->TVA))/(100.0);
            double TTC=total+total_TVA;
            cout<<"Nombre total des produits Achetes: "<<i<<endl;
            cout<<"Le Montant Total HT: "<<total<<endl;
            cout<<"Total TVA de "<<temp->TVA<<" : "<<total_TVA<<endl;
            cout<<"Total TTC: "<<TTC<<endl;
        }   


    }
int main(){
    Product *head=NULL;
    string codes; string dess; string ums; double puas; double qtes; double tvas;
    for(int i=0;i<1;i++){
        cout<<"Donner les infos pour le proudit "<<i+1<<endl;
        cin>>codes;
        cin>>dess;
        cin>>ums;
        cin>>puas;
        cin>>qtes;
        cin>>tvas;
        head=Add_Product(head, codes, dess, ums, puas, qtes, tvas);

    }
    Afficher1();
    return 0;
}

【问题讨论】:

  • 想想tempwhile (temp != NULL) 循环之后是什么。
  • Afficher1() 没有被调用或没有运行似乎极不可能。看看它:你能想象它可能运行的方式,但给你的印象是它没有运行吗?
  • 获得调试器并学习使用它将对您大有裨益。您甚至可能已经拥有一个,因为大多数 IDE 都包含一个或包含与外部接口的接口,并且大多数操作系统的开发人员工具至少包含一个(例如 gdb)。
  • “我不知道如何使用调试器而且我没有” 这是一个完全不相关的陈述。每个 c++ 工具链都附带一个调试器,您显然应该学习如何使用它。这是每个程序员都需要掌握的基本技能。
  • “我不知道如何使用调试器,我也没有” - 那么你应该获得一个并学习如何使用它。请停止喊叫。

标签: c++ linked-list singly-linked-list


【解决方案1】:

在您的main() 函数中,您声明一个名为head 的局部变量:

int main(){
   Product *head=NULL;

...然后你将它设置为非NULL:

    head=Add_Product(head, codes, dess, ums, puas, qtes, tvas);

...但是,您的 Afficher1() 函数不知道该局部变量,而是查看您在程序顶部声明的全局变量 head

Product *head=NULL;

...并且该全局变量仍然为 NULL,因此 Afficher1() 顶部的 if (head != NULL) 测试失败并且该函数什么也不做。

【讨论】:

  • 其中一个“头”与另一个不同,其中一个“头”不属于...
  • 那么,我应该删除 head 的全局声明还是本地声明?
  • 我认为删除全局链表会更好,因为这样你就可以在内存中同时拥有两个链表。
  • 我已经尝试了这里提到的所有方法,但所有解决方案都不起作用
【解决方案2】:

您的程序存在全局声明变量的范围和同名的本地声明变量相互覆盖的基本问题。

此外,您在分配 'head->next = head' 时出现逻辑错误,这将创建循环链表。

如果条件 (head != NULL) 下包含的函数体未执行,因为 head 变量在全局范围内为 NULL。

我在上面的代码中添加了 cmets 指向相同的内容,现在它可以正常工作了。见下文:-

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

struct Product{
    string code_prod;
    string designation;
    string UM;
    double PUA_HT;
    double QTE;
    double TVA;
    Product *next;
};

    /* This globally visible head pointer */
    Product *head=NULL;


    Product *Add_Product(Product* &head, string code, string des, string um, double pua, double qte, double tva){

        Product *prod=new Product;
        prod->code_prod=code;
        prod->designation=des;
        prod->UM=um;
        prod->PUA_HT=pua;
        prod->QTE=qte;
        prod->TVA=tva;
        prod->next=NULL; // You set it to head, will create circular linked list and your Afficher1 loop will run infinitely.

        /* This is head pointer is pointing to GLOBAL head which is NULL */
        head=prod;

        return head;
    }

    void Afficher1(){
        if(head != NULL){
            Product *temp=head;
            double total=0;
            int i=0;
            while(temp != NULL){
                total=total + ((temp->PUA_HT)*(temp->QTE));
                i++;
                temp=temp->next;
            }
            double total_TVA=((total)*(temp->TVA))/(100.0);
            double TTC=total+total_TVA;
            cout<<"Nombre total des produits Achetes: "<<i<<endl;
            cout<<"Le Montant Total HT: "<<total<<endl;
            cout<<"Total TVA de "<<temp->TVA<<" : "<<total_TVA<<endl;
            cout<<"Total TTC: "<<TTC<<endl;
        }   


    }
int main(){
    /* This is locally declared and initialized head pointer which overrides global scope */
    // Product *head=NULL;
    string codes; string dess; string ums; double puas; double qtes; double tvas;
    for(int i=0;i<1;i++){
        cout<<"Donner les infos pour le proudit "<<i+1<<endl;
        cin>>codes;
        cin>>dess;
        cin>>ums;
        cin>>puas;
        cin>>qtes;
        cin>>tvas;
        head=Add_Product(head, codes, dess, ums, puas, qtes, tvas);

    }
    Afficher1();
    return 0;
}

【讨论】:

  • 您好,谢谢您的回复我认为我添加节点的方式是正确的,因为在您的情况下,您将节点添加到列表的末尾,而在我的情况下,我正在添加它首先,我也尝试了您的代码,但该函数在执行时仍然没有显示任何内容:/
  • 我已经编辑了代码。现在它将按预期打印。我在您的代码中强调了问题,忘记注释掉本地声明的 head 变量,它会覆盖您的全局声明的 head 变量。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
  • 2018-05-01
  • 1970-01-01
  • 2021-04-16
  • 2022-06-18
  • 2013-08-24
  • 1970-01-01
相关资源
最近更新 更多