Thread: Problem with ostream, error: "undefined symbol 'out' in function main"

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    Russellville, AR
    Posts
    4

    Problem with ostream, error: "undefined symbol 'out' in function main"

    This program is our first using classes, and I cannot figure out a compiler error. Any help is really appreciated, as this may decide whether I pass the class. :-/

    The program is designed to input a set of a grades from a data file, average the grades, and return the name of the student, the student's grades, and the student's average.

    The only problem is one compiler error in line 24 of Program03.cpp. I'm using the Borland Compiler, and the error I get states:

    "Undefined symbol 'out' in function main".

    I've tried declaring 'out' but I can't seem to get it correct. Any idea why I'm getting this? I'm copying(what I believe are) the relevant parts of the three files below.

    Code:
    //Program03.cpp
    
    #include "StudentType.h"
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    const int NUM_STUDENTS = 10;
    
    
    int main(){
    string filename;
    ifstream fin;
    StudentType students[NUM_STUDENTS];
    cout << "Enter filename: ";
    cin >> filename;
    fin.open(filename.c_str());
    if(fin){
    cout << "File successfully opened." << endl;
    for(int i = 0; i < NUM_STUDENTS; ++i){
    students[i].getData(fin);
    students[i].printData(out); //This is where the error is.} 
    }
    else{
    cout << "File failed." << endl;
    cout << filename << " could not be opened. \n";
    }
    return 0;
    }
    Code:
    //StudentType.cpp
    #include "StudentType.h"
    using namespace std;
    
    StudentType::StudentType(){
    name = " ";
    average = 0.0;
    for(int i = 0; i < NUM_GRADES; ++i){
    grades[i] = 0;
    }
    }
    void StudentType::getData(std::ifstream& fin){
    float total = 0.0;
    fin >> name;
    for(int i =0; i < NUM_GRADES; ++i){
    fin >> grades[i];
    total += grades[i];
    }
    average = total/NUM_GRADES;
    }
    
    void StudentType::printData(std::ostream& out) const
    {
    cout << name;
    for(int i = 0; i < NUM_GRADES; ++i){
    out << grades[i];
    }
    out << average << endl;
    }
    Code:
    //StudentType.h
    
    #ifndef STUDENTTYPE_H
    #define STUDENTTYPE_H
    
    #include <string>
    #include <iostream>
    #include <fstream>
    
    const int NUM_GRADES = 10;
    
    class StudentType{
    public:
    StudentType();
    void getData(std::ifstream&);
    void printData(std::ostream&) const;
    private:
    std::string name;
    int grades[NUM_GRADES];
    float average;
    };
    #endif
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Error message is self explanatory. You are using something named out, and no such thing exists. If you are trying to print to standard output, the name you need to use is cout.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Location
    Russellville, AR
    Posts
    4
    Quote Originally Posted by grumpy View Post
    Error message is self explanatory. You are using something named out, and no such thing exists. If you are trying to print to standard output, the name you need to use is cout.
    Based off some info I found on one of the tutorials, I simply declared it above as:

    fstream out;

    And it compiled perfectly, would that have been appropriate?

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Sure it may compile fine, but it won't do what you expect when you run it.

    cout is a predefined stream in iostream.h that writes to standard output (i.e. your terminal). Like grumpy said, you need to write to cout, not a user defined stream.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Location
    Russellville, AR
    Posts
    4
    That's what I would have preferred to have done, but the professor asked us to do it like so:

    StudentType
    -name: string
    -grades: int[NUM_GRADES]
    -average: float
    +StudentType()
    +getData(ifstream&): void
    +printData(ostream&) const:void

    So would using the standard cout work with the printData function? Sorry to be a pain...

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    90
    Yes. cout is an ostream, so you can pass it in.

    Also, just to be consistent, in your printData() function, replace references to "cout" with "out". If you call students[i].printData(cout), then within your function, "out" will refer to "cout".

  7. #7
    Registered User
    Join Date
    Apr 2011
    Location
    Russellville, AR
    Posts
    4
    Quote Originally Posted by Clairvoyant1332 View Post
    Yes. cout is an ostream, so you can pass it in.

    Also, just to be consistent, in your printData() function, replace references to "cout" with "out". If you call students[i].printData(cout), then within your function, "out" will refer to "cout".
    So, it would look similar to?

    void StudentType:rintData(std::cout& out) const

    Never worked with ostream before.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by brunion View Post
    Based off some info I found on one of the tutorials, I simply declared it above as:

    fstream out;

    And it compiled perfectly, would that have been appropriate?
    That depends on what you intend your code to achieve. If you want code that successfully compiles but fails to write anything to a file (that's what happens with a default-constructed fstream) then your solution is perfectly appropriate.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    10
    Quote Originally Posted by brunion View Post
    So, it would look similar to?

    void StudentType:rintData(std::cout& out) const

    Never worked with ostream before.
    I think it should be
    void StudentType:rintData(ostream& out) const

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by brunion View Post
    So, it would look similar to?

    void StudentType:rintData(std::cout& out) const

    Never worked with ostream before.
    No. std::cout is an object of type std::ostream. std::ostream is the type, and it is the type you shall put in your declaration.
    Also, indent your code when posting it on the board.
    Also a good idea to dump Borland since it's old. GCC and Visual C++ are good compilers.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Undefined Symbol" when compiling a socket program?
    By phummon in forum C++ Programming
    Replies: 4
    Last Post: 07-07-2010, 01:03 PM
  2. "error LNK2001: unresolved external symbol"problem
    By gjgcstick in forum C++ Programming
    Replies: 2
    Last Post: 03-05-2010, 09:06 AM
  3. Replies: 6
    Last Post: 08-26-2008, 12:38 PM
  4. "Unresolved External Symbol" Error
    By mikeman118 in forum C++ Programming
    Replies: 12
    Last Post: 12-20-2007, 10:05 AM
  5. Ask about these "unresolved external symbol" error
    By ooosawaddee3 in forum C++ Programming
    Replies: 1
    Last Post: 06-29-2002, 11:39 AM