AlasdairRyan AlasdairRyan - 1 month ago 11
C++ Question

c++ Passing List Of Classes In Function Parameter

I have been trying to pass a instance of a class "person" to a function to print the contains of that list,but am unsure how to solve the errors.

Program::Program()
{

list<Person*> people;
list<Person*>::iterator iter;

cout << "Hello, Please Enter The Number Of People " << endl;
cin >> Maxpeople;
{
for (int i = 0; i < Maxpeople; i++)
{
Person *p1 = new Person();

cin.ignore();
cout << "Hello " << endl;
cout << "Please Enter The Name Of Person " << i + 1 << endl;
getline(cin, input);
p1->setname(input);

people.push_back(p1);
}
Program P;

P.print(people, iter); <----Call Print Function

getline(cin, input);
}

//header
void print(list<Person*> p, list<Person*>::iterator i);
//header

void Program::print(list<Person*> p, list<Person*>::iterator i)
{
for (i = p.begin(); i != p.end(); i++)
{
cout << (*i)->getname() << endl;
}
}


Severity Code Description Project File Line Suppression State
Error C2061 syntax error: identifier 'list' App1 c:\users\alasdair\documents\visual studio 2015\projects\drawstudio\c++fun\app1\stdafx.h 26

Error C2061 syntax error: identifier 'list' App1 c:\users\alasdair\documents\visual studio 2015\projects\drawstudio\c++fun\app1\stdafx.h 26

Error C2511 'void Program::print(std::list>,std::_List_iterator>>)': overloaded member function not found in 'Program' App1 c:\users\alasdair\documents\visual studio 2015\projects\drawstudio\c++fun\app1\app1.cpp 55

Error C2061 syntax error: identifier 'list' App1 c:\users\alasdair\documents\visual studio 2015\projects\drawstudio\c++fun\app1\stdafx.h 26

I believe looking at the errors c2061/2511 I am not defining the type of lists correctly.Any help will be greatly appreciated,thanks.

---update one -----

Severity Code Description Project File Line Suppression State
Error C2065 'Person': undeclared identifier App1 c:\users\alasdair\documents\visual studio 2015\projects\drawstudio\c++fun\app1\stdafx.h 26

Error C2059 syntax error: '>' App1 c:\users\alasdair\documents\visual studio 2015\projects\drawstudio\c++fun\app1\stdafx.h 26

Error C2955 'std::list': use of class template requires template argument list App1 c:\users\alasdair\documents\visual studio 2015\projects\drawstudio\c++fun\app1\stdafx.h 26

Error C2976 'std::list': too few template arguments App1 c:\users\alasdair\documents\visual studio 2015\projects\drawstudio\c++fun\app1\stdafx.h 26

Warning C4717 'Program::Program': recursive on all control paths, function will cause runtime stack overflow App1 c:\users\alasdair\documents\visual studio 2015\projects\drawstudio\c++fun\app1\app1.cpp 53

Answer

So I figured out what I was doing wrong(although I am using a list of strings and not a custom class)as the people who replied said I was not passing by reference correctly.For any one interested this is my code.Obviously you can use the pop_front() to retrieve the next value. Many thanks for your help.

====Main====

#include "stdafx.h"

int main()
{
    std::string input = "";

    std::list<std::string> people;
    people.push_back("Harry");
    printlist(&people);

    std::cin >> input;
}

void printlist(std::list<std::string> *mylist)
{
    std::list<std::string>::iterator myiterator;

    for (myiterator = mylist->begin(); myiterator != mylist->end();myiterator++)
    {
        std::cout << mylist->front() << std::endl;
    }
}

====Header====

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <string>
#include <iostream>
#include <list>

void printlist(std::list<std::string> *mylist);