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
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;
}
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;
}
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);
}
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
Have you declared Total() as a static function? I suspect that's the problem
Change to
staticdouble 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.