![]() |
| ||
| missing storage-class or type specifiers error 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. |
| ||
| Re: missing storage-class or type specifiers error 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) |
| ||
| Re: missing storage-class or type specifiers error Thanks for the help. But it isnt helping. Isnt template <class T> 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). |
| ||
| Re: missing storage-class or type specifiers error Also I read this webpage http://www.cplusplus.com/doc/tutorial/templates.html and from what I gather, my code should work. |
| ||
| Re: missing storage-class or type specifiers error Problem fixed. Thank you for your help. |
| ||
| Re: missing storage-class or type specifiers error 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 template <typename T> 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) |
| ||
| Re: missing storage-class or type specifiers error 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: 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: // correct |
| All times are GMT -4. The time now is 6:33 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC