missing storage-class or type specifiers error

Reply

Join Date: Dec 2006
Posts: 6
Reputation: shift25 is an unknown quantity at this point 
Solved Threads: 0
shift25 shift25 is offline Offline
Newbie Poster

missing storage-class or type specifiers error

 
0
  #1
Dec 21st, 2006
Please help me figure out what is wrong with my code...

  1.  
  2. #include <iostream> // required to perform C++ stream I/O
  3. #include <iomanip> // required for parameterized stream manipulators
  4. using namespace std; // for accessing C++ Standard Library members
  5. //declaration of calculateAverage function-template prototype
  6. template <class T>
  7. T calculateAverage(T dataValue[], int size)
  8. // function main begins program execution
  9. ;int main()
  10. {
  11. int classSize; // size of the class
  12. // prompt the user for and input class size
  13. cout << "\nEnter the number of students in the class: ";
  14. cin >> classSize;
  15. // determine whether the user input a valid value
  16. while ( classSize <= 0 )
  17. {
  18. cout << "\nError: Please enter a positive value" << endl;
  19. // prompt the user for and input class size
  20. cout << "\nEnter the number of students in the class: ";
  21. cin >> classSize;
  22. } // end while
  23. // dynamically allocate memory for the tests array
  24. int *tests = new int[ classSize ];
  25. // dynamically allocate memory for the GPA's array
  26. double *GPAs = new double[ classSize ];
  27. for ( int count = 0; count < classSize; count++ )
  28. {
  29. // prompt the user for and input the standardized test scores
  30. cout << "\nEnter student #" << count + 1 << "'s standardized "
  31. << "test score (0 - 1600): ";
  32. cin >> tests[ count ];
  33.  
  34. // determine whether the user entered valid input
  35. while ( tests[ count ] < 0 || tests[ count ] > 1600 )
  36. {
  37. cout << "\nError: Please enter a value between 0 and 1600"
  38. << endl;
  39. // prompt the user for and input the standardized test score
  40. cout << "\nEnter student #" << count + 1 << "'s "
  41. << "standardized test score (0 - 1600): ";
  42. cin >> tests[ count ];
  43. } // end while
  44. // prompt the user for and input the student's GPA
  45. cout << "Enter student #" << count + 1 << "'s GPA "
  46. << "(0.0 - 4.0): ";
  47. cin >> GPAs[ count ];
  48. // determine whether the user entered valid input
  49. while ( GPAs[ count ] < 0.0 || GPAs[ count ] > 4.0 )
  50. {
  51. cout << "\nError: Please enter a value between 0.0 and 4.0"
  52. << endl;
  53.  
  54. // prompt the user for and input the student's GPA
  55. cout << "\nEnter student #" << count + 1 << "'s GPA "
  56. << "(0.0 - 4.0): ";
  57. cin >> GPAs[ count ];
  58. } // end while
  59.  
  60. } // end for
  61. // calculate and display the standardized test and GPA averages
  62. cout << setprecision(3);
  63. cout << "The class average test score is: " << calculateAverage<int>(tests,classSize)
  64. << "\nThe class average GPA is: " << calculateAverage<double>(GPAs,classSize);
  65. return 0; // indicate that program ended successfully
  66. } // end function main
  67. T calculateAverage(T dataValue[], int size);
  68. {
  69. T total=0;
  70. for(i=0; i<size; i++)
  71. total = total + dataValue[i];
  72. return(total / size);
  73. }


I am actually getting the following errors:
C:\Data\ET486Final\CollegeApplications.cpp(84) : error C2146: syntax error : missing ';' before identifier 'calculateAverage'
C:\Data\ET486Final\CollegeApplications.cpp(84) : error C2501: 'T' : missing storage-class or type specifiers
C:\Data\ET486Final\CollegeApplications.cpp(84) : fatal error C1004: unexpected end of file found

All 3 errors point to "T calculateAverage(T dataValue[], int size);". Thank you in advance for your help.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 482
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 47
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: missing storage-class or type specifiers error

 
0
  #2
Dec 21st, 2006
Your code is difficult to read - try adding some formatting!

a few points to note

1) Why is there a semicolon before int main() ? Presumably its supposed to belong to the function prototype 2 lines up - you should put it there instead, else it just looks like an error to anyone who's casually reading your code. (This won't stop the program from working, its just bad style IMO)

2) Get rid of the semicolon at the end of the line where you define calculateAverage after the end of main

3) calculateAverage is presumably supposed to be a templated function, so you need to specify template <typename T> before you define the function, else the compiler has no idea what T is.

4) inside calculateAverage there is a for loop - you haven't declared 'i' before attempting to initialise it

If you fix those errors, the program should compile (Although I haven't checked the logic of the program)
Last edited by Bench; Dec 21st, 2006 at 5:02 pm.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 6
Reputation: shift25 is an unknown quantity at this point 
Solved Threads: 0
shift25 shift25 is offline Offline
Newbie Poster

Re: missing storage-class or type specifiers error

 
0
  #3
Dec 21st, 2006
Thanks for the help. But it isnt helping. Isnt template <class T>
T calculateAverage(T dataValue[], int size)
what you meant by your 3rd point? And I tell it what T is in the cout before the return 0; when I call the function. Also just to clarify, I have made the changes in Bench's post (1st, 2nd, and 4th items).
Last edited by shift25; Dec 21st, 2006 at 5:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 6
Reputation: shift25 is an unknown quantity at this point 
Solved Threads: 0
shift25 shift25 is offline Offline
Newbie Poster

Re: missing storage-class or type specifiers error

 
0
  #4
Dec 21st, 2006
Also I read this webpage http://www.cplusplus.com/doc/tutorial/templates.html and from what I gather, my code should work.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 6
Reputation: shift25 is an unknown quantity at this point 
Solved Threads: 0
shift25 shift25 is offline Offline
Newbie Poster

Re: missing storage-class or type specifiers error

 
0
  #5
Dec 21st, 2006
Problem fixed. Thank you for your help.
Last edited by shift25; Dec 21st, 2006 at 6:09 pm. Reason: Problem solved.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 482
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 47
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: missing storage-class or type specifiers error

 
0
  #6
Dec 21st, 2006
The compiler must be told every single time a section of code is templated,
essentially, you need the template parameter list for both the forward declaration at the top of your program (which you already have) and then again when you actually define it. ie

  1. template <typename T>
  2. T calculateAverage(T dataValue[], int size)
  3. {
  4. T total=0;
  5. for(int i=0; i<size; i++)
  6. total = total + dataValue[i];
  7. return(total / size);
  8. }

Simply having the template parameter list at the forward declaration isn't enough - since when you reach the definition of calculateAverage, the compiler doesn't know what T is (template parameter lists are not strictly part of a function signature)
Last edited by Bench; Dec 21st, 2006 at 6:07 pm.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,581
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 461
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: missing storage-class or type specifiers error

 
0
  #7
Dec 21st, 2006
It is worth noting that though those two things ( <typename T> and <class T> ) are interchangeable in most of the cases there are some senarios where you must use <typename T>.

Eg. Suppose you want to create a templated class with something like this:
  1. template < class T, T::member> struct ABC { // }
This actually won't work since in templated class you are passing the type and then the member of the same type. So in such cases <class T> won't work.

You can do somethings like:
  1. // correct
  2. template < class T, typename T::member> struct ABC { // }
  3.  
  4. //correct
  5. template < typename T, typename T::member> struct ABC { // }
  6.  
  7. //incorrect since the compiler won't come to know that T::member
  8. // is a type.
  9. template < typename T, T::member> struct ABC { // }
Last edited by ~s.o.s~; Dec 21st, 2006 at 10:33 pm.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC