LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-27-2022, 10:19 PM   #1
errigour
Member
 
Registered: May 2009
Posts: 366

Rep: Reputation: 6
c++ how to pass empty list to function parameter?


I have this code here but it is complaining about line 38 the Null paramater I am passing to the list argument. How can i pass NULL or at least define it empty?
Code:
#include <iostream>
#include <list>

using namespace std;

template<class T>
void Swap(T&a,T&b){
    T temp = a;
    a=b;
    b=temp;
}

class ilf /* inteliigent life form */ {
    string fn;      // First Name
    string mn;      // Middle Name
    string ln;      // Last Name
    long long int a;     // Age
    list<ilf> sib;  // Siblings
public:
    void dlf (string fname, string mname, string lname, long long int age, list<ilf>sibling) {
        fn = fname;
        mn = mname;
        ln = lname;
        a = age;
        sib=sibling;
    }
};

void print_ft() /* Print Family Tree */ {
}

int main(int argc, char *argv[])
{
    char a='a',b='b';

    ilf am;

    am.dlf("Mwana", "", "", 12500, NULL);

    cout<<a<<" - "<<b<<"\n";
    Swap(a,b);
    cout<<a<<" - "<<b<<"\n";
}
 
Old 09-27-2022, 11:10 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
NULL is a pointer value. You're passing it by value, so it's not a pointer; it's a list.

If the idea is to make the parameter optional, then you declare the function like this and then you only pass in fname, mname, lname and age:

Code:
void dlf (string fname, string mname, string lname, long long int age, list<ilf>sibling = list<ilf>())

Last edited by dugan; 09-27-2022 at 11:23 PM.
 
Old 09-27-2022, 11:24 PM   #3
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Ok

After thinking about it maybe the compiler should tell me I am declaring an infinite loop of objects or something cause that's what i was doing i guess. Ok thanks for the pointer.
 
Old 09-27-2022, 11:31 PM   #4
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,223

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
Yeah, that's exactly what happens when a struct has itself as a value member.

It can have pointers to instances of itself though. That's how linked lists are done.
 
Old 09-28-2022, 12:15 AM   #5
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
Can I ask one more question here?

Mined if i ask one more question here? on line 36 i can't iterate through the list like I want there are errors what am I doing wrong there?
Code:
#include <iostream>
#include <list>

using namespace std;

template<class T>
void Swap(T&a,T&b){
    T temp = a;
    a=b;
    b=temp;
}

class ilf /* inteliigent life form */ {
public:
    string fn;          // First Name
    string mn;          // Middle Name
    string ln;          // Last Name
    long long int a;    // Age
    bool s;             // Sex false Male True Female
    list<ilf> *sib;     // Siblings

    void dlf (string fname, string mname, string lname, long long int age, bool sex, list<ilf>*sibling) {
        fn = fname;
        mn = mname;
        ln = lname;
        a = age;
        sib=sibling;
    }
};

void print_ft(ilf *pilf, string mname) /* Print Family Tree */ {
    if(!pilf)
        return;
    cout<< pilf->fn << ((mname == "NULL") ? "" : ((pilf->s) ? " Daughter of "+mname : " Son of "+mname))<<endl;

    for(auto i : pilf->sib) {
        print_ft(&i, pilf->fn);
    }
}

int main(int argc, char *argv[])
{
    char a='a',b='b';

    ilf Mwana;
    // Sons of Mwana
    ilf Marvin;
    ilf Ron;
    //Daughter of Mwana Jane
    ilf Jane;

    Marvin.dlf("Marvin", "NULL", "NULL", 12499, 0, NULL);
    Ron.dlf("Ron", "NULL", "NULL", 12498, 0, NULL);
    Jane.dlf("Jane", "NULL", "NULL", 12497, 1, NULL);
    Mwana.dlf("Mwana", "NULL", "NULL", 12500, 2, new list<ilf>{Marvin, Ron, Jane});

    cout<<a<<" - "<<b<<"\n";
    Swap(a,b);
    cout<<a<<" - "<<b<<"\n";

    print_ft(&Mwana, "NULL");
}
 
Old 09-28-2022, 12:21 AM   #6
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
nevermine i just had to make it
Code:
    if(pilf->sib)
        for(auto i : *pilf->sib) {
            print_ft(&i, pilf->fn);
        }
 
Old 09-28-2022, 10:38 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Right now, your 'siblings' list contains copies of the objects, maybe storing pointers would be better. (Actually, every object should be replaced with pointers and some garbage-collection should be used. Oh wait, that is known as "Java").
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
python: why threading.thread pass empty dictionary to function thread golden_boy615 Programming 0 03-31-2014 11:45 AM
[SOLVED] Scheme -> Lisp. How can I pass a function as parameter to another function ? muggabug Programming 2 12-26-2012 10:23 AM
[SOLVED] How can i pass an entire Array into a function parameter by value? (no pointer pass) esgol Programming 26 07-31-2012 11:34 AM
( C ) How do you declare a function pointer where a parameter is a function pointer? spursrule Programming 5 11-27-2007 07:56 PM
Passing one php function result as a parameter to another php function davee Programming 13 09-12-2004 12:08 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration