Compile error C2664 - Why??

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

    Compile error C2664 - Why??

    When I compile it, I get a "error C2664: 'ExtractID' : cannot convert
    parameter 1 from 'char' to 'char []'" error and I don't understand why.
    I am just learning C++, so keep it simple. One more note, I have the
    program compiled using strings instead of char[]; however, I should be
    able to convert all my strings to char[] and get the program to
    compile. Right? Here is my code. Thanks.

    // Written by David Hoffman
    // Sept 30, 2003

    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;

    //Function Prototypes
    bool TestValidLine(c har[]);
    void ExtractID(char[], char[]);

    int main()
    {
    char cRawLine[70];
    char cidNumber[8];
    bool bLastGood = true;
    char wait;

    ifstream RawList;
    ofstream NewList;
    RawList.open("R AWLIST.txt");
    NewList.open("N EWLIST.txt");

    //Loop while not EOF and the last line read was valid.
    while (RawList.getlin e(cRawLine, 80) && bLastGood)
    {
    bLastGood = TestValidLine(c RawLine);
    if (bLastGood)
    {
    //Extract data from line.
    ExtractID(cRawL ine[70], cidNumber[8]);
    cin >> wait;
    }
    }
    //Close files here.
    RawList.close() ;
    NewList.close() ;

    return 0;
    }

    bool TestValidLine(c har cRawLine[])
    {
    return ((cRawLine[0] == '|') && (cRawLine[strlen(cRawLine )] == '|'));
    }

    void ExtractID(char cRawLine[], char cidNumber[])
    {
    int x;
    for (x = 0; x <= 6; x++)
    {
    cidNumber[x] = cRawLine[x + 6];
    }
    cout << cidNumber << "\n";
    }
  • Rolf Magnus

    #2
    Re: Compile error C2664 - Why??

    David Hoffman wrote:
    [color=blue]
    > When I compile it, I get a "error C2664: 'ExtractID' : cannot convert
    > parameter 1 from 'char' to 'char []'" error and I don't understand
    > why.[/color]

    You should always mark the line that produced the error, though it's
    quite obvious in this case.
    [color=blue]
    > I am just learning C++, so keep it simple. One more note, I have
    > the program compiled using strings instead of char[]; however, I
    > should be able to convert all my strings to char[] and get the program
    > to compile. Right?[/color]

    Yes.
    [color=blue]
    > Here is my code. Thanks.
    >
    > // Written by David Hoffman
    > // Sept 30, 2003
    >
    > #include <iostream>
    > #include <string>
    > #include <fstream>
    > using namespace std;
    >
    > //Function Prototypes
    > bool TestValidLine(c har[]);
    > void ExtractID(char[], char[]);
    >
    > int main()
    > {
    > char cRawLine[70];
    > char cidNumber[8];
    > bool bLastGood = true;
    > char wait;
    >
    > ifstream RawList;
    > ofstream NewList;
    > RawList.open("R AWLIST.txt");
    > NewList.open("N EWLIST.txt");
    >
    > //Loop while not EOF and the last line read was valid.
    > while (RawList.getlin e(cRawLine, 80) && bLastGood)[/color]

    Why do you specify a size of 80 to getline? Your array can only take 70
    characters.
    [color=blue]
    > {
    > bLastGood = TestValidLine(c RawLine);
    > if (bLastGood)
    > {
    > //Extract data from line.
    > ExtractID(cRawL ine[70], cidNumber[8]);[/color]

    This call is wrong. cRawLine[70] is not the array itself, but rather the
    71st character of it (which is btw one beyond the bounds of the array).
    Similar for cidNumber. The compiler cannot convert a char into a
    pointer to it, that's why it's complaining. Just try:

    ExtractID(cRawL ine, cidNumber);
    [color=blue]
    > cin >> wait;
    > }
    > }
    > //Close files here.
    > RawList.close() ;
    > NewList.close() ;[/color]

    Closing the files here isn't neccesary, since it's done automatically
    when the streams are destroyed. It doesn't hurt though.
    [color=blue]
    >
    > return 0;
    > }
    >
    > bool TestValidLine(c har cRawLine[])
    > {
    > return ((cRawLine[0] == '|') && (cRawLine[strlen(cRawLine )] ==
    > '|'));[/color]

    That test won't work. cRawLine[strlen(cRawLine )] is always a '\0'
    character. If cRawLine is e.g. "Test":

    strlen(cRawLine ) == 4
    cRawLine[0] == 'T'
    cRawLine[1] == 'e'
    cRawLine[2] == 's'
    cRawLine[3] == 't'
    cRawLine[4] == '\0' == cRawLine[strlen(cRawLine )]
    [color=blue]
    > }
    >
    > void ExtractID(char cRawLine[], char cidNumber[])
    > {
    > int x;
    > for (x = 0; x <= 6; x++)
    > {
    > cidNumber[x] = cRawLine[x + 6];
    > }
    > cout << cidNumber << "\n";
    > }[/color]

    Comment

    • Stanley Yue

      #3
      Re: Compile error C2664 - Why??


      "David Hoffman" <davidhoffman@c ableone.net>
      ??????:f0272d9. 0310102200.1d2a bf44@posting.go ogle.com...[color=blue]
      > When I compile it, I get a "error C2664: 'ExtractID' : cannot convert
      > parameter 1 from 'char' to 'char []'" error and I don't understand why.
      > I am just learning C++, so keep it simple. One more note, I have the
      > program compiled using strings instead of char[]; however, I should be
      > able to convert all my strings to char[] and get the program to
      > compile. Right? Here is my code. Thanks.
      >
      > // Written by David Hoffman
      > // Sept 30, 2003
      >
      > #include <iostream>
      > #include <string>
      > #include <fstream>
      > using namespace std;
      >
      > //Function Prototypes
      > bool TestValidLine(c har[]);
      > void ExtractID(char[], char[]);
      >
      > int main()
      > {
      > char cRawLine[70];
      > char cidNumber[8];
      > bool bLastGood = true;
      > char wait;
      >
      > ifstream RawList;
      > ofstream NewList;
      > RawList.open("R AWLIST.txt");
      > NewList.open("N EWLIST.txt");
      >
      > //Loop while not EOF and the last line read was valid.
      > while (RawList.getlin e(cRawLine, 80) && bLastGood)
      > {
      > bLastGood = TestValidLine(c RawLine);
      > if (bLastGood)
      > {
      > //Extract data from line.
      > ExtractID(cRawL ine[70], cidNumber[8]);
      > cin >> wait;
      > }
      > }
      > //Close files here.
      > RawList.close() ;
      > NewList.close() ;
      >
      > return 0;
      > }
      >
      > bool TestValidLine(c har cRawLine[])
      > {
      > return ((cRawLine[0] == '|') && (cRawLine[strlen(cRawLine )] ==[/color]
      '|'));[color=blue]
      > }
      >
      > void ExtractID(char cRawLine[], char cidNumber[])
      > {
      > int x;
      > for (x = 0; x <= 6; x++)
      > {
      > cidNumber[x] = cRawLine[x + 6];
      > }
      > cout << cidNumber << "\n";
      > }[/color]


      ExtractID(cRawL ine[70], cidNumber[8]); // !! error is here

      change it as follow : ExtractID(cRawL ine, cidNumber);
      The reason is : cRawLine[70] and cidNumber[8] are char, it is a element ,
      not a array!



      Comment

      • Juan Antonio Domínguez Pérez

        #4
        Re: Compile error C2664 - Why??

        David Hoffman wrote:
        [color=blue]
        > When I compile it, I get a "error C2664: 'ExtractID' : cannot convert
        > parameter 1 from 'char' to 'char []'" error and I don't understand why.
        > I am just learning C++, so keep it simple. One more note, I have the
        > program compiled using strings instead of char[]; however, I should be
        > able to convert all my strings to char[] and get the program to
        > compile. Right? Here is my code. Thanks.
        >
        > // Written by David Hoffman
        > // Sept 30, 2003
        >
        > #include <iostream>
        > #include <string>
        > #include <fstream>
        > using namespace std;
        >
        > //Function Prototypes
        > bool TestValidLine(c har[]);
        > void ExtractID(char[], char[]);
        >
        > int main()
        > {
        > char cRawLine[70];[/color]
        Beware, this must be cRawLine[80][color=blue]
        > char cidNumber[8];
        > bool bLastGood = true;
        > char wait;
        >
        > ifstream RawList;
        > ofstream NewList;
        > RawList.open("R AWLIST.txt");
        > NewList.open("N EWLIST.txt");
        >
        > //Loop while not EOF and the last line read was valid.
        > while (RawList.getlin e(cRawLine, 80) && bLastGood)
        > {
        > bLastGood = TestValidLine(c RawLine);
        > if (bLastGood)
        > {
        > //Extract data from line.
        > ExtractID(cRawL ine[70], cidNumber[8]);[/color]

        Trivial, cRawLine[70] is a char, and ExtractID expects a char* (or
        char[]). If you change ExtractId(char[]... to ExtractId(char* ,... and
        change this line to: ExtractId(&cRaw Line[70],.... all will work as you
        expect.


        Comment

        • David Hoffman

          #5
          Re: Compile error C2664 - Why??

          Juan Antonio Domínguez Pérez <dominpe@dominp e.com> wrote in message news:<bm8v7o$ao t$1@news.ya.com >...[color=blue]
          > David Hoffman wrote:
          >[color=green]
          > > When I compile it, I get a "error C2664: 'ExtractID' : cannot convert
          > > parameter 1 from 'char' to 'char []'" error and I don't understand why.
          > > I am just learning C++, so keep it simple. One more note, I have the
          > > program compiled using strings instead of char[]; however, I should be
          > > able to convert all my strings to char[] and get the program to
          > > compile. Right? Here is my code. Thanks.
          > >
          > > // Written by David Hoffman
          > > // Sept 30, 2003
          > >
          > > #include <iostream>
          > > #include <string>
          > > #include <fstream>
          > > using namespace std;
          > >
          > > //Function Prototypes
          > > bool TestValidLine(c har[]);
          > > void ExtractID(char[], char[]);
          > >
          > > int main()
          > > {
          > > char cRawLine[70]; Beware, this must be cRawLine[80]
          > > char cidNumber[8];
          > > bool bLastGood = true;
          > > char wait;
          > >
          > > ifstream RawList;
          > > ofstream NewList;
          > > RawList.open("R AWLIST.txt");
          > > NewList.open("N EWLIST.txt");
          > >
          > > //Loop while not EOF and the last line read was valid.
          > > while (RawList.getlin e(cRawLine, 80) && bLastGood)
          > > {
          > > bLastGood = TestValidLine(c RawLine);
          > > if (bLastGood)
          > > {
          > > //Extract data from line.
          > > ExtractID(cRawL ine[70], cidNumber[8]);[/color][/color]

          Thanks. Making the 2 changes you said did make the difference. The 80
          was a typo, it was supposed to be 70. I was trying so many different
          things to get the function ExtractID to work I forgot to change that
          back to 70. Now I can't get the while loop to run. All this program
          does now is quit when it gets to that line. I will try to find a
          solution on the web. I also understand that what I forgot to do with
          the function ExtractID was to setup pointers to the variables in
          memory. Thanks again.

          Comment

          Working...