Syntax issue (string, QString) - invoking constructor?

M

Micha

Hi,

i am trying to get to run this piece of code.

#include <qstring.h>
//:1
#include <list>
//:2
#include <algorithm>
//:3
#include <iostream> //:4
//:5
void myTestF(const std::string& str)
{ //:6
//:7
std::string val1( std::string(str) ); //:8
std::string val2( std::string("123") ); //:9
//:10
char * testChar = NULL; //:11
std::string val3( testChar ); //:12
//:13
QString qStr(str); //:14
//:15
std::string val4( qStr.ascii() ); //:16
//:17
std::string val5( std::string( QString(str).ascii() ) ); //:18
//:19
} //:20
//:21
int main() { //:22
std::cout << "hello World!\n"; //:23
return 0; //:24
} //:25

G++ 3.3.2 won't accept line #18, commenting out this line makes the
whole
program compilable. Starting from line #18 i tried to decompose this
line into
it's statements in order to encircle the issue - which you can see in
the lines 8-16.
Lines 8-16 are accepted, line 18 is rejected. Why? ;-)

thanks, Micha

PS : this piece of code will be accepted by aCC (hp-compiler),
currently i am porting
a program from aCC to gcc - this is how I got stuck on this. On some
web-page i read,
that aCC is kept anso-compatible. Mhh i ran my gcc with the -ansi flag
- it didn't help.
Will the current gcc compile the program? (can't install one myself
here)
 
I

Ioannis Gyftos

std::string val5( std::string( QString(str).ascii() ) ); //:18

I think I had stumbled across something like this, where I wanted to
debug-dump some QStrings somewhere. If memory serves well, try this:

std::string val5( std::string( (const char*)QString(str) ) );

I cannot compile against Qt at the moment to make sure, sorry.

And, well, although you know better, I have to point out that QString
is more 'powerful' than std::string, and ask if you are really sure
you need/want such a conversion (certainly for a Qt project you'd want
to work with QStrings?)
 
C

Cholo Lennon

Hi,

i am trying to get to run this piece of code.

#include <qstring.h>
//:1
#include <list>
//:2
#include <algorithm>
//:3
#include <iostream> //:4
//:5
void myTestF(const std::string& str)
{ //:6
//:7
std::string val1( std::string(str) ); //:8
std::string val2( std::string("123") ); //:9
//:10
char * testChar = NULL; //:11
std::string val3( testChar ); //:12
//:13
QString qStr(str); //:14
//:15
std::string val4( qStr.ascii() ); //:16
//:17
std::string val5( std::string( QString(str).ascii() ) ); //:18
//:19} //:20

//:21
int main() { //:22
std::cout << "hello World!\n"; //:23
return 0; //:24

} //:25

G++ 3.3.2 won't accept line #18, commenting out this line makes the
whole
program compilable. Starting from line #18 i tried to decompose this
line into
it's statements in order to encircle the issue - which you can see in
the lines 8-16.
Lines 8-16 are accepted, line 18 is rejected. Why? ;-)

thanks, Micha

PS : this piece of code will be accepted by aCC (hp-compiler),
currently i am porting
a program from aCC to gcc - this is how I got stuck on this. On some
web-page i read,
that aCC is kept anso-compatible. Mhh i ran my gcc with the -ansi flag
- it didn't help.
Will the current gcc compile the program? (can't install one myself
here)

Can you post the compiler error message?
BTW, line 12 will fail at runtime. std::string doesn't work with null
pointers.

Regards
 
E

Erik Wikström

On 2007-11-22 14:15, Micha wrote:

I can not help you with you problem, but are you aware that you are
creating unnecessary temporary objects?
std::string val1( std::string(str) );

std::string val1(str);
std::string val2( std::string("123") );

std::string val2("123");
std::string val5( std::string( QString(str).ascii() ) );

std::string val5( QString(str).ascii() );
 
T

Tadeusz B. Kopec

On 2007-11-22 14:15, Micha wrote:

I can not help you with you problem, but are you aware that you are
creating unnecessary temporary objects?


std::string val1(str);

His statement seems to me a bigger problem than unnecessary temporary
object. Isn't it a function declaration? At least my gcc interprets it so.
 
E

Erik Wikström

His statement seems to me a bigger problem than unnecessary temporary
object. Isn't it a function declaration? At least my gcc interprets it so.

Visual Studio said the same thing, butt adding an extra pair of
parentheses solved that. Though I am not sure what the standard says
about it.
 
M

Micha

Tanks for the answers so far. I know, the piece of code I've posted
so far makes little sense and is not elegant or efficient at all. I
used it to encircle my problem. Which occured while porting a bigger
project from aCC,hp-ux to gcc,solaris.
My hope was that I could pass some magic switches to gcc and make it
comppile the code without manual patching - which could end up with
lots of stupid work for me.

Besides that I got interested in the problem itself.
The (shortened) original code is :

#include <qstring.h>
#include <iostream>

std::string someFunction(const std::string& str)
{
std::string val(std::string(QString(str).stripWhiteSpace().ascii()));

std::replace(val.begin(),val.end(), '/', '_');

return(val);
}


int main() {
std::cout << "hello World!\n";
return 0;
}

And the gcc's output is :
g++ -c -Wall -W -O2 -DQT_NO_DEBUG -DQT_SHARED -DQT_THREAD_SUPPORT -I/
home2/eng/qt/mkspecs/solaris-g++ -I. -I.. -I/home2/eng/qt/include -o
testIssue2.o testIssue2.cpp
testIssue2.cpp: In function `std::string someFunction(const
std::string&)':
testIssue2.cpp:6: error: parse error before `.' token
testIssue2.cpp:8: error: request for member `begin' in `val(...)',
which is of
non-aggregate type `std::string ()(...)'
testIssue2.cpp:8: error: request for member `end' in `val(...)', which
is of
non-aggregate type `std::string ()(...)'
testIssue2.cpp:10: error: conversion from `std::string (*)(...)' to
non-scalar
type `std::string' requested
*** Error code 1

As I understood it so far - gcc interprets

std::string
val(std::string(QString(str).stripWhiteSpace().ascii()));

as a function instead of a variable definition which was clearly ment
here - at least for the aCC ;-).

1) Any chances to make it compile with a current gcc without manual
patching?
2) Any corrections to my theory?
 
E

Erik Wikström

Tanks for the answers so far. I know, the piece of code I've posted
so far makes little sense and is not elegant or efficient at all. I
used it to encircle my problem. Which occured while porting a bigger
project from aCC,hp-ux to gcc,solaris.
My hope was that I could pass some magic switches to gcc and make it
comppile the code without manual patching - which could end up with
lots of stupid work for me.

Besides that I got interested in the problem itself.
The (shortened) original code is :

#include <qstring.h>
#include <iostream>

std::string someFunction(const std::string& str)
{
std::string val(std::string(QString(str).stripWhiteSpace().ascii()));

std::replace(val.begin(),val.end(), '/', '_');

return(val);
}


int main() {
std::cout << "hello World!\n";
return 0;
}

And the gcc's output is :

g++ -c -Wall -W -O2 -DQT_NO_DEBUG -DQT_SHARED -DQT_THREAD_SUPPORT -I/
home2/eng/qt/mkspecs/solaris-g++ -I. -I.. -I/home2/eng/qt/include -o
testIssue2.o testIssue2.cpp
testIssue2.cpp: In function `std::string someFunction(const
std::string&)':
testIssue2.cpp:6: error: parse error before `.' token
testIssue2.cpp:8: error: request for member `begin' in `val(...)',
which is of
non-aggregate type `std::string ()(...)'
testIssue2.cpp:8: error: request for member `end' in `val(...)', which
is of
non-aggregate type `std::string ()(...)'
testIssue2.cpp:10: error: conversion from `std::string (*)(...)' to
non-scalar
type `std::string' requested
*** Error code 1

As I understood it so far - gcc interprets

std::string
val(std::string(QString(str).stripWhiteSpace().ascii()));

as a function instead of a variable definition which was clearly ment
here - at least for the aCC ;-).

1) Any chances to make it compile with a current gcc without manual
patching?

As VC++2005 does the same thing I would guess that gcc is correct in its
behaviour. In Vc++2005 the following will not compile:

std::string val1(std::string(str));

but this does:

std::string val1((std::string(str)));

You should be able to do the same thing, i.e.:

val((std::string(QString(str).stripWhiteSpace().ascii())));
 
C

Cholo Lennon

As VC++2005 does the same thing I would guess that gcc is correct in its
behaviour. In Vc++2005 the following will not compile:

std::string val1(std::string(str));

but this does:

std::string val1((std::string(str)));

You should be able to do the same thing, i.e.:

val((std::string(QString(str).stripWhiteSpace().ascii())));

With Comeau Online Compiler (http://www.comeaucomputing.com/tryitout/)
the following code compile without errors:

Code:
#include <string>

void test(const std::string& str)
{
  std::string val1(std::string(str));
  std::string val2((std::string(str)));
}

I try changing some compiler options, but in all cases the
compilation was successful.

Regards
 
T

Tadeusz B. Kopec

Tanks for the answers so far. I know, the piece of code I've posted so
far makes little sense and is not elegant or efficient at all. I used it
to encircle my problem. Which occured while porting a bigger project
from aCC,hp-ux to gcc,solaris. My hope was that I could pass some magic
switches to gcc and make it comppile the code without manual patching -
which could end up with lots of stupid work for me.

Besides that I got interested in the problem itself. The (shortened)
original code is :

#include <qstring.h>
#include <iostream>

std::string someFunction(const std::string& str) {
std::string val(std::string(QString(str).stripWhiteSpace().ascii ()));

std::replace(val.begin(),val.end(), '/', '_');

return(val);
}


int main() {
std::cout << "hello World!\n";
return 0;
}

And the gcc's output is :

g++ -c -Wall -W -O2 -DQT_NO_DEBUG -DQT_SHARED -DQT_THREAD_SUPPORT -I/
home2/eng/qt/mkspecs/solaris-g++ -I. -I.. -I/home2/eng/qt/include -o
testIssue2.o testIssue2.cpp
testIssue2.cpp: In function `std::string someFunction(const
std::string&)':
testIssue2.cpp:6: error: parse error before `.' token testIssue2.cpp:8:
error: request for member `begin' in `val(...)', which is of
non-aggregate type `std::string ()(...)'
testIssue2.cpp:8: error: request for member `end' in `val(...)', which
is of
non-aggregate type `std::string ()(...)'
testIssue2.cpp:10: error: conversion from `std::string (*)(...)' to
non-scalar
type `std::string' requested
*** Error code 1

As I understood it so far - gcc interprets

std::string
val(std::string(QString(str).stripWhiteSpace().ascii()));

as a function instead of a variable definition which was clearly ment
here - at least for the aCC ;-).

1) Any chances to make it compile with a current gcc without manual
patching?
2) Any corrections to my theory?

AFAIK anything that can be parsed as a function declaration is a function
declaration. But your expression

std::string val(std::string(QString(str).stripWhiteSpace().ascii()));

can't be parsed as a function declaration, so it should be treated as
intended. I don't have QT at home, I tried to make small class QString
with methods used here and your code compiled.

But anyway

std::string val(QString(str).stripWhiteSpace().ascii());

is better.

BTW does QString have a constructor from std::string? I always used
str.c_str() when converted strings to QStrings.
 
T

Tadeusz B. Kopec

As VC++2005 does the same thing I would guess that gcc is correct in
its behaviour. In Vc++2005 the following will not compile:

std::string val1(std::string(str));

but this does:

std::string val1((std::string(str)));
[snip]
With Comeau Online Compiler (http://www.comeaucomputing.com/tryitout/)
the following code compile without errors:

Code:
#include <string>

void test(const std::string& str)
{
std::string val1(std::string(str));
std::string val2((std::string(str)));
}

I try changing some compiler options, but in all cases the compilation
was successful.

I guess Erik just abbreviated too much. Code:

std::string val1(std::string(str));
std::string val2((std::string(str)));

should compile, but the problem is, that val1 is a function although it
was intended to be a string (as val2 is).
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top