将struct作为参数传递给函数不工作 [英] Give struct as parameter to a function doesnt work

查看:221
本文介绍了将struct作为参数传递给函数不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个双链表结构,我想使用函数duplaLista连接节点和插入值。我实际上发现在某人elses问题如何给结构作为参数在函数调用。但它不会为我工作(不完全类型是不允许的)。我也读过关于这个问题的答案,但我不能理解我做错了什么,因为我只是看到它应该这样做,不是我完全理解它吗?有人可以告诉我有什么问题,并解释为什么?

I created a double linked list structure, and I want to use function duplaLista to connect nodes and insert values.I have actually found in someone elses question how to give struct as a parameter in function call. But it wont work for me(Incomplete type is not allowed). I also have read answers on that issue but I cant understand what am I doing wrong because I just saw it should be done that way not that I understood it completely? Can someone tell me what is wrong, and explain me why?

#include "DoubleList.h"
#include<iostream>
#include<stdio.h>
#include"string"
using namespace std;

struct Cvor
{
    Cvor *head;
    Cvor *tail;
    char vred;

    Cvor(const char &value, Cvor *prev = NULL, Cvor *next = NULL) : vred(value),
                                                                    head(next), tail(prev)
    {}

};

void duplaLista(Cvor *cvor)
{

}


int main(int argc, char *argv[])
{   
    Cvor cvor;

    duplaLista(cvor);

    return 0;
}


推荐答案

首先,因为你没有为Cvor定义一个默认构造函数,除非你提供最小的必要参数来构造Cvor,否则main中的声明(即Cvor cvor)将失败。你需要给它一些样本const char&以便能够构建它。完成后,您需要修复duplaLista提供的不正确的参数类型。更正后,您的主要功能应如下所示。下面给出的参数A只是一个初始值。

You have two errors here. First, as you did not define a default constructor for Cvor, unless you provide the minimum essential parameters to construct Cvor, the declaration in main(i.e. Cvor cvor) will fail. You need to give it some sample const char & to be able to construct it. When that's done, you need to fix the incorrect type of parameter you provide to duplaLista. When corrected, your main function should look as follows. The parameter 'A' I gave below is just a sample initial value. Replace it with whatever you think is useful.

int main(int argc, char *argv[])
{   
    Cvor cvor('A');
    duplaLista(&cvor);
    return 0;
}

这篇关于将struct作为参数传递给函数不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆