Compile error C2664 - Why??

D

David Hoffman

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(char[]);
void ExtractID(char[], char[]);

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

ifstream RawList;
ofstream NewList;
RawList.open("RAWLIST.txt");
NewList.open("NEWLIST.txt");

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

return 0;
}

bool TestValidLine(char 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";
}
 
R

Rolf Magnus

David said:
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.

You should always mark the line that produced the error, though it's
quite obvious in this case.
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?
Yes.

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(char[]);
void ExtractID(char[], char[]);

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

ifstream RawList;
ofstream NewList;
RawList.open("RAWLIST.txt");
NewList.open("NEWLIST.txt");

//Loop while not EOF and the last line read was valid.
while (RawList.getline(cRawLine, 80) && bLastGood)

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

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(cRawLine, cidNumber);
cin >> wait;
}
}
//Close files here.
RawList.close();
NewList.close();

Closing the files here isn't neccesary, since it's done automatically
when the streams are destroyed. It doesn't hurt though.
return 0;
}

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

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)]
}

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

Stanley Yue

David Hoffman said:
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(char[]);
void ExtractID(char[], char[]);

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

ifstream RawList;
ofstream NewList;
RawList.open("RAWLIST.txt");
NewList.open("NEWLIST.txt");

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

return 0;
}

bool TestValidLine(char 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";
}


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

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

=?ISO-8859-1?Q?Juan_Antonio_Dom=EDnguez_P=E9rez?=

David said:
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(char[]);
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("RAWLIST.txt");
NewList.open("NEWLIST.txt");

//Loop while not EOF and the last line read was valid.
while (RawList.getline(cRawLine, 80) && bLastGood)
{
bLastGood = TestValidLine(cRawLine);
if (bLastGood)
{
//Extract data from line.
ExtractID(cRawLine[70], cidNumber[8]);

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(&cRawLine[70],.... all will work as you
expect.
 
D

David Hoffman

Juan Antonio Domínguez Pérez said:
David said:
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(char[]);
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("RAWLIST.txt");
NewList.open("NEWLIST.txt");

//Loop while not EOF and the last line read was valid.
while (RawList.getline(cRawLine, 80) && bLastGood)
{
bLastGood = TestValidLine(cRawLine);
if (bLastGood)
{
//Extract data from line.
ExtractID(cRawLine[70], cidNumber[8]);

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top