Converting C to C++

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
::compare(<unknown type>)'

Something about the use of compare() in the C++ findcode()...

Help, please?
 
D

Daniel T.

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;
}

(I just need to know if the code exists [ie, function returns 1] in the
class. Indeces are not necessary.)

// Libraries hold more than just books, but everything a
// library holds must have a code associated with it.
class Resource
{
string code;
public:
Resource( string code_ ): code( code_ ) { }
virtual ~Resource() { }
const string& getCode() const { return code; }
};

struct has_code : unary_function<Resource, int>
{
string code;
has_code( const string& code_ ): code( code_ ) { }
bool operator()( const Resource& resource ) const {
return resource.getCode() == code;
}
};

class Library
{
vector<Resource> resources;
public:
bool codeExists( const string& tcode ) const {
return find_if( resources.begin(), resources.end(),
has_code( tcode ) ) != resources.end();
}
};

class Book : public Resource
{
// add whatever here
};

This is what I have:


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;

I am inclined to make Library a full fledged class...
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;
}

bool findcode( const Library& lib, const string& tcode )
{
for (Library::const_iterator it = lib.begin(); it != lib.end(); ++it)
if ( it->getCode() == tcode )
return true;
return false;
}

(a) If all you need to know is if something with the code 'tcode'
exists, then return a 'bool' instead of an 'int'.

(b) You don't need to cast the '*itor' into a 'Book&'

(c) You don't need to use the 'compare' member-function, simply compare
them with operator==.

(d) 'b.getCode' needs parens after it... 'b.getCode()'
 
J

Jerry Coffin

@j73g2000cwa.googlegroups.com>, (e-mail address removed)
says...
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;
}


First I'd want to know whether LIBRARY::code is the
primary (or perhaps, only) method of identifying an item
in the library. If it is, I'd probably do something like
this:

typedef std::multimap<std::string, item_data> lib_t;

lib_t library;

The key of the map is equivalent to LIBRARY.code in your
function above. Finding the data for a particular item
looks like:

lib_t::iterator pos = library.find(tcode);

This returns an iterator instead of an int, so instead of
library[pos] you use *pos to refer to the data associated
with an item in the library.
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.)

In that case, you could use something like:

bool findcode(lib_t const &lib, std::string const &code)
{
return lib.find(code) != lib.end();
}

From the looks of things, you probably want to be able to
look things up by code, title, or author. In that case,
I'd probably do things a bit differently. One possibility
would be to look at the boost libraries -- if memory
serves, they have a pre-built class for a set or map with
multiple index fields.

If you prefer to use the standard library, I'd make
library a vector<item>, and then each index would be a
multimap<std::string, int>, taking the appropriate
string, and returning the index of the appropriate item
in the vector. Considering that you're likely to have
more than one book by a particular author, you might
prefer to look at using equal_range to get all the books
by an author at once.

One basic point: the fact that your C code used a linear
search in an array doesn't mean that you really want to
continue doing that. In fact, unless your library will
always be really tiny, you're probably better off doing
something else.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top