Thread: Directly outputting struct contents?

  1. #1
    Beginner in C++
    Join Date
    Dec 2007
    Posts
    34

    Directly outputting struct contents?

    I've just reached chapter 5 in Accelerated C++, and in C4 it briefly described making your own data structures. I've tried to implement an address book (following a suggestion on these tutorials), I've made a struct and added the information into it, and up to there the program compiles fine. Now, when I try to output the contents like this:

    Code:
    #include <iostream>
    using namespace std;
    
    struct database {
      int id_number;
      int age;
      float salary;
    };
    
    int main()
    {
      database employee;  //There is now an employee variable that has modifiable 
                          // variables inside it.
      employee.age = 22;
      employee.id_number = 1;
      employee.salary = 12000.21;
    
      cout << employee;	
      cin.get();
    
    }
    (Example taken from the cprogramming tutorials) I get this compiler error:

    no match for ‘operator<<’ in ‘std::cout << employee’

    Put simply, how do you directly output the contents of a user-defined structure?
    Ubuntu Linux / Vista Home Premium (for games)
    SCiTE text editor, G++ compiler

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Keep reading (and practicing with the examples) until at least chapter 12 for the answer. You would need to know a little more than you do right now, so take it step by step.

    If you really are impatient, then skip all the way to section 12.3.1 on pages 215 and 216.
    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

  3. #3
    Beginner in C++
    Join Date
    Dec 2007
    Posts
    34
    Ah, that's not what I was expecting :S Ok, I'll carry on practicing. Thanks!
    Ubuntu Linux / Vista Home Premium (for games)
    SCiTE text editor, G++ compiler

  4. #4
    Emulator
    Join Date
    Feb 2008
    Posts
    43
    What compiler are you using Caduceus?

  5. #5
    Beginner in C++
    Join Date
    Dec 2007
    Posts
    34
    G++? It's the default c++ compiler that comes with Ubuntu, it's called by doing:

    g++ -c "path name"
    Ubuntu Linux / Vista Home Premium (for games)
    SCiTE text editor, G++ compiler

  6. #6
    Emulator
    Join Date
    Feb 2008
    Posts
    43
    We need to get Elysia over here.. Sorry I can't provide much help as for now, compilers vary. As of now, I'm using Dev C++

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by PЯO View Post
    We need to get Elysia over here..
    Lol.

    Sorry I can't provide much help as for now, compilers vary.
    Not with standards compliant code, they don't.

    But I'll tell you this, however, as to not undermine laserlight's answer:
    How the heck can cout know the contents of your struct, much less know how to output it properly? Think about that.
    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.

  8. #8
    Beginner in C++
    Join Date
    Dec 2007
    Posts
    34
    I don't have that much working knowledge of in/output streams, so I just assumed cout worked on everything the same. I think I'm trying to make programs that are too advanced for myself.

    Thanks, I have dodged a bullet on that one (had another similar program lined up).
    Last edited by Caduceus; 02-10-2008 at 08:04 AM. Reason: Typo
    Ubuntu Linux / Vista Home Premium (for games)
    SCiTE text editor, G++ compiler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  4. My final data does not display
    By p1c1078 in forum C Programming
    Replies: 3
    Last Post: 04-23-2003, 06:32 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM