By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,007 Members | 1,025 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,007 IT Pros & Developers. It's quick & easy.

Converting C to C++

P: n/a
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[i].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
`std::basic_string<char, std::char_traits<char>, std::allocator<char>
::compare(<unknown type>)'


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

Help, please?

May 24 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
posted:
How would I go about converting this C code to C++?


Copy-paste it into a new file, then go through it looking for
inconsistencies between C and C++. Here's a guide:

http://david.tribble.com/text/cdiffs.htm
As for, changing the "style" of it from "C style" to "C++ style"... well
maybe just get a good C++ book.
-Tomás
May 24 '06 #2

P: n/a
In article <11**********************@j73g2000cwa.googlegroups .com>,
fa**********@gmail.com wrote:
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[i].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()'
May 25 '06 #3

P: n/a
In article <1148511337.722910.174590
@j73g2000cwa.googlegroups.com>, fa**********@gmail.com
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[i].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.

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 25 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.