F
fakeprogress
How would I go about converting this C code to C++?
/* LIBRARY is an array of structures */
/* This function compares 'tcode' with */
/* existing codes in the array. */
/* It returns the index of the code in */
/* the LIBRARY structure if it is found. */
int findcode( LIBRARY *b, int n, char *tcode ) {
int i;
for( i = 0; i < n; i++ )
if( strcmp( b.code, tcode ) == 0 )
return i;
return -1;
}
This is what I have:
(I just need to know if the code exists [ie, function returns 1] in the
class. Indeces are not necessary.)
class Book {
private:
std::string author;
std::string title;
std::string code;
int ncopies;
int onloan;
public:
Book( const std::string &auth, const std::string &tit,
const std::string &cd, int ncop, int nonloan );
Book( const std::string &auth, const std::string &tit,
const std::string &cd, int ncop );
const std::string &getAuthor( ) const;
const std::string &getTitle( ) const;
const std::string &getCode( ) const;
int getNcopies( ) const;
int getOnLoan( ) const;
void Borrow( int qty );
void nReturn( int qty );
};
typedef std::vector<Book> Library;
int findcode( Library &lib, std::string tcode ) {
for( Library::iterator itor = lib.begin( ); itor != lib.end( );
++itor ) {
Book &b = *itor;
if( ( tcode.compare( b.getCode ) ) == 0 )
return 1;
}
return -1;
}
However, I get this error:
37 C:\CIS\22\asn2\asn2.cpp no matching function for call to
Something about the use of compare() in the C++ findcode()...
Help, please?
/* LIBRARY is an array of structures */
/* This function compares 'tcode' with */
/* existing codes in the array. */
/* It returns the index of the code in */
/* the LIBRARY structure if it is found. */
int findcode( LIBRARY *b, int n, char *tcode ) {
int i;
for( i = 0; i < n; i++ )
if( strcmp( b.code, tcode ) == 0 )
return i;
return -1;
}
This is what I have:
(I just need to know if the code exists [ie, function returns 1] in the
class. Indeces are not necessary.)
class Book {
private:
std::string author;
std::string title;
std::string code;
int ncopies;
int onloan;
public:
Book( const std::string &auth, const std::string &tit,
const std::string &cd, int ncop, int nonloan );
Book( const std::string &auth, const std::string &tit,
const std::string &cd, int ncop );
const std::string &getAuthor( ) const;
const std::string &getTitle( ) const;
const std::string &getCode( ) const;
int getNcopies( ) const;
int getOnLoan( ) const;
void Borrow( int qty );
void nReturn( int qty );
};
typedef std::vector<Book> Library;
int findcode( Library &lib, std::string tcode ) {
for( Library::iterator itor = lib.begin( ); itor != lib.end( );
++itor ) {
Book &b = *itor;
if( ( tcode.compare( b.getCode ) ) == 0 )
return 1;
}
return -1;
}
However, I get this error:
37 C:\CIS\22\asn2\asn2.cpp no matching function for call to
::compare(<unknown type>)'
Something about the use of compare() in the C++ findcode()...
Help, please?