why this code does not work

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • baobaoba

    why this code does not work

    The following code compiles on gcc, but cannot be executed. I got some
    wierd message when executed:
    1: Syntax error: ")" unexpected

    Is there anything wrong in my function template?


    #include <iostream>
    using namespace std;

    class DDComparePolicy
    {
    public:
    static void Compare ()
    {
    cout<<"DDCompar ePolicy "<<endl;
    }
    };

    class TDComparePolicy
    {
    public:
    static void Compare ()
    {
    cout<<"TDCompar ePolicy "<<endl;
    }
    };

    template <typename Policy>
    void validateField()
    {
    Policy::Compare ();
    }

    template <typename Policy>
    void validateAllFiel ds()
    {
    validateField<P olicy> ();
    }

    int main ()
    {
    string currentRecType ="A";
    string lastRecType = "A";

    if(currentRecTy pe == "A" && lastRecType == "A")
    validateAllFiel ds<DDComparePol icy>();
    else if(currentRecTy pe == "T" && lastRecType == "A")
    validateAllFiel ds<TDComparePol icy>();

    return 0;
    }
  • Buster Copley

    #2
    Re: why this code does not work

    baobaoba wrote:[color=blue]
    > The following code compiles on gcc, but cannot be executed. I got some
    > wierd message when executed:
    > 1: Syntax error: ")" unexpected[/color]

    That is weird.

    [buster@localhos t scratch]$ g++ -o scratch funct_templ.cpp
    [buster@localhos t scratch]$ ./scratch
    DDComparePolicy
    [buster@localhos t scratch]$

    Seems OK.
    [color=blue]
    > Is there anything wrong in my function template?[/color]

    Hard to say. I hope you can work it out.

    Regards,
    Buster.
    [color=blue]
    > #include <iostream>
    > using namespace std;
    >
    > class DDComparePolicy
    > {
    > public:
    > static void Compare ()
    > {
    > cout<<"DDCompar ePolicy "<<endl;
    > }
    > };
    >
    > class TDComparePolicy
    > {
    > public:
    > static void Compare ()
    > {
    > cout<<"TDCompar ePolicy "<<endl;
    > }
    > };
    >
    > template <typename Policy>
    > void validateField()
    > {
    > Policy::Compare ();
    > }
    >
    > template <typename Policy>
    > void validateAllFiel ds()
    > {
    > validateField<P olicy> ();
    > }
    >
    > int main ()
    > {
    > string currentRecType ="A";
    > string lastRecType = "A";
    >
    > if(currentRecTy pe == "A" && lastRecType == "A")
    > validateAllFiel ds<DDComparePol icy>();
    > else if(currentRecTy pe == "T" && lastRecType == "A")
    > validateAllFiel ds<TDComparePol icy>();
    >
    > return 0;
    > }[/color]

    Comment

    • Kevin Goodsell

      #3
      Re: why this code does not work

      baobaoba wrote:
      [color=blue]
      > The following code compiles on gcc, but cannot be executed. I got some
      > wierd message when executed:
      > 1: Syntax error: ")" unexpected
      >
      > Is there anything wrong in my function template?[/color]

      The only thing I see wrong is the lack of a #include <string> directive.

      My guess is that you are on a Unix system, and you are invoking the
      program incorrectly and running a completely different program instead.
      For example, people often name a program 'test' and try to run it:

      $ test

      Instead of

      $ ./test

      These two commands refer to completely different programs (depending on
      path settings). The first invokes the standard unix 'test' program, the
      second invokes the 'test' program that is in the current directory.

      -Kevin
      --
      My email address is valid, but changes periodically.
      To contact me please use the address from a recent posting.

      Comment

      • Noah Roberts

        #4
        Re: why this code does not work

        Kevin Goodsell wrote:[color=blue]
        > $ test
        >
        > Instead of
        >
        > $ ./test
        >
        > These two commands refer to completely different programs (depending on
        > path settings). The first invokes the standard unix 'test' program, the
        > second invokes the 'test' program that is in the current directory.[/color]

        And what is really funny is that when you do this there is absolutely no
        output from what you can see. The system 'test' program simply returns
        a value, it doesn't output anything. So you sometimes mistake no output
        for something not being executed in your code - and you would be
        right...that something is your code, it isn't being executed :P

        I myself have been caught by this before, and still accidentally do it
        from time to time though I am much faster at catching my error now :P

        NR

        Comment

        Working...