Thread: can't figure out simple program

  1. #1
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266

    Angry can't figure out simple program

    hi, can someone please tell me why this won't compile? errors are improper typedef 'test', statment mising ; and expression syntax. thanks so much

    Code:
    #include <iostream.h>
    #define tab '\t'
    
    struct test{
    		char name;
    		int value;
    		};	
    
    ostream& operator<<(ostream& oput, test& t)
    {
    	oput<<t.name<<tab;
    	oput<<t.value<<tab<<endl;
    	return(oput);
    };
    
    main()
    {
    	test try={"a",1};
    	cout<<try;
    	return(0);
    }

  2. #2
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Lightbulb I think...

    I think it's just supposed to be:

    struct test{
    char name;
    int value;
    } // without the ;
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  3. #3
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    I dunno about that

  4. #4
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    A simple error blight, but somewhat easy to miss if you don't know C++ style try..throw..catch exception handling.

    try is a keyword.

    It is the same problem you would have with a variable called for or do.

    Also, a smaller but critical error, when you define an overloaded operator you are defining a function. So don't put a semicolon after the declaration.

    One last error, you are assigning "a" (which is a const char*) to a variable of type char without a cast. You will need to change the quotes to single quotes.

    So:

    1: Change try to something else
    2: No semicolon after operator<< declaration
    3: Single quotes around the a

  5. #5
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    thanks, as you can see i'm new at this . . . i was taught to put the ; after overloads, it works either way so i was wondering why it's a critical error? thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a very simple program
    By htdefiant in forum C++ Programming
    Replies: 13
    Last Post: 08-14-2007, 01:27 PM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. [Help] Simple Array/Pointer Program
    By sandwater in forum C Programming
    Replies: 3
    Last Post: 03-30-2007, 02:42 PM
  4. simple silly program
    By verbity in forum C Programming
    Replies: 5
    Last Post: 12-19-2006, 06:06 PM
  5. Simple window program
    By baniakjr in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2006, 03:46 PM