473,386 Members | 1,819 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,386 software developers and data experts.

Invoking constructor : Foo(var) and Foo ins(var)

Hi,

What is wrong with this code?

====== C++ code : File t.cpp : BEGIN ======
#include <iostream>
using namespace std;

struct Foo
{
Foo (int n) { cout << n << endl; }
};

int main ()
{
int i = 100; // Line#11

Foo (i); // Line#13 : Compilation error
Foo f (i); // Compiled with no errors

Foo (200); // Compiled with no errors
Foo (int(300)); // Compiled with no errors

return 0;
}
====== C++ code : File t.cpp : END ========
====== Compilation : BEGIN ======

$ g++ -v
[--omitted--]
gcc version 3.3.1 (cygming special)

$ g++ t.cpp

t.cpp: In function `int main()':
t.cpp:13: error: conflicting types for `Foo i'
t.cpp:11: error: previous declaration as `int i'

====== Compilation : END ========
=====================================
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html
=====================================

Jul 19 '05 #1
4 1737
Alex,
Foo(i ) means 'i' is declared a variable of type Foo.
but 'i' is already defined and C++ do now allow redeclaration.

Thanks
Avinash
"Alex Vinokur" <al****@bigfoot.com> wrote in message news:<bk************@ID-79865.news.uni-berlin.de>...
Hi,

What is wrong with this code?

====== C++ code : File t.cpp : BEGIN ======
#include <iostream>
using namespace std;

struct Foo
{
Foo (int n) { cout << n << endl; }
};

int main ()
{
int i = 100; // Line#11

Foo (i); // Line#13 : Compilation error
Foo f (i); // Compiled with no errors

Foo (200); // Compiled with no errors
Foo (int(300)); // Compiled with no errors

return 0;
}
====== C++ code : File t.cpp : END ========
====== Compilation : BEGIN ======

$ g++ -v
[--omitted--]
gcc version 3.3.1 (cygming special)

$ g++ t.cpp

t.cpp: In function `int main()':
t.cpp:13: error: conflicting types for `Foo i'
t.cpp:11: error: previous declaration as `int i'

====== Compilation : END ========
=====================================
Alex Vinokur
mailto:al****@connect.to
http://mathforum.org/library/view/10978.html
=====================================

Jul 19 '05 #2


Avinash wrote:

Alex,
Foo(i ) means 'i' is declared a variable of type Foo.

No it doe not.
It means to create an unnamed temporary object, initialize it with i
and destroy it afterwards.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #3

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...


Avinash wrote:

Alex,
Foo(i ) means 'i' is declared a variable of type Foo.


No it doe not.
It means to create an unnamed temporary object, initialize it with i
and destroy it afterwards.


That doesn't help the OP with his compiler error msg.
Jul 19 '05 #4

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:3F***************@gascad.at...


Avinash wrote:

Alex,
Foo(i ) means 'i' is declared a variable of type Foo.

No it doe not.
It means to create an unnamed temporary object, initialize it with i
and destroy it afterwards.


No actually, Avinash is right. Syntactically this is ambiguous between
being a declaration (Avinash) and an expression (Karl). The semantic
rules say the declaration wins out. Foo (i) is the same as Foo i in this
case.
Jul 19 '05 #5

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

Similar topics

7
by: Andy Fish | last post by:
Hi, I'm trying to figure out how to invoke a method on an object given the object and the method name. For example, here's a simple object with a method: function MyObj (pName, pDesc) {...
12
by: Jack | last post by:
Hi, Is it recommended to use the var keyword when declaring a variable? IE lets me create a variable with var, then create the same one again with no problem. Also it lets me create a...
4
by: Andrew Poulos | last post by:
If I do something like this: Cnst = function() { // blah } Cnst.prototype.meth = function() { // blah } var foo = new Cnst()
6
by: Rufnex | last post by:
Hi, is there a delete for a object inside the constructor, while i init it? i will try something like that: var obj = function(a) { if (!a) delete this; this.a = a; }
31
by: Peter Michaux | last post by:
Hi, I want to know the name of an object's constructor function as a string. Something like this <script type="text/javascript"> function Foo(){}; var a = new Foo(); alert('"' +...
12
by: satyajit | last post by:
I am trying to learn the concept of constructors in ECMAScript. I executed following code (See execution in Rhino JavaScript shell): function Foo(a) { this.a = a; } function Bar(b) { this.b...
53
by: Sanders Kaufman | last post by:
I'm having a BLAST writing this app as OOP as PHP allows. It's one thing to discuss the dance, but it's a thing of beauty to see the performance. I'm noticing that the constructor is a "reset"...
30
by: kj | last post by:
My book (Flanagan's JavaScript: The Definitive Guide, 5th ed.) implies on page 111 that the following two constructs are equivalent: ( x.constructor == Foo ) and ( x instanceof Foo ) The...
14
by: Dan Rumney | last post by:
I've been taking a look at Douglas Crockford's JSLint. One of the conventions that it expects is that arrays be created using literal notation var arr1 = ; as opposed to using a constructor...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.