question about .h file and .cpp file,also compiler question

Discussion in 'C++' started by key9, Sep 13, 2006.

  1. key9

    key9 Guest

    Hi all


    I think .h file offord a "catalog" of symbols and .cpp offord implements

    but I have no idea why I got such an complier error

    > g++ -o test test.cpp virtual_screen.cpp

    virtual_screen.h:15: error: expected initializer before a€?&a€? token
    test.cpp: In function a€?int main(int, const char**)a€?:
    test.cpp:14: error: no match for a€?operator<<a€? in
    a€?operator<<(((VirtualScreen&)(& tmp_vs_)), i_a) << str_tmpa€?
    virtual_screen.h:17: note: candidates are: VirtualScreen&
    operator<<(VirtualScreen&, int)
    virtual_screen.h:15: error: expected initializer before a€?&a€? token

    virtual_screen.h
    ===============================================================
    #ifndef VIRTUAL_SCREEN_H
    #define VIRTUAL_SCREEN_H


    #include <string>


    class VirtualScreen
    {
    public:
    void printf_char(std::string ch);

    }

    VirtualScreen& operator<< (VirtualScreen& p_vs, std::string p_String);
    //line 15

    VirtualScreen& operator<< (VirtualScreen& p_vs, int p_Number); //
    line 17

    #endif //VIRTUAL_SCREEN_H

    virtual_screen.cpp
    ================================================================
    #include <stdio.h>
    #include <string>
    #include "virtual_screen.h"
    #include <sstream>

    class VirtualScreen;

    void
    VirtualScreen::
    printf_char(std::string ch)
    {
    fprintf(stdout,ch.c_str());
    }

    /* sample of over ride */

    VirtualScreen& operator<< (VirtualScreen& p_vs, std::string p_String)
    {
    // Let the terminal print the string as it is.
    std::stringstream Temp;
    Temp << p_String;
    p_vs.printf_char (Temp.str());
    return p_vs;
    }


    VirtualScreen& operator<< (VirtualScreen& p_vs, int p_Number)
    {
    // Convert the number to a string and let the terminal print the
    // text representation of the number.
    std::stringstream Temp;
    Temp << p_Number;
    p_vs.printf_char (Temp.str());
    return p_vs;
    }

    ======================================================================
    test.cpp
    #include <stdio.h>
    #include <string>
    #include "virtual_screen.h"



    int main( int argc, const char* argv[] )
    {
    VirtualScreen tmp_vs_;

    int i_a = 100;
    std::string str_tmp = "This is test string";

    tmp_vs_ << i_a << str_tmp;

    return 0;


    }

    sorry for my last stupid post , I've just lost my mind.

    thank you very
    much

    key9
    key9, Sep 13, 2006
    #1
    1. Advertising

  2. key9 wrote:

    > I think .h file offord a "catalog" of symbols and .cpp offord implements
    >
    > but I have no idea why I got such an complier error
    >
    > > g++ -o test test.cpp virtual_screen.cpp

    > virtual_screen.h:15: error: expected initializer before a€?&a€? token
    > test.cpp: In function a€?int main(int, const char**)a€?:


    I don't know why the euro symbols... I've noted one error in your code.
    There may be others. In general if a compiler complains about code that

    looks ok it may be confused, take a look at the line *before* the
    complaint.


    > test.cpp:14: error: no match for a€?operator<<a€? in
    > a€?operator<<(((VirtualScreen&)(& tmp_vs_)), i_a) << str_tmpa€?
    > virtual_screen.h:17: note: candidates are: VirtualScreen&
    > operator<<(VirtualScreen&, int)
    > virtual_screen.h:15: error: expected initializer before a€?&a€? token
    >
    > virtual_screen.h
    > ===============================================================
    > #ifndef VIRTUAL_SCREEN_H
    > #define VIRTUAL_SCREEN_H
    >
    >
    > #include <string>
    >
    >
    > class VirtualScreen
    > {
    > public:
    > void printf_char(std::string ch);
    >
    > }


    semi-colon here



    > VirtualScreen& operator<< (VirtualScreen& p_vs, std::string p_String);
    > //line 15
    >
    > VirtualScreen& operator<< (VirtualScreen& p_vs, int p_Number); //
    > line 17
    >
    > #endif //VIRTUAL_SCREEN_H



    <snip>


    --
    Nick Keighley

    De maan likt niet hoog The moon doesn't look high
    Maar het is niet zo But it is not so
    De maan is wel hoog The moon is very high
    Of niet sams? Or is it?
    Nick Keighley, Sep 13, 2006
    #2
    1. Advertising

  3. key9 wrote:
    > virtual_screen.h
    > ===============================================================
    > #ifndef VIRTUAL_SCREEN_H
    > #define VIRTUAL_SCREEN_H
    >
    >
    > #include <string>
    >
    >
    > class VirtualScreen
    > {
    > public:
    > void printf_char(std::string ch);
    >
    > }
    >


    Yes as Nick wrote there has to be a semi colon at the end of the class.
    I don't know why your compiler did not show that.

    -- Murali Krishna
    Murali Krishna, Sep 13, 2006
    #3
  4. key9

    Pete Becker Guest

    Murali Krishna wrote:
    > key9 wrote:
    >> virtual_screen.h
    >> ===============================================================
    >> #ifndef VIRTUAL_SCREEN_H
    >> #define VIRTUAL_SCREEN_H
    >>
    >>
    >> #include <string>
    >>
    >>
    >> class VirtualScreen
    >> {
    >> public:
    >> void printf_char(std::string ch);
    >>
    >> }
    >>

    >
    > Yes as Nick wrote there has to be a semi colon at the end of the class.
    > I don't know why your compiler did not show that.
    >


    A semicolon is not required at the end of a class definition.

    class C
    {
    }

    c;

    This defines an object named "c" whose type is C.

    class C
    {
    }

    int i;

    This is an error which can be fixed by adding a semicolon after the
    class definition.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
    Pete Becker, Sep 13, 2006
    #4
  5. key9

    key9 Guest

    the problem solved, thanks ; but I am still confuse.
    I still don't know if a semicolon is required at end of class defination.

    but here the problem actually solved , I can focus on my mind.

    thank all of you very much!


    "Pete Becker" <> ????
    news:...
    > Murali Krishna wrote:
    >> key9 wrote:
    >>> virtual_screen.h
    >>> ===============================================================
    >>> #ifndef VIRTUAL_SCREEN_H
    >>> #define VIRTUAL_SCREEN_H
    >>>
    >>>
    >>> #include <string>
    >>>
    >>>
    >>> class VirtualScreen
    >>> {
    >>> public:
    >>> void printf_char(std::string ch);
    >>>
    >>> }
    >>>

    >>
    >> Yes as Nick wrote there has to be a semi colon at the end of the class.
    >> I don't know why your compiler did not show that.
    >>

    >
    > A semicolon is not required at the end of a class definition.
    >
    > class C
    > {
    > }
    >
    > c;
    >
    > This defines an object named "c" whose type is C.
    >
    > class C
    > {
    > }
    >
    > int i;
    >
    > This is an error which can be fixed by adding a semicolon after the class
    > definition.
    >
    > --
    >
    > -- Pete
    >
    > Author of "The Standard C++ Library Extensions: a Tutorial and Reference."
    > For more information about this book, see www.petebecker.com/tr1book.
    key9, Sep 13, 2006
    #5
  6. key9

    Jerry Coffin Guest

    In article <ee9dqr$bfa$>, says...
    > the problem solved, thanks ; but I am still confuse.
    > I still don't know if a semicolon is required at end of class defination.


    Consider something like this:

    struct point {
    int x, y;
    } point, point2;

    You have 'struct', an optional name, the body, a closing brace, and then
    an optional list of objects being defined of that type, then a semicolon
    to tell the compiler it's reached the end of the list. Even if you don't
    define any objects of the type:

    struct point {
    int x, y;
    };

    you still need the semicolon there so the compiler knows that the next
    name(s) it sees aren't supposed to be interpreted as a list of objects
    to be defined of that type.

    A class is just a struct with the default access set to private instead
    of public. You can still do the same things otherwise, and you still
    need the semicolon to signal the end of the list of objects being
    defined (even if it's empty).

    You don't need a semicolon after something like a namespace definition
    -- you can't create an object with a type of 'namespace whatever'. Since
    you _can't_ include a list of objects to be created, there's no need to
    signal the end of the list either.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Sep 13, 2006
    #6
  7. key9

    David Harmon Guest

    On Thu, 14 Sep 2006 01:06:34 +0800 in comp.lang.c++, "key9"
    <> wrote,
    >the problem solved, thanks ; but I am still confuse.
    >I still don't know if a semicolon is required at end of class defination.


    The class definition is a declaration. You must have a semicolon at
    the end of the whole declaration.

    Pete's statement that it isn't needed at the end of the class
    definition is more confusing than necessary. Yes, there can be
    other things declared between the end of the class definition and
    the semicolon. The semicolon must still be there at the end of it
    all.
    David Harmon, Sep 13, 2006
    #7
  8. key9

    Pete Becker Guest

    David Harmon wrote:
    >
    > Pete's statement that it isn't needed at the end of the class
    > definition is more confusing than necessary.


    I didn't say it isn't needed. Clearly there are times when it is needed.
    I said it isn't required, that is, there are times when it is not needed
    and, indeed, it would be an error to put it in. That was the point of
    the two examples that I also gave.

    --

    -- Pete

    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." For more information about this book, see
    www.petebecker.com/tr1book.
    Pete Becker, Sep 13, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DrUg13
    Replies:
    1
    Views:
    452
    DrUg13
    Feb 10, 2004
  2. Alex Vinokur
    Replies:
    7
    Views:
    393
    Greg Comeau
    Nov 15, 2004
  3. Vinu
    Replies:
    9
    Views:
    584
  4. www.hitechskill.com
    Replies:
    0
    Views:
    1,320
    www.hitechskill.com
    Apr 9, 2006
  5. Skybuck Flying
    Replies:
    82
    Views:
    516
    Antoon Pardon
    Nov 4, 2013
Loading...

Share This Page