sending linked list to function

Hi Guys,
I was wondering how to send a class list<T> to a function.
I have created a class Polynomial and am using this as the type of listT.
I am trying to send a list (alist) from main to an implementation file Polynomial.cpp

When I try to compile from main it says that the list has not been declared.
just wondering if someone might explain to me where I'm going wrong.
regards
Brendan

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
double Total(CO_EX_LIST & alist);            //declaration in header file Polynomial.h
typedef list<Polynomial> CO_EX_LIST;     //list <T> declaration of Polynomial class linked list in Polynomial.h




// in main()

CO_EX_LIST alist;           //Polynomial list
double xFactor                       //multiplying variable





//in Polynomial.cpp

double Polynomial::Total(CO_EX_LIST& alist,double xFactor)

{
 double runningTotal;
 for(CO_EX_LIST:: iterator it = thelist.begin();it!= thelist.end();it++)   //while traversing the list 
    {

      runningTotal+=(*it).power(xFactor);                       //compute power factor of exponent and xFactor
    }
return runningTotal;
}


You forgot to include the line where you make the call to Total.

In the Total function, you're using a variable named thelist. Did you really mean alist?
Hi Thanks for your reply.
Yes I did mean alist.
I've also included the call to total in main.
regards
Brendan

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 double Total(CO_EX_LIST & alist);            //declaration in header file Polynomial.h
typedef list<Polynomial> CO_EX_LIST;     //list <T> declaration of Polynomial class linked list in Polynomial.h




// in main()

CO_EX_LIST thelist;           //Polynomial list
double xFactor                       //multiplying variable

double rTotal = Total(thelist,xFactor);            //running total of list of polynomial values




//in Polynomial.cpp

double Polynomial::Total(CO_EX_LIST& alist,double xFactor)

{
 double runningTotal;
 for(CO_EX_LIST:: iterator it = alist.begin();it!= alist.end();it++)   //while traversing the list 
    {

      runningTotal+=(*it).power(xFactor);                       //compute power factor of exponent and xFactor
    }
return runningTotal;
}
Sorry guys
Thought I might give the error message
I think I'm calling the fuction incorrectly.
Brendan

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
double Total(CO_EX_LIST & alist);            //declaration in header file Polynomial.h
typedef list<Polynomial> CO_EX_LIST;     //list <T> declaration of Polynomial class linked list in Polynomial.h




// in main()

CO_EX_LIST thelist;           //Polynomial list
double xFactor                       //multiplying variable

double rTotal = Total(thelist,xFactor);            //running total of list of polynomial values




//in Polynomial.cpp

double Polynomial::Total(CO_EX_LIST& alist,double xFactor)

{
 double runningTotal;
 for(CO_EX_LIST:: iterator it = alist.begin();it!= alist.end();it++)   //while traversing the list 
    {

      runningTotal+=(*it).power(xFactor);                       //compute power factor of exponent and xFactor
    }
return runningTotal;
}




cd /home/boneill3/assignments/a7/
g++ -Wall -c PolynomialApp.cpp
PolynomialApp.cpp: In function âint main()â:
PolynomialApp.cpp:82: error: âTotalâ was not declared in this scope

Compilation exited abnormally with code 1 at Tue May 27 06:02:07
Ok.
The compilation failed because you are trying to call the function incorrectly. You have created a class called "Polynomial". Yet inside your main function you are trying to call a non-class function called "Total".

The correct implementation would be something like:
1
2
3
4
5
6
int main() {
 CO_EX_LIST thelist;           //Polynomial list
 double xFactor 
 Polynomial myVar = Polynomial(); // Class Declaration
 double rTotal = myVar.Total(thelist,xFactor); 
}


If the function was a static function you would use:
1
2
3
4
5
int main() {
 CO_EX_LIST thelist;           //Polynomial list
 double xFactor 
 double rTotal = Polynomial::Total(thelist,xFactor); 
}
Thanks for the reply,
I tried the second implementation to use the static function call.

double rTotal = Polynomial::Total(thelist,xFactor);

but got the following error message.


PolynomialApp.cpp: In function âint main()â:
PolynomialApp.cpp:82: error: cannot call member function âdouble Polynomial::Total(std::list<Polynomial, std::allocator<Poly\
nomial> >&, double)â without object

Compilation exited abnormally with code 1 at Tue May 27 19:39:14


The function returns a double
What object should I be sending?
regards
Brendan


[code][double Polynomial::Total(CO_EX_LIST& alist,double xFactor)

{
double runningTotal;
for(CO_EX_LIST:: iterator it = alist.begin();it!= alist.end();it++) //while traversing the list
{

runningTotal+=(*it).power(xFactor); //compute power factor of exponent and xFactor
}
return runningTotal;
}
/code]




Have you declared Total() as a static function? I suspect that's the problem
Change to
 
static double Total(CO_EX_LIST & alist);            //declaration in header file Polynomial.h 

The trouble with going in the static direction is that static methods can only access static member variables. In your case I think you will be OK because your are passing the list<Polynomial> in as a parameter.
Topic archived. No new replies allowed.