Istream& error

This is a discussion on Istream& error within the C++ Programming forums, part of the General Programming Boards category; Hi, I am copying the following function from Accelerated C++ Code: istream& read_hw(istream& in, vector<double>& hw) { if(in) { hw.clear(); ...

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

    Istream& error

    Hi,

    I am copying the following function from Accelerated C++

    Code:
    istream& read_hw(istream& in, vector<double>& hw)
    {
    	if(in) {
    		hw.clear();
    
    		double x;
    		while(in >> x)
    			hw.push_back(x);
    
    		in.clear();
    	}
    	return in;
    }
    I get the following error:

    error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

    Anybody know why?

    Looking at the book, it doesnt advise that I need to attach a type?

    Thanks.

  2. #2
    Chi! whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    8,189
    Is this all of the code? There is nothing wrong with the function you posted -- the error is elsewhere.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,614
    Check that you included the necessary headers.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Hi,

    Thanks for the responses. The error definately points to this function and is the only error within the code.

    The header I have included is <iostream>, is that correct?

    Thanks.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Which line does the error point to? You need <vector> for vector naturally, but I imagine you've got that too.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Here is the full code and error messages:

    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <iomanip>
    #include <ios>
    #include <vector>
    
    
    
    // say what standard-library names we use
    using std::cin;
    using std::cout;
    using std::endl;
    using std::string;
    using std::max;
    using std::setprecision;
    using std::sort;
    using std::streamsize;
    using std::vector;
    
    double median(vector<double> vec)
    {
    	typedef vector<double>::size_type vec_sz;
    
    	vec_sz size = vec.size();
    	
    
    	sort(vec.begin(), vec.end());
    
    	vec_sz mid = size/2;
    
    	return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
    }
    
    double grade(double midterm, double final, double homework)
    {
    	return 0.2 * midterm + 0.4 * final + 0.4 * homework;
    }
    
    double grade(double midterm, double final, const vector<double>& hw)
    {
    	
    	return grade(midterm, final, median(hw));
    }
    
    istream& read_hw(istream& in, vector<double>& hw)
    {
    	if(in) {
    		hw.clear();
    
    		double x;
    		while(in >> x)
    			hw.push_back(x);
    
    		in.clear();
    	}
    	return in;
    }
    
    
    
    int main()
    {
    	
    	cout << "Please enter your name: ";
    	string name;
    	cin >> name;
    	cout << "Hello " << name << "!" << endl;
    
    	cout << "Please enter your midterm and final exam grades: ";
    	double midterm, final;
    	cin >> midterm >> final;
    
    	cout << "Enter all your homework grades, "
    		"followed by end-of-file: ";
    
    	vector<double> homework;
    
    	read_hw(cin, homework);
    
    	
    		double final_grade = grade(midterm, final, homework);
    
    		streamsize prec = cout.precision();
    		cout << "Your final grade is " << setprecision(3)
    			 << final_grade << setprecision(prec) << endl;
    	
    	cin.get();
    	return 0;
    }
    1>c:\users\darren\documents\visual studio 2008\projects\acceleratedc++\test.cpp(46) : error C2143: syntax error : missing ';' before '&'
    1>c:\users\darren\documents\visual studio 2008\projects\acceleratedc++\test.cpp(46) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\users\darren\documents\visual studio 2008\projects\acceleratedc++\test.cpp(46) : error C2065: 'in' : undeclared identifier
    1>c:\users\darren\documents\visual studio 2008\projects\acceleratedc++\test.cpp(46) : error C2065: 'hw' : undeclared identifier
    1>c:\users\darren\documents\visual studio 2008\projects\acceleratedc++\test.cpp(46) : error C2275: 'std::vector<_Ty>' : illegal use of this type as an expression

    As you can see, all errors point to that one line.

    Thanks.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,614
    You forgot the using declaration for istream.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Ha....so simple!

    Thanks. I usually just use namespace std, but i'm wanting to understand that a little more, hence doing it the long way.

    Thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock problem
    By Wolf` in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2010, 04:55 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21