Directly outputting struct contents?

This is a discussion on Directly outputting struct contents? within the C++ Programming forums, part of the General Programming Boards category; I've just reached chapter 5 in Accelerated C++, and in C4 it briefly described making your own data structures. I've ...

  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
    22,304
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Posts
    23,029
    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, 10:42 AM
  3. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-03-2008, 12:10 AM
  4. My final data does not display
    By p1c1078 in forum C Programming
    Replies: 3
    Last Post: 04-23-2003, 07: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, 02:27 PM

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