error trying to toupper string

S

sandy

I am trying to upper case a string, so I have this method:

string FileSystem::toupper(string S)
{
for (int i=0; i<S.length(); ++i)
{
S=toupper(S);
}

return S;
}

I get the errors:
invalid conversion from `char' to `const char*'

`std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const
_CharT*, const _Alloc&) [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>]'

cannot convert `std::string' to `char' in assignment

I did some looking on the Net and it appears I have done nothing new,
in fact it is almost character by character a match of 3 samples I
found on the net.

But of course... it doesn't compile.
 
B

BobR

(e-mail address removed) wrote in message ...
I am trying to upper case a string, so I have this method:

string FileSystem::toupper(string S){
// > for (int i=0; i<S.length(); ++i){
// > S=toupper(S);

for( size_t i(0); i < S.size(); ++i){ // fix compiler warning.
S[ i ] = std::toupper( S ); // call the right function.
}
return S;
}

I get the errors:
invalid conversion from `char' to `const char*'

`std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const
_CharT*, const _Alloc&) [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>]'

cannot convert `std::string' to `char' in assignment

You are calling 'FileSystem::toupper' inside 'FileSystem::toupper'. The
std::toupper will work. Here's what you are trying to do:

std::string S("Hello World");
char A;
A = S; // BZzzt!! Nope! Won'r fit!

That and your 'toupper' takes a string, 'std::toupper' takes a char/int.
That is a problem in nameing your functions with names used in the std
library.
A better name could be:

std::string FileSystem::ToUpper( std::string S ){/* */}
I did some looking on the Net and it appears I have done nothing new,
in fact it is almost character by character a match of 3 samples I
found on the net.

But of course... it doesn't compile.

My (old libc) docs say:
int toupper (int c) Function
If c is a lower-case letter, toupper returns the corresponding
upper-case letter. Otherwise c is returned unchanged.

....and <cctype> puts it in namespace std.

If the above does not fix your problem, try the following:

string FileSystem::toupper( string S ){
for( size_t i(0); i < S.size(); ++i){
S[ i ] = char( std::toupper( S[ i ] ) );
}
return S;
}

OK?
 
S

sandy

Thanks Bob.

It works, and I understand it... but I don't (how's that for confusing)

I understand that it was calling the wrong toupper... but since the
original toupper takes a char, and mine takes a string, I thought it
was like regular overloading and it would be taken care of...

Hmmm... Like I say, I now know what it was doing; I am just not sure
what that means about overloading.

I am a student and we have to overload pretty much everything in our
assignments (=, ==, <, > etc.) and I thought this would work the same
way...

Thanks.

I will have to think about this a bit.
(e-mail address removed) wrote in message ...
I am trying to upper case a string, so I have this method:

string FileSystem::toupper(string S){
// > for (int i=0; i<S.length(); ++i){
// > S=toupper(S);

for( size_t i(0); i < S.size(); ++i){ // fix compiler warning.
S[ i ] = std::toupper( S ); // call the right function.
}
return S;
}

I get the errors:
invalid conversion from `char' to `const char*'

`std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const
_CharT*, const _Alloc&) [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>]'

cannot convert `std::string' to `char' in assignment

You are calling 'FileSystem::toupper' inside 'FileSystem::toupper'. The
std::toupper will work. Here's what you are trying to do:

std::string S("Hello World");
char A;
A = S; // BZzzt!! Nope! Won'r fit!

That and your 'toupper' takes a string, 'std::toupper' takes a char/int.
That is a problem in nameing your functions with names used in the std
library.
A better name could be:

std::string FileSystem::ToUpper( std::string S ){/* */}
I did some looking on the Net and it appears I have done nothing new,
in fact it is almost character by character a match of 3 samples I
found on the net.

But of course... it doesn't compile.

My (old libc) docs say:
int toupper (int c) Function
If c is a lower-case letter, toupper returns the corresponding
upper-case letter. Otherwise c is returned unchanged.

...and <cctype> puts it in namespace std.

If the above does not fix your problem, try the following:

string FileSystem::toupper( string S ){
for( size_t i(0); i < S.size(); ++i){
S[ i ] = char( std::toupper( S[ i ] ) );
}
return S;
}

OK?
 
B

BobR

(e-mail address removed) wrote in message
Thanks Bob.

It works, and I understand it... but I don't (how's that for confusing)

I understand that it was calling the wrong toupper... but since the
original toupper takes a char, and mine takes a string, I thought it
was like regular overloading and it would be taken care of...
Hmmm... Like I say, I now know what it was doing; I am just not sure
what that means about overloading.

I am a student and we have to overload pretty much everything in our
assignments (=, ==, <, > etc.) and I thought this would work the same
way...
Thanks.
I will have to think about this a bit.

You could have overloaded 'toupper' in the class:

class ToUpTest{ public:
void Print(std::string str, std::eek:stream &sos){
sos<<toupper(str)<<std::endl;
return;
}
std::string toupper(std::string S){
// using std::toupper; // another way
for( size_t i(0); i < S.size(); ++i ){
S[ i ] = toupper(S[ i ]);
//S[ i ] = std::toupper(S[ i ]);
//S[ i ] = ::toupper(S[ i ]);
}
return S;
}

char toupper(char S){
return std::toupper( S );
}
// but, this is kind of silly since you can go direct.

};

I think you should bring up this problem in your programming class. Hopefully
the instructor will allow the other students to (try to) explain it. Lessons
learned that way seem to 'stick' better. <G>

This also works (re-name):

class ToUpTest{ public:
void Print(std::string str, std::eek:stream &sos){
sos<<ToUpper(str)<<std::endl;
return;
}
std::string ToUpper(std::string S){ // note rename
for( size_t i(0); i < S.size(); ++i){
S[ i ] = toupper(S[ i ]);
}
return S;
}
};

So why?

[ the following: I may be feeding you fact or fiction. I'm not trying to
confuse you. Just invoke some thoughts.]
// ------
Think about what happens when you overload (=, ==, <, > etc.). The
compiler would provide (some) defaults if you didn't overload, right? But,
when you overload, you take responsibility. Could that be it?

struct A{
int func( int ){ return 42;}
};
struct B{
double func( double ){ return 42.9;}
};

int num(0);
B b;
int num2 = b.func( num );

Would you expect A::func(int) to be called? Think what would happen if it
did. Could there ever be encapsulation?
// ------ <end single-brain-cell tyrannical tirade>

Well, run it around in your head for a while, but, before it gets to the
migraine level, come back here and we'll wring it out.
[ hopefully with the aid of some of the *real* experts. ]
 

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,575
Members
45,053
Latest member
billing-software

Latest Threads

Top