c++ puzzle

N

Nan Li

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;
}
 
M

MC felon

Nan said:
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,
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
 
N

Nan Li

MC said:
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.
 
M

MC felon

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.
 
S

Stuart Golodetz

MC felon said:
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...
 
M

MC felon

and i thought

int main()
{
\\something


return 0;
}

and

void main()
{
\\something
}

mean the same here
 
L

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.
 
L

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.
 
M

MC felon

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.
 
L

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.
 
Z

Zara

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
 
E

Earl Purple

Nan said:
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 ) );
 
R

red floyd

MC said:
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.
 
F

Frederick Gotham

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);
}
 
G

Gavin Deane

MC said:
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
 

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

Similar Threads

Lexical Analysis on C++ 1
Boomer trying to learn coding in C and C++ 6
C language. work with text 3
generics puzzle 57
Organization Assignment in C programming 0
C pipe 1
React+Redux+RTK 0
React+Redux+RTK 1

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top