Qt Error help with char and const *char

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 3383 Views - Last Post: 05 March 2013 - 05:33 PM Rate Topic: -----

#1 ZacCarlson   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Qt Error help with char and const *char

Posted 03 March 2013 - 07:19 PM

I have the following error with Qt. Can anybody help me with these Qt function errors and how to fix it?

error: request for member 'begin' in 'answer', which is of non-class type 'char*'
error: request for member 'end' in 'answer', which is of non-class type 'char*'
error: invalid conversion from 'char' to 'const char*'


QString qStr = plainTextBox -> text();
QByteArray qba = qStr.toAscii();
char *answer = qba.data();
for (std::string::iterator it = answer.begin(); it != answer.end(); ++it){
     if (isalpha(*it)){
          *it = tolower(*it);
          *it = ((a*(*it -'a')+B)/>/>/>/> % 26)+'a';
          *it = toupper(*it);
          qStr = QString::fromAscii(*it);
          encodedTextBox -> setText(qStr);
      }
}


This post has been edited by ZacCarlson: 03 March 2013 - 07:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Qt Error help with char and const *char

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2619
  • View blog
  • Posts: 4,175
  • Joined: 21-June 11

Re: Qt Error help with char and const *char

Posted 03 March 2013 - 07:57 PM

As the error message says, answer is a char pointer, so you can't call methods on it. A std::string::iterator is something you use with an std::string. You don't have an std::string in your code - you have a char pointer.

The third error is about the fact that *it has type char, but fromAscii expects a C string (i.e. a char pointer) - not a single char.
Was This Post Helpful? 0
  • +
  • -

#3 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Qt Error help with char and const *char

Posted 03 March 2013 - 08:18 PM

You are using a std::string::iterator you could use a QString::iterator, and then iterate through the qStr.

Are you trying to do anything specific with your code?
Was This Post Helpful? 0
  • +
  • -

#4 ZacCarlson   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Qt Error help with char and const *char

Posted 03 March 2013 - 08:24 PM

I'm going through each letter in the answer from the input and encoding each letter. I have it working properly using just c++ code but now am trying to convert it to run through Qt based GUI. So you are saying I can use QString::iterator the same way as I was doing the other iterator. Can you write out an example similar to my code?

This post has been edited by ZacCarlson: 03 March 2013 - 08:30 PM

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,454
  • Joined: 05-May 12

Re: Qt Error help with char and const *char

Posted 03 March 2013 - 09:26 PM

Why don't you try it out and see what results you get? Otherwise, your previous request is really bordering on "gimme the codez".
Was This Post Helpful? 0
  • +
  • -

#6 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Qt Error help with char and const *char

Posted 03 March 2013 - 09:34 PM

Update the editbox outside the for loop.

I'll let you try and work out the for expressions :-

QString qStr = plainTextBox->text();
qStr =  qStr.toUpper();

for (/* what do you think the code will be here? */)
{
  if (isalpha(*it))
  {
    *it = ( ( a * (*it -'A') + b ) % 26 ) + 'A';
  }
}

// Update textbox outside for loop
encodedTextBox->setText(qStr);



Was This Post Helpful? 0
  • +
  • -

#7 ZacCarlson   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Qt Error help with char and const *char

Posted 03 March 2013 - 09:40 PM

View PostSkydiver, on 04 March 2013 - 04:26 AM, said:

Why don't you try it out and see what results you get? Otherwise, your previous request is really bordering on "gimme the codez".


Yes, I know to try it out myself but I was wondering if there was skeleton type code. I have tried it out and still having issues. I'll post on here in a bit what they are. Thanks.
Was This Post Helpful? 0
  • +
  • -

#8 ZacCarlson   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Qt Error help with char and const *char

Posted 03 March 2013 - 10:15 PM

Duke,
Here are my errors with the code I've updated. I also don't understand the setText(qStr) you suggested. Does the *it get passed into that function automatically?

In member function 'void Cypher::encryptClicked()':
error: invalid conversion from 'QChar*' to 'int'
error: initializing argument 1 of 'int isalpha(int)'
error: no match for 'operator-' in '* it - 'A''
The errors are from the isalpha loop....
void Cypher::encryptClicked()
{
        QString qStr = plainTextBox -> text();
        qStr = qStr.toUpper();
        for (QString::iterator *it = qStr.begin(); *it != qStr.end(); ++it){
                if (isalpha(*it)){
                        it = ((a*(*it - 'A') + B)/> % 26) + 'A';
                }
        }
        decodedTextBox -> setText(qStr);



Was This Post Helpful? 0
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,454
  • Joined: 05-May 12

Re: Qt Error help with char and const *char

Posted 03 March 2013 - 10:51 PM

Perhaps I'm misreading the Qt documentation, but my reading of it is that QString::begin() returns a QString::iterator, not a QString::iterator *. That maybe one of your problems right there.
Was This Post Helpful? 0
  • +
  • -

#10 ZacCarlson   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Qt Error help with char and const *char

Posted 03 March 2013 - 11:03 PM

I think you're right as I found documentation explaining that. However, I have this error with this code:
invalid operands of types 'int' and 'QChar*' to binary 'operator*'
So how do I fix it?

for (QString::iterator it = qStr.begin(); it != qStr.end(); ++it){
                //if (isalpha(it)){
                        it = ((a*(it - 'A') + B)/> % 26) + 'A';
                //}
        }


Was This Post Helpful? 0
  • +
  • -

#11 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Qt Error help with char and const *char

Posted 03 March 2013 - 11:15 PM

View PostZacCarlson, on 04 March 2013 - 07:15 AM, said:

Here are my errors with the code I've updated. I also don't understand the setText(qStr) you suggested. Does the *it get passed into that function automatically?


A string is a collection of characters.
The QString qStr is an object by itself, the iterator it returns each character *it in the string one at a time. The character is of a type QChar. You are using = to assign the character back to the string.


View PostZacCarlson, on 04 March 2013 - 07:15 AM, said:

In member function 'void Cypher::encryptClicked()':
error: invalid conversion from 'QChar*' to 'int'
error: initializing argument 1 of 'int isalpha(int)'
error: no match for 'operator-' in '* it - 'A''
The errors are from the isalpha loop....


It seems you will need to convert QChar to a char and back at some point. QChar has a toAscii function to create a char, and should implicitly convert in the reverse direction.

The for expression is not quite correct see the string example string::begin.

void Cypher::encryptClicked()
{
  QString qStr = plainTextBox -> text();
  qStr = qStr.toUpper();

  for ( /* almost correct */) {
    if ( (*it).isLetter() ) {
      // create a char 
      /* what do you think here? */

      // process char - something like
      ch = ((a*(ch - 'A') + b ) % 26 ) + 'A';

      // convert char to QChar
      /* what do you think here? */
    }
  }

  decodedTextBox -> setText(qStr);
}


Was This Post Helpful? 0
  • +
  • -

#12 ZacCarlson   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Qt Error help with char and const *char

Posted 03 March 2013 - 11:56 PM

I don't see my mistake with the iterator function? I changed string to qstring, similar to the link you gave me, so I don't know? I also can't find any good sources for how to convert a char to QChar so I need your help? Here is what I think is the code (minus the char to QChar):
void Cypher::encryptClicked()
{
        QString qStr = plainTextBox -> text();
        qStr = qStr.toUpper();
        for (QString::iterator it = qStr.begin(); it != qStr.end(); ++it){
                if ((*it).isLetter()){
                        QByteArray qba = *it.toAscii();
                        qba  = ((a*(it - 'A') + B)/>/> % 26) + 'A';

                }
        }
        decodedTextBox -> setText(qStr);


This post has been edited by ZacCarlson: 03 March 2013 - 11:57 PM

Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,454
  • Joined: 05-May 12

Re: Qt Error help with char and const *char

Posted 04 March 2013 - 06:39 AM

ZacCarlson: Did you even take time to read the Qt QString documentation, or are you just throwing code at the screen to see what sticks?
http://qt-project.or....8/qstring.html

It even has a section about converting to and from 8-bit strings (aka ASCII) and Unicode.
Was This Post Helpful? 1
  • +
  • -

#14 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Qt Error help with char and const *char

Posted 04 March 2013 - 03:01 PM

View PostZacCarlson, on 04 March 2013 - 08:56 AM, said:

I don't see my mistake with the iterator function?


It is correct now you have changed after Skydiver queried it.


View PostZacCarlson, on 04 March 2013 - 08:56 AM, said:

I also can't find any good sources for how to convert a char to QChar so I need your help?


Don't confuse the QString class and the QChar class.
QString has a toAscii() member function which returns a QByteArray.
QChar has a toAscii() member function which returns a char.

QChar


Sometimes data types can be automatically changed from one to another (implicit type conversion).

C++ Standard (SC22-N-4411.pdf) section 12.3.4 said:

1 Type conversions of class objects can be specified by constructors and by conversion functions. These conversions are called user-defined conversions and are used for implicit type conversions (Clause 4), for initialization (8.5), and for explicit type conversions (5.4, 5.2.9).


You might need to update your understanding of classes.



View PostSkydiver, on 04 March 2013 - 03:39 PM, said:

http://qt-project.or....8/qstring.html

It even has a section about converting to and from 8-bit strings (aka ASCII) and Unicode.


I had a look at that before and discounted it. It doesn't address the issue of individual character conversion.

So to paraphrase yourself :
Skydiver: Did you even take time to read the previous posts, or are you just throwing text at the screen?
Was This Post Helpful? 1
  • +
  • -

#15 ZacCarlson   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 146
  • Joined: 08-October 12

Re: Qt Error help with char and const *char

Posted 04 March 2013 - 03:16 PM

So to paraphrase yourself :
Skydiver: Did you even take time to read the previous posts, or are you just throwing text at the screen?
[/quote]

I don't understand this comment "so to paraphrase yourself". What does it mean? Who is it directed toward?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2