Hi Everyone,
class Base
{
public : Base(int i)
{
printf("constru ctor\n");
}
void disp()
{
printf("display ed...\n");
}
virtual ~Base()
{
}
};
int main()
{
Base obj();
// obj.disp();
return(0);
}
What does the first line in main() function mean?
The second commented line causes a compilation error saying "left of
'.disp' must have class/struct/union type"
Thanks in advance!!! 9 1304
Rahul <sa*****@yahoo. co.inwrote:
class Base
{
public : Base(int i)
{
printf("constru ctor\n");
}
void disp()
{
printf("display ed...\n");
}
virtual ~Base()
{
}
};
int main()
{
Base obj();
// obj.disp();
return(0);
}
What does the first line in main() function mean?
It defines a function called "obj" that returns an object of type Base.
Much like "int main()" defines a function called "main" that returns an
int.
The second commented line causes a compilation error saying "left of
'.disp' must have class/struct/union type"
Try this instead:
int main()
{
Base obj;
obj.disp();
// return not necessary in main
}
On Jan 7, 10:16 pm, Rahul <sam_...@yahoo. co.inwrote:
Hi Everyone,
class Base
{
public : Base(int i)
{
printf("constru ctor\n");
}
void disp()
{
printf("display ed...\n");
}
virtual ~Base()
{
}
};
int main()
{
Base obj();
// obj.disp();
return(0);
}
What does the first line in main() function mean?
The second commented line causes a compilation error saying "left of
'.disp' must have class/struct/union type"
Thanks in advance!!!
The error is referring to left of disp(). So that error occurs in the
previous line.
Its telling you 'obj' is not an instance of a known type or union.
Look at your constructor for type Base....
Is it not declared as Base(int) ?
instead of
Base obj();
try
Base obj(8); // now that looks like Base(int)
See if you can follow this example:
#include <iostream>
class Base
{
int m_n;
public:
Base() : m_n(0)
{
std::cout << "default ctor\n";
}
Base(int i) : m_n(i)
{
std::cout << "Base(int)\ n";
}
void disp() const
{
std::cout << "Base::disp()\t ";
std::cout << "m_n = " << m_n;
std::cout << std::endl;
}
~Base()
{
std::cout << "~Base()\n" ;
}
};
void obj()
{
std::cout << "obj()\n";
}
int main()
{
Base base;
base.disp();
Base another_base(8) ;
another_base.di sp();
obj();
}
/*
default ctor
Base::disp() m_n = 0
Base(int)
Base::disp() m_n = 8
obj()
~Base()
~Base()
*/
On Jan 8, 8:45 am, "Daniel T." <danie...@earth link.netwrote:
Rahul <sam_...@yahoo. co.inwrote:
class Base
{
public : Base(int i)
{
printf("constru ctor\n");
}
void disp()
{
printf("display ed...\n");
}
virtual ~Base()
{
}
};
int main()
{
Base obj();
// obj.disp();
return(0);
}
What does the first line in main() function mean?
It defines a function called "obj" that returns an object of type Base.
Much like "int main()" defines a function called "main" that returns an
int.
The second commented line causes a compilation error saying "left of
'.disp' must have class/struct/union type"
Try this instead:
int main()
{
Base obj;
obj.disp();
// return not necessary in main
}
Yeah, its a function prorotype visible only in main function, however
i tried the following code,
class Base
{
public: Base(int i = 0)
{
cout<<"Construc tor"<<endl;
}
};
int main()
{
Base obj();
Base obj1(10); // Invokes constructor
Base obj2; // Invokes constructor
return ( 0 ) ;
}
i expected the constructor to be called in first case, as default
arguement is provided for the constructor...
On Jan 8, 1:41 am, Rahul <sam_...@yahoo. co.inwrote:
On Jan 8, 8:45 am, "Daniel T." <danie...@earth link.netwrote:
Rahul <sam_...@yahoo. co.inwrote:
class Base
{
public : Base(int i)
{
printf("constru ctor\n");
}
void disp()
{
printf("display ed...\n");
}
virtual ~Base()
{
}
};
int main()
{
Base obj();
// obj.disp();
return(0);
}
What does the first line in main() function mean?
It defines a function called "obj" that returns an object of type Base.
Much like "int main()" defines a function called "main" that returns an
int.
The second commented line causes a compilation error saying "left of
'.disp' must have class/struct/union type"
Try this instead:
int main()
{
Base obj;
obj.disp();
// return not necessary in main
}
Yeah, its a function prorotype visible only in main function, however
i tried the following code,
class Base
{
public: Base(int i = 0)
{
cout<<"Construc tor"<<endl;
}
};
int main()
{
Base obj();
Base obj1(10); // Invokes constructor
Base obj2; // Invokes constructor
return ( 0 ) ;
}
i expected the constructor to be called in first case, as default
arguement is provided for the constructor...
This invokes default construction:
Base obj;
this does not:
Base obj(); // is a function call, not a ctor
A function is looked for that isn't a constructor and matches the
provided signature.
example:
Base obj() { return Base(); } // returns a temporary by value
Base obj(); // does nothing [bad code example !!]
Now, you can use Base() in the case of a temporary, since a function
will enforce the type of the parameter.
ie:
void foo(const Base& b) { /*do stuff to a temp*/ }
int main()
{
foo(Base()); // Base() generates a temporary, this is ok
}
There is a distinction to be made here, since the function will only
accept a Base.
The program has no choice but to consider Base constructors alone. So
you can think of Base() as a direct call to the default constructor
(which generates the resulting temporary). And the code is safe
because the reference is a reference_to_co nst (the lifetime of the
temporary is guarenteed to last until function ends).
Recap:
the following statement creates a default object:
Base obj;
and this statement tries to run a function
Base obj();
and finally, the following creates a temporary
Base();
It will be crystal-clear soon enough. Its a subject that rears its
head daily in this newsgroup.
On Jan 8, 7:41 am, Rahul <sam_...@yahoo. co.inwrote:
On Jan 8, 8:45 am, "Daniel T." <danie...@earth link.netwrote:
Rahul <sam_...@yahoo. co.inwrote:
class Base
{
public : Base(int i)
{
printf("constru ctor\n");
}
void disp()
{
printf("display ed...\n");
}
virtual ~Base()
{
}
};
int main()
{
Base obj();
// obj.disp();
return(0);
}
What does the first line in main() function mean?
It defines a function called "obj" that returns an object of
type Base. Much like "int main()" defines a function called
"main" that returns an int.
Not "defines", declares.
The second commented line causes a compilation error
saying "left of '.disp' must have class/struct/union type"
Try this instead:
int main()
{
Base obj;
obj.disp();
// return not necessary in main
}
Yeah, its a function prorotype visible only in main function,
however i tried the following code,
class Base
{
public: Base(int i = 0)
{
cout<<"Construc tor"<<endl;
}
};
int main()
{
Base obj();
Base obj1(10); // Invokes constructor
Base obj2; // Invokes constructor
return ( 0 ) ;
}
i expected the constructor to be called in first case, as
default arguement is provided for the constructor...
The constructor is called when an object is constructed. The
first line in main, above, is not a definition of an object, but
a declaration of an external function. No object, no
constructor gets called.
This is sometimes called "C++'s most embarassing parse", and it
occasionally catches out even the experts. Regretfully, for
historical reasons, there's not much that can be done about it.
C++ is based on C, and as someone (I think it was Bjarne
Stroustrup) said: "C's declaration syntax can be considered an
experiment which failed." If the language were being designed
from scratch, there would probably be a keyword which would make
the distinction, e.g.:
int
main()
{
var Base obj() ; // Defines a variable...
fnc Base obj() ; // Declares a function...
//...
}
It would make reading the code easier for both compiler and
human readers.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
In article
<e2************ *************** *******@i7g2000 prf.googlegroup s.com>,
James Kanze <ja*********@gm ail.comwrote:
On Jan 8, 7:41 am, Rahul <sam_...@yahoo. co.inwrote:
On Jan 8, 8:45 am, "Daniel T." <danie...@earth link.netwrote:
Rahul <sam_...@yahoo. co.inwrote:
class Base
{
public : Base(int i)
{
printf("constru ctor\n");
}
void disp()
{
printf("display ed...\n");
}
virtual ~Base()
{
}
};
int main()
{
Base obj();
// obj.disp();
return(0);
}
What does the first line in main() function mean?
It defines a function called "obj" that returns an object of
type Base. Much like "int main()" defines a function called
"main" that returns an int.
Not "defines", declares.
The second commented line causes a compilation error
saying "left of '.disp' must have class/struct/union type"
Try this instead:
int main()
{
Base obj;
obj.disp();
// return not necessary in main
}
And while we are at it, Salt Peter caught the fact that the OP doesn't
have a default constructor so the above won't work either.
Rahul <sa*****@yahoo. co.inwrote in news:73a87338-c616-4406-84d7- 35**********@l6 g2000prm.google groups.com:
Hi Everyone,
class Base
{
public : Base(int i)
{
printf("constru ctor\n");
}
void disp()
{
printf("display ed...\n");
}
virtual ~Base()
{
}
};
int main()
{
Base obj();
// obj.disp();
return(0);
}
What does the first line in main() function mean?
You are declaring the existence of a function named 'obj' which takes no
parameters and returns an instance of a Base object.
The second commented line causes a compilation error saying "left of
'.disp' must have class/struct/union type"
Yep. obj isn't a class/struct/union. It's a function (which has no body
yet).
The extra parentheses mean something. Without the parens, the first line
becomes the definition of an object named 'obj' of type Base.
"Salt_Peter " <pj*****@yahoo. comwrote in message
news:5695f89f-5d29-4235-a789-
and this statement tries to run a function
Base obj();
No, it declares a function named obj which takes no parameters and returns
an object of type Base. That's different from actually calling such a
function. (And I suspect it will lead to a linker error, unless you provide
a function body for it somewhere.)
-Howard
On Jan 9, 9:07 am, "Howard" <m...@here.comw rote:
"Salt_Peter " <pj_h...@yahoo. comwrote in message
news:5695f89f-5d29-4235-a789-
and this statement tries to run a function
Base obj();
No, it declares a function named obj which takes no parameters and returns
an object of type Base. That's different from actually calling such a
function. (And I suspect it will lead to a linker error, unless you provide
a function body for it somewhere.)
-Howard
A prototype specification alone doesn't cause any linker error, may be
when the actual call is done and the library is not found... This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Richard |
last post by:
Hi all,
I am looking for some help on understanding the overhead associated with
object creation in Java.
I am writing an application where I have written a class to encapsulate some
text data.
The class is contains these private variables:
|
by: Sam Collett |
last post by:
Is there a basic guide on Xml document creation and editing (simpler
than the MSDN docs). Say I want to create a file containing the
following:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Files>
<File>
<Text>Test</Text>
<Name>Test.html</Name>
</File>
|
by: Anthony Munter |
last post by:
I have a web application with impersonate=â€true†in Web.config and on my own logon page I allow the user to either
- specify a userid/password for the app to impersonate when calling legacy COM objects
- or, just use the interactive user
If they choose to use the interactive option, the impersonate="true" means that the process runs under the interactiv
user (which I've confirmed works correctly). If they specify a userid/password, I...
|
by: John |
last post by:
Hi,
How can I achieve that a childs constructor is only callable from it's parent?
Must I declare the class Private within A?
Thanks in Advance
Public MustInherit Class A
Protected Sub New()
|
by: Rob Richardson |
last post by:
Greetings!
It seems to me that there is room for a lot of improvement in automatic
generation of database objects. If I create a data adapter from a table
shown in the Server Explorer, I get Select, Insert, Update, and Delete
commands, not all of which may be necessary. I don't have any control over
command names or parameter names. If all I am going to be doing is
inserting data into a table, I don't even need a data adapter. All I...
| |
by: Charles Law |
last post by:
As a matter of practice, where would people put the following elements of
object creation/initialisation:
Create shared member objects
Initialise shared member objects
Create non-shared member objects
Initialise non-shared member objects
Initialise peripherals
The places where these could take place would appear to be
|
by: dav3 |
last post by:
Any help here is appreciated folks.
First in my Person class the comments = errors visual basics is giving me and I am not sure why. Also when i try and set up my array of pointers to Student class I get the error that is in the comment. This is really bothering me as I spent the last hour and a half with a classmate working on this and we can not figure out whats up.
class Person
{
public:
Person(int sinNumber, char studentName);...
|
by: fiona |
last post by:
Reading, Berkshire, UK 05 June 2007 - Crainiate Software make details
available of the release of Objecto Framework 2.0, an upgrade to their
enterprise business component framework, designed to make it easy for
programmers to create agile persisted business objects that are
reusable, customisable and scalable, without additional developer
tools.
Objecto uses object persistence and managed database technology to
allow developers to...
|
by: cctv.star |
last post by:
I need to maintain certain data structure, shared by all instances of
a class, declared as . This data structure must change
whenever a new object is created or an existing object is destroyed.
So I declared a static field with attribute.
Now I need to add some code into all places where objects are created
and destroyed. This class has 2 ctors: one default, and another taking
parameters. I've found out, to my surprise, that new...
|
by: Tom P. |
last post by:
I am doing quite a bit of custom painting and it means I have to
create a lot of brushes (think one for every file system object in a
directory) per paint. How expensive is this? Should I find a way to
create the brushes once, store them in an member variable, and use
them when I need them? Or is creating brushes a throw-away process
that doesn't take a lot of work?
Thanks for the info.
Tom P.
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |