Please check out Federation II !

Member Discussions

terms



[Previous] [Next] [Post] [Reply] [Topics] [Summary] [Search]


1. C++ in Linux not being able to convert const Mon Jan 26, 2004 [4:25 PM]
Dern
Email not supplied
member since: Jan 26, 2004
Reply
Ugh, I'm beating my head against the monitor, here's the situation. Upon make i get the following error:

/usr/bin/g++ -c -O -Wall -I. account.cpp -o ./obj/account.o
account.cpp: In function `void do_purchase(char_data*, char*)':
account.cpp:909: no matching function for call to `matches(char*&, const
char[4])'
string2.h:13: candidates are: bool matches(const char*&, const char*)
string2.h:81: bool matches(const char*&, const char*, bool)
account.cpp:910: could not convert `argument' to `const char*&'
string2.h:16: in passing argument 1 of `bool number_arg(const char*&, int&)'
make: *** [account.o] Error 1


I COULD go and change all the const's, but there has to be an easier way around this.

/* code */

void do_request( char_data* ch, char* argument )
{
char* tmp;

if( matches( argument, 'cancel' ) )

/* matches def */

bool matches ( const char*&, const char* );

bool exact_match ( const char*&, const char* );

inline bool matches( const char*& argument, const char* word, bool exact )
{
return( exact ? exact_match( argument, word )
: matches( argument, word ) );
}


Thanks in advance!!


2. RE: Well, THAT is not the problem here.. Mon Jan 26, 2004 [5:08 PM]
muir
Email not supplied
member since: Sep 14, 2003
In Reply To
Reply
The problem is that 'cancel' is parsed as a character constant. Use "cancel" to make it a string literal which is what you want.

Additional tips: references to pointers are unnecessary in this situation, drop them; and use std::string above char*'s unless you have a very good reason not to do so.

.


3. RE: Well, THAT is not the problem here.. Mon Jan 26, 2004 [5:10 PM]
Dern
Email not supplied
member since: Jan 26, 2004
In Reply To
Reply
Cancel is actually in double quotes, for some reason this forum changed them to single quotes lol.


4. RE: C++ in Linux not being able to convert const Mon Jan 26, 2004 [6:14 PM]
Ghasatta
Email not supplied
member since: Jun 17, 2002
In Reply To
Reply
The problem lies within the fact that you are passing references. It's unclear to me why you are doing it that way - as muir said, references are unnecessary for what you are doing.

Essentially, you are taking the pointer argument (not a reference to a pointer) and trying to convert it to a reference to a pointer, which is what the error message is about; not converting to a constant.

I would also echo muir's recommendation to use std::string if you are in fact going to use C++. However, one problem we have run into is that std::string is not guaranteed to be thread-safe (and, unfortunately, doesn't seem to be on most platforms ;). After going multi-threaded, we had weird crashes until I finally said screw it and wrote my own Buffer class to replace std::string.

Good luck,
-Ghasatta


5. RE: C++ in Linux not being able to convert const Mon Jan 26, 2004 [6:26 PM]
muir
Email not supplied
member since: Sep 14, 2003
In Reply To
Reply
I thought that might be the problem originally, but the error messages seem to suggest otherwise, distinctly pinning the 'argument' as the failing cast.. wouldn't be the first time the error message isn't correct, though :)

As an aside, the reason std::string isn't thread-safe is that you should code in a fashion that doesn't allow it to be a problem .)

.


6. RE: C++ in Linux not being able to convert const Mon Jan 26, 2004 [6:36 PM]
Dern
Email not supplied
member since: Jan 26, 2004
In Reply To
Reply
This appears to be a distinctly linux thing, it compiles fine with MSVC. Could there possibly be a compiler option to make it more forgiving?


7. RE: C++ in Linux not being able to convert const Mon Jan 26, 2004 [6:39 PM]
muir
Email not supplied
member since: Sep 14, 2003
In Reply To
Reply
>This appears to be a distinctly linux thing, it compiles fine with MSVC.

In general when MSVC compiles something other compilers don't, it's a good sign you need to change your code :)

>Could there possibly be a compiler option to make it more forgiving?

I you do not want to make it more forgiving in that kind of an instance. Let's try figure it out again..

.

(Comment added by muir on Mon Jan 26 20:56:10 2004)

Tested your code in Comeau and it didn't work. This, however, does:

bool matches ( const char*, const char* );
bool exact_match ( const char*, const char* );
inline bool matches( const char* argument, const char* word, bool exact );

void do_request( int * ch, char* argument )
{
   if( matches( argument, "cancel" ) )
   {}
   
   return;
} 

/* matches def */

bool matches ( const char*&, const char* ) {return true;}

bool exact_match ( const char*, const char* ) {return true;}

inline bool matches( const char* argument, const char* word, bool exact )
{
return( exact ? exact_match( argument, word )
: matches( argument, word ) );
}

int main()
{
  int a = 5;
  char c[7] = "cancel";
  c[6] = '\0';

  do_request(&a, c);

  return 0;
}

The problem indeed seems to be the use of the reference operator, although why, I'm not sure...

Aha! I got it! The compiler can't take a reference to an array of unknown size, which it assumes is what you're trying to do.


8. RE: C++ in Linux not being able to convert const Tue Jan 27, 2004 [2:38 AM]
Kastagaar
Email not supplied
member since: Jul 29, 1999
In Reply To
Reply
> if( matches( argument, "cancel" ) )

> no matching function for call to `matches(char*&, const
char[4])'


> candidates are: bool matches(const char*&, const char*)

Your problem is not with the consts.

It's with the "cancel". "cancel" is of type char[7], and that is not implicitly convertible to const char * in this context. Something to do with "r-values" and "character array literals", I'm sure. I've come across it before. Two possible solutions:


char const cancel[] = "cancel";
if( matches( argument, &cancel[0] ) )



char const *cancel = "cancel";
if( matches( argument, cancel ) )



(Comment added by Kastagaar on Tue Jan 27 5:18:41 2004)

By the way, the fact that your error message cites [4] and you're using [7] means that you're not posting the error message for the code you posted. But it's the same problem wherever it is.
There are two ways of constructing software: to make it so simple that there are obviously no errors, and to make it so complex that there are no obvious errors.


9. RE: C++ in Linux not being able to convert const Tue Jan 27, 2004 [9:00 AM]
muir
Email not supplied
member since: Sep 14, 2003
In Reply To
Reply
>It's with the "cancel". "cancel" is of type char[7], and that is not implicitly convertible to const char * in this context. Something to do with "r-values" and "character array literals", I'm sure. I've come across it before.

Interesting.. any idea why changing argument's type in the matching functions from char*& to char* compiles fine? I assumed it was due to not being able to take a reference to an array of an unknown size (which it seems to parse to.) I admit the error message is still a bit baffling -when match(char*&, const char*) is used, the error shows as 'no match for (char*&, char[7])', but when match(char*, const char*) is used, there's no problem (this in Comeau 4.3 and g++ 3.2.)

.

(Comment added by muir on Tue Jan 27 12:34:51 2004)

Can't seem to find any tangible references on the matter.. it seems that changing the ref-to-ptr to a simple ptr allows compilation, so I suppose the original problem is solved :)




[Previous] [Next] [Post] [Reply] [Topics] [Summary] [Search]