| | |
missing storage-class or type specifiers error
![]() |
•
•
Join Date: Dec 2006
Posts: 6
Reputation:
Solved Threads: 0
Please help me figure out what is wrong with my code...
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.
C++ Syntax (Toggle Plain Text)
#include <iostream> // required to perform C++ stream I/O #include <iomanip> // required for parameterized stream manipulators using namespace std; // for accessing C++ Standard Library members //declaration of calculateAverage function-template prototype template <class T> T calculateAverage(T dataValue[], int size) // function main begins program execution ;int main() { int classSize; // size of the class // prompt the user for and input class size cout << "\nEnter the number of students in the class: "; cin >> classSize; // determine whether the user input a valid value while ( classSize <= 0 ) { cout << "\nError: Please enter a positive value" << endl; // prompt the user for and input class size cout << "\nEnter the number of students in the class: "; cin >> classSize; } // end while // dynamically allocate memory for the tests array int *tests = new int[ classSize ]; // dynamically allocate memory for the GPA's array double *GPAs = new double[ classSize ]; for ( int count = 0; count < classSize; count++ ) { // prompt the user for and input the standardized test scores cout << "\nEnter student #" << count + 1 << "'s standardized " << "test score (0 - 1600): "; cin >> tests[ count ]; // determine whether the user entered valid input while ( tests[ count ] < 0 || tests[ count ] > 1600 ) { cout << "\nError: Please enter a value between 0 and 1600" << endl; // prompt the user for and input the standardized test score cout << "\nEnter student #" << count + 1 << "'s " << "standardized test score (0 - 1600): "; cin >> tests[ count ]; } // end while // prompt the user for and input the student's GPA cout << "Enter student #" << count + 1 << "'s GPA " << "(0.0 - 4.0): "; cin >> GPAs[ count ]; // determine whether the user entered valid input while ( GPAs[ count ] < 0.0 || GPAs[ count ] > 4.0 ) { cout << "\nError: Please enter a value between 0.0 and 4.0" << endl; // prompt the user for and input the student's GPA cout << "\nEnter student #" << count + 1 << "'s GPA " << "(0.0 - 4.0): "; cin >> GPAs[ count ]; } // end while } // end for // calculate and display the standardized test and GPA averages cout << setprecision(3); cout << "The class average test score is: " << calculateAverage<int>(tests,classSize) << "\nThe class average GPA is: " << calculateAverage<double>(GPAs,classSize); return 0; // indicate that program ended successfully } // end function main T calculateAverage(T dataValue[], int size); { T total=0; for(i=0; i<size; i++) total = total + dataValue[i]; return(total / size); }
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.
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
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)
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? •
•
Join Date: Dec 2006
Posts: 6
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Dec 2006
Posts: 6
Reputation:
Solved Threads: 0
Also I read this webpage http://www.cplusplus.com/doc/tutorial/templates.html and from what I gather, my code should work.
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
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)
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
CPP Syntax (Toggle Plain Text)
template <typename T> T calculateAverage(T dataValue[], int size) { T total=0; for(int i=0; i<size; i++) total = total + dataValue[i]; return(total / size); }
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? 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:
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:
Eg. Suppose you want to create a templated class with something like this:
C++ Syntax (Toggle Plain Text)
template < class T, T::member> struct ABC { // }
You can do somethings like:
C++ Syntax (Toggle Plain Text)
// correct template < class T, typename T::member> struct ABC { // } //correct template < typename T, typename T::member> struct ABC { // } //incorrect since the compiler won't come to know that T::member // is a type. 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.
![]() |
Similar Threads
- C++ A Few Errors (C++)
- Adding memberfunctions into a class? (C++)
- Class Vectorization requirements (C++)
- "missing storage-class or type specifiers" error (C++)
Other Threads in the C++ Forum
- Previous Thread: complete(mostly) list of differences between c , c++ and related stuff.
- Next Thread: Help with structures
| Thread Tools | Search this Thread |
6 api array based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dwmapi dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui happy homeworkhelp homeworkhelper iamthwee ifstream input int integer java job lib linkedlist linker loop looping loops map math matrix memory mobile modal multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion reference rpg snakes string strings system temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






