473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

c++ puzzle

I have questions about the following code. What does the error line try
to declare ? If the next line can succeed, why does the first one fail
? Thanks a lot.

class A {
public:
A( char const * ) {}
};

int main()
{
char const * a = "hello";
A(a); //error
A( (char const *)a ); //ok
return 0;
}

Oct 4 '06 #1
16 1968

Nan Li wrote:
I have questions about the following code. What does the error line try
to declare ? If the next line can succeed, why does the first one fail
? Thanks a lot.

class A {
public:
A( char const * ) {}
};

int main()
{
char const * a = "hello";
A(a); //error
A( (char const *)a ); //ok
return 0;
}
here,
>>A( char const * )
i tried giving it a name, like 'a'

A(char const* a) {}

and then in main, i just :

char const* p;
A(p);
it works perfectly

Oct 4 '06 #2

MC felon wrote:
Nan Li wrote:
I have questions about the following code. What does the error line try
to declare ? If the next line can succeed, why does the first one fail
? Thanks a lot.

class A {
public:
A( char const * ) {}
};

int main()
{
char const * a = "hello";
A(a); //error
A( (char const *)a ); //ok
return 0;
}

here,
>>A( char const * )
i tried giving it a name, like 'a'

A(char const* a) {}

and then in main, i just :

char const* p;
A(p);
it works perfectly
What compiler did you use? I use g++. With your change, I still got the
same old results. I don't think adding a formal parameter in the
signature will change things.

It seems the error line A(a) here is actually the same as 'A a'. I
also tried 'A (a)', 'A(((a)))', ... they are all the same thing.

Oct 4 '06 #3
i dont know what you're talking about. the code is something that's
universally accepted in all compilers of c++.it's a plain code of OOP
that uses no libraries. no matter what compiler you use, it'll be the
same. enter exactly this code:
class a
{
public:
a(char const* a) {}
};

void main()
{
char const* pic;
a(pic);
}

if it doesn't work, i'll dye my hair pink and send you a pic of it.

Oct 4 '06 #4
"MC felon" <pa******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>i dont know what you're talking about. the code is something that's
universally accepted in all compilers of c++.it's a plain code of OOP
that uses no libraries. no matter what compiler you use, it'll be the
same. enter exactly this code:
class a
{
public:
a(char const* a) {}
};

void main()
{
char const* pic;
a(pic);
}

if it doesn't work, i'll dye my hair pink and send you a pic of it.
It seems to be code that's universally rejected on ever compiler I've just
tried it on. Here's what the Comeau compiler said:

"ComeauTest.c", line 9: error: "a" has already been declared in the current
scope
A(a); //error
^

"ComeauTest.c", line 9: error: no default constructor exists for class "A"
A(a); //error
^

"ComeauTest.c", line 10: error: no suitable conversion function from "A" to
"const char *" exists
A( (char const *)a ); //ok
^

"ComeauTest.c", line 8: warning: variable "a" was declared but never
referenced
char const * a = "hello";
^

3 errors detected in the compilation of "ComeauTest.c".

I think you may be going round with pink hair for a while :)

Stu

P.S. It's "int main()"! :) No, seriously...
Oct 4 '06 #5
but it works in TC++...

Oct 4 '06 #6

and i thought

int main()
{
\\something
return 0;
}

and

void main()
{
\\something
}

mean the same here

Oct 4 '06 #7
lwz
class a
{
public:
a(const char *A){;}
};
int main()
{
const char *pic;
a x(pic);
return 0;
}

The above code *should* work, (I tried it in Dev-Cpp, which uses g++),
and anyway you cannot call a function that is of a class just like that
without defining any object (of course of that class) at all... classes
are like bases for objects... (objects having the same declaration as a
class, just that you can have many such objects at once like a
x("a"),y("s"),z("d");
but you can call x=new a(pic); if i'm not mistaken... correct me if i'm
wrong.

Stuart Golodetz wrote:
"MC felon" <pa******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
i dont know what you're talking about. the code is something that's
universally accepted in all compilers of c++.it's a plain code of OOP
that uses no libraries. no matter what compiler you use, it'll be the
same. enter exactly this code:
class a
{
public:
a(char const* a) {}
};

void main()
{
char const* pic;
a(pic);
}

if it doesn't work, i'll dye my hair pink and send you a pic of it.

It seems to be code that's universally rejected on ever compiler I've just
tried it on. Here's what the Comeau compiler said:

"ComeauTest.c", line 9: error: "a" has already been declared in the current
scope
A(a); //error
^

"ComeauTest.c", line 9: error: no default constructor exists for class "A"
A(a); //error
^

"ComeauTest.c", line 10: error: no suitable conversion function from "A" to
"const char *" exists
A( (char const *)a ); //ok
^

"ComeauTest.c", line 8: warning: variable "a" was declared but never
referenced
char const * a = "hello";
^

3 errors detected in the compilation of "ComeauTest.c".

I think you may be going round with pink hair for a while :)

Stu

P.S. It's "int main()"! :) No, seriously...
Oct 4 '06 #8
lwz
And yes, in Turbo-C++ you can use void main... but not in other
compilers as they require a return value... if you would notice, in
Turbo-C++ they also give you a warning when you do that... just that in
other compilers it gives an error.

MC felon wrote:
and i thought

int main()
{
\\something
return 0;
}

and

void main()
{
\\something
}

mean the same here
Oct 4 '06 #9
oh... yeahh, i meant to include naming the object..
so instead of

a(pic);

it's
a object_of_a(pic);
sorry!
anyway, it'll work now or i'll die my hair red.

Oct 4 '06 #10
lwz
I just checked the last statement out (the new statement)...

a *x;
x=new a(pic);

The above should work also. Just that call members of the class using
the -operator, since x is a pointer to the object.

lwz wrote:
class a
{
public:
a(const char *A){;}
};
int main()
{
const char *pic;
a x(pic);
return 0;
}

The above code *should* work, (I tried it in Dev-Cpp, which uses g++),
and anyway you cannot call a function that is of a class just like that
without defining any object (of course of that class) at all... classes
are like bases for objects... (objects having the same declaration as a
class, just that you can have many such objects at once like a
x("a"),y("s"),z("d");
but you can call x=new a(pic); if i'm not mistaken... correct me if i'm
wrong.

Stuart Golodetz wrote:
"MC felon" <pa******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
>i dont know what you're talking about. the code is something that's
universally accepted in all compilers of c++.it's a plain code of OOP
that uses no libraries. no matter what compiler you use, it'll be the
same. enter exactly this code:
>
>
class a
{
public:
a(char const* a) {}
};
>
void main()
{
char const* pic;
a(pic);
}
>
>
>
if it doesn't work, i'll dye my hair pink and send you a pic of it.
It seems to be code that's universally rejected on ever compiler I've just
tried it on. Here's what the Comeau compiler said:

"ComeauTest.c", line 9: error: "a" has already been declared in the current
scope
A(a); //error
^

"ComeauTest.c", line 9: error: no default constructor exists for class "A"
A(a); //error
^

"ComeauTest.c", line 10: error: no suitable conversion function from "A" to
"const char *" exists
A( (char const *)a ); //ok
^

"ComeauTest.c", line 8: warning: variable "a" was declared but never
referenced
char const * a = "hello";
^

3 errors detected in the compilation of "ComeauTest.c".

I think you may be going round with pink hair for a while :)

Stu

P.S. It's "int main()"! :) No, seriously...
Oct 4 '06 #11
On 3 Oct 2006 23:15:17 -0700, "Nan Li" <na******@gmail.comwrote:
>I have questions about the following code. What does the error line try
to declare ? If the next line can succeed, why does the first one fail
? Thanks a lot.

class A {
public:
A( char const * ) {}
};

int main()
{
char const * a = "hello";
A(a); //error
A( (char const *)a ); //ok
return 0;
}
It may be a surprise to you, but your error line is equivalent to:

A a;

So you are defining a variable a of type A (which has already been
defined, and with other type), and initialising it via the default
contructor,(which is undefined). Two errors (at least!) in one line.

Instead, the other line is telling the compiuler to create a temporary
of type A, initializing it with the constructor declared for const
char *, and then discard it. There is no error, but it is useless as
such class and such constructor have no side effects.

Best regards,

Zara
Oct 4 '06 #12

"MC felon" <pa******@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
but it works in TC++...
This is not a chat room! How about quoting what you're responding to, eh?

-Howard

Oct 4 '06 #13

Nan Li wrote:
I have questions about the following code. What does the error line try
to declare ? If the next line can succeed, why does the first one fail
? Thanks a lot.

class A {
public:
A( char const * ) {}
};

int main()
{
char const * a = "hello";
A(a); //error
You are trying to create a temporary of type A but the compiler doesn't
think you are. Bind it to a const reference and the line will work so

const A& temp = A(a);
A( (char const *)a ); //ok
return 0;
}
If you don't want the A to live even as long as temp for whatever
reason you can create a dummy function:

void do_nothing( const A& a );

do_nothing( A( a ) );

Oct 4 '06 #14
MC felon wrote:
but it works in TC++...
so TC++ is "all compilers of c++"?

The OP's problem is that it's actually declaring a second variable named
"a" of type A. The problem is that he's already got a variable named "a".

The compiler will do a lot of stripping of parens on declarations.
Google for "most vexing parse", to see the canonical/extreme example.
Oct 4 '06 #15
Nan Li posted:
char const * a = "hello";
A(a); //error

As someone else has already pointed out, the error line is interpretted as:

A a;

, which of course is a definition of an object. Two remedies:

1.
(A)a;

2.
(void)A(a);

If you fathom in any way that it could possibly be a declaration, then
it's a declaration.

class MyClass {
public:

MyClass() {}

MyClass(int) {}

MyClass(char,int*,double) {}
};

int main()
{
int a;

MyClass(); /* No problem! */

MyClass(4); /* No problem! */

MyClass(a+2); /* No problem! */

MyClass(a); /* ERROR: This is a declaration! */

(MyClass)a; /* No problem! */

(void)MyClass(a);
}

--

Frederick Gotham
Oct 4 '06 #16

MC felon wrote:
and i thought

int main()
{
\\something
return 0;
}

and

void main()
{
\\something
}

mean the same here
No.

int main()
{
// something
return 0;
}

and

int main()
{
// something
}

mean the same thing. If you leave out the return statement at the end
of main, the compiler inserts an implicit return 0;

void main()
{
// anything
}

is not correct C++. Any compiler that accepts it (and many do) is doing
so as a non-standard extension to the language.

Gavin Deane

Oct 4 '06 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Developwebsites | last post by:
Hi all, I've made a sliding puzzle game in shockwave which works just fine, except I dont know how to have it solve itself. the URL is: http://members.aol.com/rglukov/games/selfsolve.htm ...
42
by: Frank Buss | last post by:
I've setup a challenge, mainly for C++, Java and Lisp, but every other language is welcome: http://www.frank-buss.de/challenge/index.html There is nothing to win, but I hope there will be some...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
6
by: Phoe6 | last post by:
Hi All, I would like to request a code and design review of one of my program. n-puzzle.py http://sarovar.org/snippet/detail.php?type=snippet&id=83 Its a N-puzzle problem solver ( Wikipedia page...
2
by: Gio | last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio ...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
5
by: dmf1207 | last post by:
Hi All! I'm new to javascript and need a little help with a simple puzzle im trying to design. I have a 600x100 pixel picture that I have sliced into 6 100x100 rectangles making a table of of 6...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.