Thread: Error: Missing type specifier

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    42

    Error: Missing type specifier

    I was doing some revisions and while making this program I got these errors I can't really figure out:

    The program is supposed to receive a students' name, midterm grade, final grade and homework grades (using a struct), and display two lists: One for students who failed, and one for students who passed, both sorted alphabetically.

    Here's the code:

    main.cpp
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include "studentdata.h"
    #include "grades.h"
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    using std::vector;
    
    using std::sort;
    
    int main()
    {
    	StudentData student;
    	vector<StudentData> students;
    
    	cout << "Enter student data (name, first exame, last exame, homework): ";
    
    	while (readData (cin, student))
    	{
    		students.push_back (student);
    		cout << "Enter student data (name, first exame, last exame, homework): ";
    	}
    
    	sort (students.begin(), students.end(), alphabetically);
    
    	vector<StudentData> failures = extractResult (students);
    
    	cout << "Passes: " << endl;
    
    	vector<StudentData>::size_type size = students.size();
    
    	for (vector<StudentData>::size_type i = 0; i < size; i++)
    	{
    		cout << students[i].name << endl;
    	}
    
    	cout << "Failures: " << endl;
    
    	size = failures.size();
    
    	for (vector<StudentData>::size_type i = 0; i < size; i++)
    	{
    		cout << failures[i].name << endl;
    	}
    	
    	return 0;
    }
    studentdata.h
    Code:
    #ifndef STUDENTDATA_H
    #define STUDENTDATA_H
    
    #include <iostream>
    #include <string>
    #include <vector>
    #include "grades.h"
    
    struct StudentData
    {
    	std::string name;
    	double midterm, final;
    	std::vector<double> hw;
    };
    
    std::istream& readData (std::istream& input, StudentData& data);
    std::istream& readHW (std::istream& input, std::vector<double>& vec);
    bool alphabetically (const StudentData& a, const StudentData& b);
    std::vector<StudentData> extractResult (std::vector<StudentData>& students);
    
    #endif
    studentdata.cpp
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include "grades.h"
    
    using std::istream;
    
    using std::vector;
    
    
    istream& readData (istream& input, StudentData& data)
    {
    	input >> data.name >> data.midterm >> data.final;
    
    	readHW (input, data.hw);
    
    	return input;
    }
    
    istream& readHW (istream& input, vector<double>& vec)
    {
    	if (input)
    	{
    		vec.clear();
    
    		double x;
    		while (input >> x)
    			vec.push_back(x);
    
    		input.clear();
    	}
    	return input;
    }
    
    bool alphabetically (const StudentData& a, const StudentData& b)
    {
    	return (a.name < b.name);
    }
    
    vector<StudentData> extractResult (vector<StudentData>& students)
    {
    	vector<StudentData>::size_type size = students.size();
    	vector<StudentData> success;
    	vector<StudentData> failures;
    
    	for (vector<StudentData>::size_type i = 0; i < size; i++)
    		Positive (students[i])? success.push_back (students[i]) : failures.push_back (students [i]);
    
    	students = success;
    
    	return failures;
    }
    grades.h
    Code:
    #ifndef GRADES_H
    #define GRADES_H
    
    #include <vector>
    #include "studentdata.h"
    
    double Grade (const StudentData& student);
    double Median (std::vector<double> vec);
    double Grade (double midterm, double final, const std::vector<double>& hw);
    bool Positive (const StudentData& student);
    double Grade (double midterm, double final, double hw);
    
    #endif
    grades.cpp
    Code:
    #include <vector>
    #include <algorithm>
    #include "grades.h"
    
    using std::vector;
    
    using std::sort;
    
    double Grade (const StudentData& student)
    {
    	return Grade (student.midterm, student.final, student.hw);
    }
    
    double Median (vector<double> vec)
    {
    	sort (vec.begin(), vec.end());
    	vector<double>::size_type size = vec.size();
    	vector<double>::size_type mid = size / 2;
    
    	return size % 2 == 0? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
    }
    
    double Grade (double midterm, double final, const vector<double>& hw)
    {
    	return Grade (midterm, final, Median (hw));
    }
    
    bool Positive (const StudentData& student)
    {
    	return Grade (student) >= 50? 1 : 0;
    }
    
    double Grade (double midterm, double final, double hw)
    {
    	return (0.2 * midterm + 0.4 * final + 0.4 * hw);
    }
    Compiler complains about grades.h with the following errors:
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C2143: syntax error : missing ',' before '&'
    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    error C2143: syntax error : missing ',' before '&'

    I looked through the code tons of times but I just can't seem the find the problem. It complains about missing type specifier on the first Grade function in grades.h and Positive function, yet their type is specified...

    I must be missing something but I really can't find it so... Any help appreciated.

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    It might be helpful to post the error messages completely.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Location
    Ohio
    Posts
    147
    Looks like you forgot to include 'studentdata.h' in 'studentdata.cpp'. If that doesn't solve your problem, start by going to the line that your compiler is pointing you too.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Grades.h includes studentdata.h and studentdata.h includes grades.h.
    VC simply aborts the inclusion process after a certain amount of circular includes, hence you get a type missing error.
    Let all .cpp files include all necessary includes, and use forward declarations in the header.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    Thanks for the help everyone, it's working now.

    I noticed I forgot to add the include for studentdata.h in studentdata.cpp but that didn't fix it. I removed grades.h from studentdata.h while trying with forward declarations in the header, and that just fixed it.

    I then noticed I didn't really need to include grades.h in studentdata.h, but still, is there any reason for something like this to happen just from including the header in another header not needed?

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Litz View Post
    Thanks for the help everyone, it's working now.

    I noticed I forgot to add the include for studentdata.h in studentdata.cpp but that didn't fix it. I removed grades.h from studentdata.h while trying with forward declarations in the header, and that just fixed it.

    I then noticed I didn't really need to include grades.h in studentdata.h, but still, is there any reason for something like this to happen just from including the header in another header not needed?
    This is just one of the problems when you have lots of interdependent classes. You end up with a headache trying to figure out which declarations are visible at which points. It's a subtle signal that maybe your code needs some reworking to remove some of the unnecessary dependencies.

    Ideally, code should be a pyramid (or according to some, a "diamond"), which higher levels depending only on lower levels. Once you start making dependency loops you get these problems.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    42
    Oh, so main should depend on both headers, but I should rework the code so one header doesn't need another to work. I'll look onto it, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  4. typename madness
    By zxcv in forum C++ Programming
    Replies: 4
    Last Post: 05-13-2006, 10:35 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM